How to find a Java Memory Leak

后端 未结 11 854
遇见更好的自我
遇见更好的自我 2020-11-22 07:48

How do you find a memory leak in Java (using, for example, JHat)? I have tried to load the heap dump up in JHat to take a basic look. However, I do not understand how I am s

相关标签:
11条回答
  • 2020-11-22 08:03

    A tool is a big help.

    However, there are times when you can't use a tool: the heap dump is so huge it crashes the tool, you are trying to troubleshoot a machine in some production environment to which you only have shell access, etc.

    In that case, it helps to know your way around the hprof dump file.

    Look for SITES BEGIN. This shows you what objects are using the most memory. But the objects aren't lumped together solely by type: each entry also includes a "trace" ID. You can then search for that "TRACE nnnn" to see the top few frames of the stack where the object was allocated. Often, once I see where the object is allocated, I find a bug and I'm done. Also, note that you can control how many frames are recorded in the stack with the options to -Xrunhprof.

    If you check out the allocation site, and don't see anything wrong, you have to start backward chaining from some of those live objects to root objects, to find the unexpected reference chain. This is where a tool really helps, but you can do the same thing by hand (well, with grep). There is not just one root object (i.e., object not subject to garbage collection). Threads, classes, and stack frames act as root objects, and anything they reference strongly is not collectible.

    To do the chaining, look in the HEAP DUMP section for entries with the bad trace id. This will take you to an OBJ or ARR entry, which shows a unique object identifier in hexadecimal. Search for all occurrences of that id to find who's got a strong reference to the object. Follow each of those paths backward as they branch until you figure out where the leak is. See why a tool is so handy?

    Static members are a repeat offender for memory leaks. In fact, even without a tool, it'd be worth spending a few minutes looking through your code for static Map members. Can a map grow large? Does anything ever clean up its entries?

    0 讨论(0)
  • 2020-11-22 08:10

    Questioner here, I have got to say getting a tool that does not take 5 minutes to answer any click makes it a lot easier to find potential memory leaks.

    Since people are suggesting several tools ( I only tried visual wm since I got that in the JDK and JProbe trial ) I though I should suggest a free / open source tool built on the Eclipse platform, the Memory Analyzer (sometimes referenced as the SAP memory analyzer) available on http://www.eclipse.org/mat/ .

    What is really cool about this tool is that it indexed the heap dump when I first opened it which allowed it to show data like retained heap without waiting 5 minutes for each object (pretty much all operations were tons faster than the other tools I tried).

    When you open the dump, the first screen shows you a pie chart with the biggest objects (counting retained heap) and one can quickly navigate down to the objects that are to big for comfort. It also has a Find likely leak suspects which I reccon can come in handy, but since the navigation was enough for me I did not really get into it.

    0 讨论(0)
  • 2020-11-22 08:12

    NetBeans has a built-in profiler.

    0 讨论(0)
  • 2020-11-22 08:14

    I use following approach to finding memory leaks in Java. I've used jProfiler with great success, but I believe that any specialized tool with graphing capabilities (diffs are easier to analyze in graphical form) will work.

    1. Start the application and wait until it get to "stable" state, when all the initialization is complete and the application is idle.
    2. Run the operation suspected of producing a memory leak several times to allow any cache, DB-related initialization to take place.
    3. Run GC and take memory snapshot.
    4. Run the operation again. Depending on the complexity of operation and sizes of data that is processed operation may need to be run several to many times.
    5. Run GC and take memory snapshot.
    6. Run a diff for 2 snapshots and analyze it.

    Basically analysis should start from greatest positive diff by, say, object types and find what causes those extra objects to stick in memory.

    For web applications that process requests in several threads analysis gets more complicated, but nevertheless general approach still applies.

    I did quite a number of projects specifically aimed at reducing memory footprint of the applications and this general approach with some application specific tweaks and trick always worked well.

    0 讨论(0)
  • 2020-11-22 08:17

    As most of us use Eclipse already for writing code, Why not use the Memory Analyser Tool(MAT) in Eclipse. It works great.

    The Eclipse MAT is a set of plug-ins for the Eclipse IDE which provides tools to analyze heap dumps from Java application and to identify memory problems in the application.

    This helps the developer to find memory leaks with the following features

    1. Acquiring a memory snapshot (Heap Dump)
    2. Histogram
    3. Retained Heap
    4. Dominator Tree
    5. Exploring Paths to the GC Roots
    6. Inspector
    7. Common Memory Anti-Patterns
    8. Object Query Language

    0 讨论(0)
提交回复
热议问题