Heap memory behavior

I always had a question about the behavior of heap memory.

Profiling my application I get the above graph. Everything seems to be in order. But what I do not understand is why, in GC time, a bunch of litle bits grow, even there is enough memory (red circle).

Does this mean that for an application with a long lead time, at some point it will exit the heap?

alt text

+6
java memory-management
source share
2 answers

Not necessary. The garbage collector can use up to the most distributed heap in any way convenient for him. Extrapolating future GC behavior based on current behavior (but with different memory conditions) is by no means guaranteed to be accurate.

This has an unfortunate side effect, which is very difficult to determine if an OutOfMemoryError will occur if it does not. A legitimate (but probably rather ineffective) garbage collector could just do nothing until the memory ceiling was hit, then make a stop world sign and expand the whole bunch. With this implementation, you will see that your memory is constantly increasing, and you might be tempted to say that OOME was inevitable, but you just can't say.

With such small heap sizes, the increase here is most likely due only to the equalization of the size of the accounting / cache / etc. You are talking about less than 50KB or so, looking at the resolution on the scale, so I should not worry.

If you do believe that there is a legitimate risk of OutOfMemoryErrors, the only way to show this is to conduct a stress test together and show that the application is indeed terminating from the heap.

+6
source share

HotSpot garbage collectors decide to increase the total heap size immediately after completing the full GC, if the ratio of free space to the total heap size falls below a certain threshold. This ratio can be configured using one of the many -XX options for the garbage collector.

Looking at the memory graph, you will see that the heap size increases at the “saw points”; those. local highs. Each of them corresponds to a full GC. If you carefully look at the “points” where the heap expands, you will see that in each case the amount of free space immediately after the full GC is slightly higher than the previous such “point”.

I see that what happens is that the memory usage of the application is cyclical. If the GC is operating at or near the high point of the loop, it will not be able to free up as much memory as if the GC was operating at or near the low point. This variability may be sufficient to cause the GC to expand the heap.

(Another possibility is that your application has a slow memory leak.)

Does this mean that for an application with a long lead time, at some point it will exit the heap?

Not. Assuming that the memory usage of the application (i.e., the integral of the space occupied by accessible objects) is cyclic, the heap size will approach a fixed upper limit and will never exceed it. Of course, SOME are not inevitable.

+4
source share

All Articles