General strategy for fixing Java memory leak?

I have a separate program that I run locally, it is designed to work with a server type that works 24 hours a day. I recently discovered that it has a memory leak, right now our only solution is to restart it every 4 hours. What is the best way to find a memory leak? What tool and method should we use?

+6
java memory-management memory memory-leaks
source share
7 answers

If you use Java from Sun and use at least an update for Java 6 (that is, the newest), try running jvisualvm from the JDK on the same computer as your program, and attach to it and enable profiling.

This is most likely the easiest way to get started.

+12
source share

When it comes to memory problems, I use the SAP Memory Analyzer Eclipse Memory Analyzer (MAT) , a heap dump analysis tool.

The memory analyzer provides a versatile tool for analyzing Java heap dumps. In addition to heap walking and quick calculation of stored sizes, the Eclipse tool reports suspected leaks and memory consumption. The main applications are memory errors and high memory consumption.

Initiated by SAP, the project has since been open and is now known as the Eclipse Memory Analyzer . Check the Getting Started and especially the Search for memory leaks (I insert it below because I fixed some links):

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

This blog post details how to find the Leaking Workbench window .

The memory analyzer grew up in SAP. At that time, Krum blogged about Searching for Memory Leaks with SAP Memory Analyzer . The content is still relevant!

This is probably the best tool you can get (even for money) to analyze heap dumps (and memory leaks).

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

+7
source share

You need a memory profiler . I recommend trying the Netbeans profiler .

+3
source share

You can look at JMX and the jconsole application that comes with Java. You can get interesting statistics from the box, and adding some simple tools to your classes can provide a lot more.

+2
source share

As already mentioned, jvisualvm is a great way to get started, but as soon as you know what's going on, you may need to find something that holds links to objects that I would recommend jmap and jhat about, for example

jmap -dump:live,file=heap.dump.out,format=b <pid> 

and

 jhat heap.dump.out 

where <pid> is easy to find from jvisualvm. Then, in the browser, go to localhost: 7000 and start exploring.

+2
source share

One approach would be to regularly get heap heaps, and then determine the number of instances of your classes, in order to try to determine which objects are sequentially created but not collected.

Another would be to disable parts of your application to try to narrow down where the problem is.

Take a look at tools like jmap and jhat.

+1
source share

You should try to grab a dump of the Java heap, which is the fingerprint of the Java process . This is a critical process for optimizing memory consumption and detecting memory leaks.

A Java heap dump is an important object for diagnosing memory problems, including java.lang.OutOfMemoryError, garbage collection problems, and memory leaks that are part of the Java web development process.

For clarity, the heap dump contains information, such as Java classes and objects on the heap, at the time the snapshot was taken.

To do this, you need to run jmap -dump:file=myheap.bin <program pid> .

To learn more about how to capture Java thermal dumps, check out: https://javatutorial.net/capture-java-heap-dump

+1
source share

All Articles