Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java heap size not entirely used

I'm currently monitoring my running java application with Visual VM: http://visualvm.java.net/

I'm stressing the memory usage by with -Xmx128m.

When running I see the heap size increasing to 128m (as expected) however the used heap converges to approximately 105m before I run into a java heap space error.

enter image description here

Why are these remaining 20m, not used?

like image 998
Will Avatar asked May 04 '11 10:05

Will


People also ask

What is the default Java heap size?

The Java™ virtual machine (JVM) heap size setting directly relates to how many server instances can be started within a dynamic cluster on a specific node. You might need to modify the JVM heap size setting based on your environment configuration. The default value is 256 MB.

What is the default JVM size?

By default, the JVM heap size is 1GB, which is usually enough to accommodate the data used by Task Engine. However, larger heap size may be required under some circumstances, for example, when the average size of the parameters in a task is very large.


2 Answers

You need to understand a central fact about garbage collector ergonomics:

The costly part of garbage collection is finding and dealing with the objects that are NOT garbage.

This means: as the heap gets close to its maximum capacity, the GC will spend more and more time for less and less return in reclaimed space. If the GC was to try and use every last byte of memory, the net result would be that your JVM would spend more and more time garbage collecting, until ... eventually ... almost no useful work was being done.

To avoid this pathological situation, the JVM monitors the ratio of time is spent GC'ing and doing useful work. When the ratio exceeds a configurable threshold value, the GC raises an OutOfMemoryError ... even though (technically) there is free memory available. This is probably what you are seeing, though the other explanations are equally plausible.

You can change the GC thresholds, generation sizes, etc via JVM options, but it is probably better not to. A better idea is to figure out why your application's memory usage is continually creeping upwards. There are most likely memory leaks ... i.e. a bugs ... in your code that are causing this. Spend your effort finding and fixing those bugs, rather than worrying about why you are not using all of the memory.

(In fact, you are using it ... but not all of the time.)

like image 61
Stephen C Avatar answered Sep 25 '22 12:09

Stephen C


The heap is split up in Young-Generation (Eden-Space, and two Survivor-Spaces of identical size usually called From and To), Old Generation (Tenured) and Permanent Space.

The Xmx/Xms option sets the overall heap size. So a region (with a default size) is actually the Permanent Space - and maybe, we don't know details about your stress test, no objects are actually moved from eden to tenured or permanent, so those regions remain empty while Eden runs out of space.

like image 26
Andreas Dolk Avatar answered Sep 22 '22 12:09

Andreas Dolk