2

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...

vach
  • 10,571
  • 12
  • 68
  • 106
  • 4
    Garbage Collection is a perfectly normal part of the lifecycle of a Java application. Why in particular is the fact that it's happening concerning? What problems is it causing that you're trying to solve? – JonK Mar 20 '15 at 16:12
  • I've updated the last part, but yes you're right and i know that any jvm app is suppose to do this, but in this case we want to optimize code, we need to detect what code creates those unnecessary objects. All i can see is that my app allocates lot of byte[] and char[] but where when from what function? thats the problem – vach Mar 20 '15 at 16:14
  • GC is not the problem, its the frequency of it that causes issues, i want to optimize code so that this GC runds 10 times rarely or more... Yes I can read whole source code but rather i'd like to get a tool that could analyze and give me a clue where the garbage is created... – vach Mar 20 '15 at 16:16
  • Don't forget that `String` objects are backed by `char` arrays - you're very likely seeing the backing arrays for the many `String`s that your application has created. You may also find that if you're running your application through a third party app-server such as JBoss that a good number of the objects are actually created and managed by the app server. – JonK Mar 20 '15 at 16:23

2 Answers2

2

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.

Jakub Kubrynski
  • 13,724
  • 6
  • 60
  • 85
1

I would suggest install VisualGC plugin in jvisualvm. It will give you very good idea about number of small and full GCs happening.

enter image description here

If you are looking for garbage collected objects and possible chance of memory leaks than you should inspect heap dump at two different instances of your code workflow.

Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289