Programmatically analyze java heap dump file

白昼怎懂夜的黑 提交于 2019-11-30 16:20:53

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.

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!