Search code examples
javamemory-managementoptimizationgarbage-collectiongarbage

java eliminating garbage generation


Suppose we have java task that is working in isolation and we are able to monitor it using visualvm... and we notice continuous garbage creation and periodic gc like this.

enter image description here

How do we detect what exactly is causing this issue

is there a way to see which method execution is generating garbage? how do we see where the garbage comes from?

yes we can see what objects exactly are allocating memory, but thats not helpful... i believe lot of objects are created and garbaged later, but i cant figure out where that happens and what exactly causes this...

How do we do this usually? what tools to use? any links to topics about this are appreciated

NOTE the problem here is not the GC parameter optimization, but rather the code optimization, we want to eliminate unnecessary object creation, maybe use primitives instead etc...


Solution

  • The easiest way is to use tool like JProfiler and record allocations. The "Allocation HotSpot" view will show in which method your application is allocating the objects. More details can be found here

    When you cannot use profiler another approach is to take a heapdump and investigate the objects it contains. Then based on this information assume in which method they are instantiated.