1

I am trying to analyze a memory leak using Eclipse MAT. I already know that the issue is caused by an object that is essentally a linked list, e.g. something like this:

class Obj {
  byte[] data;
  Obj next;
}

The problem is that it is very deep (around 40,000 elements), and it's also not the standard LinkedList, so MATs support for that is no available here.

Having the first element, I'm trying to get to the bottom of the list, but I couldn't find anything better than simply right clicking next and choosing Go To, which at 40,000 elements would take ages. Is there a way to use some kind of scripting to get to the bottom of the linked stack easily?

SztupY
  • 10,291
  • 8
  • 64
  • 87

1 Answers1

2

Is there a way to use some kind of scripting to get to the bottom of the linked stack easily?

What you could do is use jhat to rely on the Object Query Language also know as OOL (which a SQL-like query language to query heap dumps) to execute a query against your heap dump.

1. Launch jhat

jhat <path-to-my-heap-dump-file>

This will start a web server by default on the port 7000

2. Go to the OOL page

  1. Go to http://localhost:7000/
  2. Click on Execute Object Query Language (OQL) query in Other Queries's section

3. Execute my query

Assuming that you want to get the instance of Obj whose field next is null, the query to launch will be of type:

select o from my.package.Obj o where o.next == null

You will then find all instances of the class my.package.Obj whose field next is null. Just click on the instances of your choice to get deeper information about them.

For more complex queries, you can refer to the doc available from http://localhost:7000/oqlhelp/.


Another way to do the same thing is to use jvisualvm which is much more user friendly but also more memory consuming than jhat.

  1. Start jvisualvm
  2. Go to File/Load...
  3. Choose your heap dump file then click Open, it will open your heap dump
  4. Then click on OQL Console
  5. In the Query Editor's section put your query and click on Execute
  6. Your result will then appear in the Query Results's section, you will then be able to go deeper on the instances of your choice
Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122
  • 2
    getting closer with this, apparently there are still quite a lot of leaf objects, but fortunately not as much as the original depth of the large objects. Going to play with OQL filters. Note: MAT also supports OQL – SztupY Sep 30 '16 at 11:52