Panciz
Panciz

Reputation: 2234

How Jvm6 reduce heap size when is not necessary

I'm monitoring a Java application running on a Jvm 6. Here a screenshot of the jvisualVM panel 1.

I notice that when the heap size is small (before 12:39 in the picture) the garbage collector runs frequently. Then I run a memory expensive task a couple of times (from 12:39 to 12:41) and the heap space grows. Why from that point on the garbage collector runs less frequently?

After one hour or more, if I avoid executing the expensive tasks on the application the heap space slowly decrease. Why the used heap space takes so long to decrease?

Is there something I can do to avoid this behaviour? Does the new Java8 VM have a different behaviour?

Upvotes: 0

Views: 96

Answers (2)

jacks
jacks

Reputation: 4783

The behaviour looks normal.

Up until 12:39 on your attached profile snapshot there isn't a lot of GC going on.

Then you run your tasks and as objects that are no longer reachable become eligible for GC the sweep marks them and they get removed.

You do not necessarily need to worry about the size of the heap unless you are maxing out and crashing frequently due to some memory leak. The GC will take care of removing eligible objects from the heap and you are limited in terms of how you can impact GC (unless of course you switch GC implementation).

Each major release of the platform includes some JVM and GC changes/improvements but the behaviour of application will be very similar in Hotspot 7/8. Try it.


Modern JVMs have highly optimized garbage collectors and you shouldn't need to worry about how/when it reclaims memory, but more about making sure you release objects so that they become eligible for collection. How often after startup do you experience out of memory issues?

If you are getting crashes due to out of memory configure the JVM to take a heap dump on exit: -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=date.hprof

Upvotes: 1

the8472
the8472

Reputation: 43115

Is there something I can do to avoid this behaviour?

Set -XX:MaxHeapFreeRatio=30 -XX:MinHeapFreeRatio=15, that'll shrink the heap size more aggressively. Note that not all GC implementations yield the memory they don't use back to the OS. At least G1 does, but that's not available on java 6.

Upvotes: 2

Related Questions