Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Low GC in Java: Using primitives

DZone's Guide to

Low GC in Java: Using primitives

· Java Zone
Free Resource

Bitbucket is for the code that takes us to Mars, decodes the human genome, or drives your next car. What will your code do? Get started with Bitbucket today, it's 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 performance range was the typical (50%tile) and one of the higher (98%tile) timings. The garbage was the result of 900,000 loops per second.


These tests were run on my new system.

The Code

Loop tested
int 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

Bitbucket is the Git solution for professional teams who code with a purpose, not just as a hobby. Get started today, it's free.

Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}