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
- In eclipse, Help -> Install New Software -> install Eclipse Plug-in Development Environment
- In eclipse, Window -> Preferences -> Plug-in Development -> Target Platform -> Add
- Nothing -> Locations -> Add -> Installation
- Name = MAT
- Location = /path/to/MAT/installation/mat
Project Setup
File -> new -> Other -> Plug-in Project
Name: MAT Extension
Next
- Disable Activator
- Disable Contributions to the UI
- Disable API analysis
- Next
- Finish
Code Generation
Open plugin.xml
- Dependencies -> Add
- select org.eclipse.mat.api
- Extensions -> Add
- select org.eclipse.mat.report.query
- 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.