8

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?

erotsppa
  • 14,248
  • 33
  • 123
  • 181

7 Answers7

12

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.

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
  • Last time I checked, the newest update was 16... :-) Not sure when VisualVM was introduced though. Is it so recent? – PhiLho Sep 24 '09 at 19:45
  • JVisualVM was added to the JDK in 1.6.0_07, I believe. – Michael Myers Sep 24 '09 at 20:50
  • 1
    Start VisualVM. Select the running process from the list. Right click and choose heap dump. Let it run for a bit. Capture another heap dump. Then use File > Compare Memory Snapshots to see what changed. – Devon_C_Miller Sep 24 '09 at 20:58
7

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.

Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
3

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

Martin v. Löwis
  • 124,830
  • 17
  • 198
  • 235
2

One approach would be to take heap dumps on a regular basis, then trend the instance counts of your classes to try to work out which objects are being consistently created but not collected.

Another would be to switch off parts of your app to try to narrow down where the problem is.

Look at tools like jmap and jhat.

daveb
  • 74,111
  • 6
  • 45
  • 51
2

You might look up JMX and the jconsole app that ships with Java. You can get some interesting statistics out-of-the-box, and adding some simple instrumentation to your classes can provide a whole lot more.

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
2

As already stated jvisualvm is a great way to get started, but once you know what is leaking you may need to find what is holding references to the objects in question for which I'd recommend jmap and jhat, e.g

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

and

jhat heap.dump.out

where <pid> is easily found from jvisualvm. Then in a browser navigate to localhost:7000 and begin exploring.

Steve Bosman
  • 2,599
  • 1
  • 25
  • 41
1

You need to try and capture Java heap dump which is a memory print of the Java process. It's a critical process for memory consumption optimisation and finding memory leaks.

Java heap dump is an essential object for diagnosing memory-linked issues including java.lang.OutOfMemoryError, Garbage Collection issues, and memory leaks which are all part of Java web development process

For clarity, a Heap dump contains information such as Java classes and objects in a heap during instant of taking the snapshot.

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

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

Johnny
  • 14,397
  • 15
  • 77
  • 118