learning sites for fixing java memory leaks

568 views Asked by At

What would be the best places to learn fixing a java memory leak ? I have been trying to find good resource over the NET but to my disappointment, I find toy examples being discussed. I am also able to troubleshoot small toy dumps but the real world application dumps are more challenging and give little clue.

I have tried tools like Jhat, JMap, VisualVM and MAT.

what would be the best place to learn about fixing Java memory leaks ? suggestion of a book is also welcome.

thanks in advance.

2

There are 2 answers

3
Ingo Kegel On

There are many kind of memory leaks but in practice the following strategy gives good results (Disclaimer: I'm showing screen shots from JProfiler, because my company develops it):

1. Check the biggest objects

"Biggest objects" retain a lot of memory that nobody else is holding on to. In JProfiler, they can be found in the "Biggest objects" view of the heap walker. This often happens with caches.

enter image description here

2. Check classes with a lot of instances

Sometimes, the references are more intricate and the biggest objects do not give a clue. Classes with a lot of instances can be checked for their incoming references. First, it is helpful to look at cumulated references, to separate valid reference chains from suspicious reference chains.

enter image description here

Also, checking where instances have been allocated (as opposed to how they are referenced) can often give an insight on what is going on.

enter image description here

Often, this is a multi-step process, selecting reference chains, allocation spots, or making selections based on the contents of objects.

3. Search for paths to GC roots

If that still does not provide a clue, you can select single objects and show their reference chains that lead to garbage collector roots. It helps a lot of you were able to narrow down an object set of suspicious objects before trying to to that, otherwise you may have a lot of candidates.

enter image description here

There is also a screen cast on this topic.

4
Amir Afghani On

Have you tried searching the internet? There are many examples. A good search engine will sort them by relevance.