6

I want to write a program (preferably in java) that will parse and analyze a java heap dump file (created by jmap). I know there are many great tools that already do so (jhat, eclipse's MAT, and so on), but I want to analyze the heap from a specific perspective to my application.

Where can I read about the structure of the heap dump file, examples how to read it, and so on? Didn't find anything useful searching for it...

Many thanks.

kaiz.net
  • 1,984
  • 3
  • 23
  • 31
duduamar
  • 3,816
  • 7
  • 35
  • 54

2 Answers2

4

The following was done with Eclipse Version: Luna Service Release 1 (4.4.1) and Eclipse Memory Analyzer Version 1.4.0

Programmatically Interfacing with the Java Heap Dump

Environment Setup

  1. In eclipse, Help -> Install New Software -> install Eclipse Plug-in Development Environment
  2. In eclipse, Window -> Preferences -> Plug-in Development -> Target Platform -> Add
  3. Nothing -> Locations -> Add -> Installation
  4. Name = MAT
  5. Location = /path/to/MAT/installation/mat

Project Setup

  1. File -> new -> Other -> Plug-in Project

    Name: MAT Extension

  2. Next

    • Disable Activator
    • Disable Contributions to the UI
    • Disable API analysis
  3. Next
    • Disable template
  4. Finish

Code Generation

Open plugin.xml

  1. Dependencies -> Add
    • select org.eclipse.mat.api
  2. Extensions -> Add
    • select org.eclipse.mat.report.query
  3. right click on report.query -> New
    • Name: MyQuery
    • click "impl" to generate the class

Implementing IQuery

@CommandName("MyQuery") //for the command line interface
@Name("My Query") //display name for the GUI
@Category("Custom Queries") //list this Query will be put under in the GUI
@Help("This is my first query.") //help displayed
public class MyQuery implements IQuery
{
     public MyQuery{}

     @Argument //snapshot will be populated before the call to execute happens
     public ISnapshot snapshot;

     /*
      * execute : only method overridden from IQuery
      *           Prints out "My first query." to the output file.
     */
     @Override
     public IResult execute(IProgressListener arg0) throws Exception
     {
            CharArrayWriter outWriter = new CharArrayWriter(100);
            PrintWriter out = new PrintWriter(outWriter);
            SnapshotInfo snapshotInfo = snapshot.getSnapshotInfo();
            out.println("Used Heap Size: " + snapshotInfo.getUsedHeapSize());
            out.println("My first query.")
            return new TextResult(outWriter.toString(), false);
     }
}

ctrl+shift+o will generate the correct "import" statements. Custom queries can be accessed within the MAT GUI by accessing the toolbar's "Open Query Browser" at the top of the hprof file you have opened.

The ISnapshot interface

The most important interface one can use to extract data from a heap dump is ISnapshot. ISnapshot represents a heap dump and offers various methods for reading object and classes from it, getting the size of objects, etc…

To obtain an instance of ISnapshot one can use static methods on the SnapshotFactory class. However, this is only needed if the API is used to implement a tool independent of Memory Analyzer. If you are writing extensions to MAT, then your coding will get an instance corresponding to an already opened heap dump either by injection or as a method parameter.

Reference

Built in Command Line Utility

If you're looking to have a program generate the usual reports, there is a command line utility called ParseHeapDump available with any download of Eclipse's MAT tool. You'll be able to get useful html dumps of all the information MAT stores.

> ParseHeapDump <heap dump> org.eclipse.mat.api:suspects org.eclipse.mat.api:top_components org.eclipse.mat.api:overview #will dump out all general reports available through MAT

Hopefully this is enough information to get you started.

0

I'm not familiar with jhat, but Eclipse's MAT is open source. Their SVN link is available, perhaps you could look through that for their parser, perhaps even use it.

Michael
  • 1,239
  • 9
  • 14
  • jhat's source code is also available: http://hg.openjdk.java.net/build-infra/jdk7-modules/jdk/file/ea2549112801/src/share/classes/com/sun/tools/hat/ – J-16 SDiZ Jun 20 '12 at 20:41
  • Looking at their website, it says you can even write extensions to MAT with custom logic to analyze the heap, sounds exactly what i'm looking for. thanks. – duduamar Jun 21 '12 at 04:00