Low GC in Java: Using primitives
Join the DZone community and get the full member experience.
Join For Free
in a recent article i examined how using primitives and collections
which support primitives natively instead of wrappers and standard
collections can reduce memory usage and improve performance.
different way to have a map of int/integer
there are a number of ways you can use int/integer and a number of collections you store them in. depending on which approach you use can have a big difference on the performance and the amount of garbage produced.test | performance range | memory used |
---|---|---|
use integer wrappers and hashmap | 71 - 134 (ns) | 53 mb/sec |
use int primitives and hashmap | 45 - 76 (ns) | 36 mb/sec |
use int primitives and fastmap | 58 - 93 (ns) | 28 mb/sec |
use int primitives and tintinthashmap | 18 - 28 (ns) | nonimal |
use int primitives and simple hash map | 6 - 9 (ns) | nonimal |

the code
loop testedint runs = 300 * 300; for (int i = 0; i < runs; i++) { int x = i % 300; int y = i / 300; int times = x * y; integer count = counters.get(times); if (count == null) counters.put(times, 1); else counters.put(times, count + 1); }directory of performance examples
from http://vanillajava.blogspot.com/2011/07/low-gc-in-java-using-primitives.html
Java (programming language)
garbage collection
Data Types
Memory (storage engine)
Testing
Garbage (computer science)
Data structure
Directory
Opinions expressed by DZone contributors are their own.
Comments