views:

393

answers:

7
+4  Q: 

Java memory leak

Has anybody used Eclipse memory manager to detect memory leak in java codes? Can anybody recommend a good place to look for information regarding using memory manager? I read something online, it suggests that i need to let the program run until it crashes (out of memory error occurs), which will generates a crash report. Then use the memory manager to open this report to examine where the memory leak might occur. Is this how everybody uses memory manager?

+2  A: 

Perhaps the simplest thing is to run your program under HProf (comes as standard with the JVM) for some time, and force an exit. The output of HProf should hopefully give you some immediate pointers re. your memory leak.

Unlike the Eclipse memory debugger (which I'm only knowledgeable about from what you write) you can gather statistics from any point in the execution.

Brian Agnew
+1  A: 

This page explains working with jvm heap dumps. Jhat is a simpler, if less graphic way of working with heaps, but you can also load the same dump files into the eclipse memory manager. You can also get some information from jvisualvm, if you're using a current(1.6) jvm.

Steve B.
A: 

I have never used it myself, but you may find this link helpful.

David Pierre
+2  A: 

it suggests that i need to let the program run until it crashes (out of memory error occurs), which will generates a crash report.

I don't think this is true - you won't get a dump file when an OutOfMemoryError occurs (I would bet the author is confusing this problem with some sort of JVM bug that would cause a core dump to be saved).

The best procedure is to take a heap dump using jmap; this will output the contents of the heap to a binary file (usually known as hprof file). This file can then be analyzed by any number of analyzers:

  • jhat - Sun tool that analyzes hprof file, starts up an embedded web server so you can analyze the heap thru a web browser / view reports. I've found that this is very slow for large heaps.
  • VisualVM - Awesome debugging/troubleshooting tool bundled with the JDK. Among other things, this can also be used to generate a heap dump of any running process, as well as a thread dump. I've found that it's very slow to load large hprof files as well though.
  • Eclipse Memory Analyzer - Eclipse plugin that can generate hprof files.

I would highly recommend using the Eclipse plugin, as it is very quick to load large (> 500MB) heap dumps (in under a minute), produces useful reports, supports a query language with complex logic, etc.

matt b
You can request heap dumps on OOME with [1][-XX:+HeapDumpOnOutOfMemoryError]. [1] http://java.sun.com/javase/6/webnotes/trouble/TSG-VM/html/clopts.html
Michael Brewer-Davis
didn't realize that, useful to know - thanks!
matt b
A: 

I generally prefer profiling applications using the NetBeans Profiler. You can fairly easily see which objects are leaking and where they are created in most cases. There are likely several other tools that will do this as well, but I know the NetBeans profiler works well, and is easy to use.

Nerdfest
here a tutorial http://www.netbeans.org/kb/articles/nb-profiler-uncoveringleaks_pt1.html
davideconsonni
A: 

You can try using Jprobe. You can monitor your application and can look into the objects as they get created. Also this will help analysing what objects dont get garbage collect and will be pointers to move on.

Although it is not free, but I remember it comes with a trial licence, so check for that.

Nrj
+2  A: 

Though -XX:+HeapDumpOnOutOfMemoryError can be useful, my current workflow for using the Eclipse Memory Manager is:

  1. Run the program normally
  2. Wait for memory to get out of hand.
  3. Run jmap: jmap -dump:format=b,file=dump.hprof <PID>
  4. Open the hprof file in EMM.

I usually start working with the histogram and dominator tree views to see if anything seems out of whack, then drill down from there.

VisualVM can be useful but seems much less efficient than EMM when working with a heap dump (EMM caches a lot of information on loading the heap dump). Netbeans Profiler is nice for getting the locations of allocations and for time profiling.

Michael Brewer-Davis