Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

General strategy to resolve Java memory leak?

I have a standalone program that I run locally, it is meant to be a server type program running 24/7. Recently I found that it has a memory leak, right now our only solution is to restart it every 4 hours. What is the best way to go about finding this memory leak? Which tool and method should we use?

like image 380
erotsppa Avatar asked Sep 24 '09 18:09

erotsppa


2 Answers

If you are using Java from Sun and you use at least Java 6 update 10 (i.e. the newest), then try running jvisualvm from the JDK on the same machine as your program is running, and attach to it and enable profiling.

This is most likely the simplest way to get started.

like image 191
Thorbjørn Ravn Andersen Avatar answered Oct 20 '22 17:10

Thorbjørn Ravn Andersen


When it comes to hunting memory problems, I use SAP Memory Analyzer Eclipse Memory Analyser (MAT), a Heap Dump analysis tool.

The Memory Analyzer provides a general purpose toolkit to analyze Java heap dumps. Besides heap walking and fast calculation of retained sizes, the Eclipse tool reports leak suspects and memory consumption anti-patterns. The main area of application are Out Of Memory Errors and high memory consumption.

Initiated by SAP, the project has since been open sourced and is now know as Eclipse Memory Analyser. Check out the Getting Started page and especially the Finding Memory Leaks section (I'm pasting it below because I fixed some links):

Start by running the leak report to automatically check for memory leaks.

This blog details How to Find a Leaking Workbench Window.

The Memory Analyzer grew up at SAP. Back then, Krum blogged about Finding Memory Leaks with SAP Memory Analyzer. The content is still relevant!

This is probably the best tool you can get (even for money) for heap dump analysis (and memory leaks).

PS: I do not work for SAP/IBM/Eclipse, I'm just a very happy MAT user with positive feedback.

like image 22
Pascal Thivent Avatar answered Oct 20 '22 17:10

Pascal Thivent