There are a few things to consider:
- How big are these PersonDetail objects?
- How much memory is required to create a PersonDetail object that is not part of the object?
This second value can be very much. A list of arrays will cause garbage every so often. The process of creating the string "nameConst + i" will result in some garbage objects.
Suppose the answer is that PersonDetail is small, but it takes an average amount of memory to create it. Then the JVM will have a bunch of garbage to throw away (garbage collection) for every PersonDetail you create. When you create PersonDetails, garbage will accumulate and eventually the JVM will collect it. At the stage of garbage collection, a lot of free memory will be detected, since most of the allocations are short-lived objects. This can cause the graph to look like the one shown in the second image, a saw blade that uses memory, after which a lot of garbage is collected.
Now imagine that you are quickly creating a bunch of objects (without a sleep instruction). Now the total used memory will grow rapidly both through garbage and through objects that you keep in the list. When you get to maximum memory, garbage collection will occur more often. View as the top graph.
I'm not sure if this is what happens, but one thing you can do to look at it is to look at the selection in VisualVM and see how many objects are created by the class, and more importantly, how much memory they take Compare the amount used by PersonDetails with other material that is simply cleaned up when needed.
source share