Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tools for querying collections in heap dump

Tags:

java

eclipse

Is it possible to query a collection that is dumped in a heap dump? I can obviously browse that collection using something like Eclipse MAT, but would really love to be able to actually call a getter on the collection object. This would obviously be a lot clearer than going through segments in a ConcurrentHashMap trying to find the mapping I need.

I suppose what I'm looking for is some way to 'rehydrate' the dumped state of a named collection so that it can then be manipulated using the standard APIs.

Does anyone know of any such utilities, or, if not, can someone provide some sample code or pointers as to how to achieve this?

like image 832
Rich Avatar asked Feb 19 '10 12:02

Rich


People also ask

Which tool is specially designed for analyzing Java heap dumps?

The recommended tool is IBM Monitoring and Diagnostic Tools for Java - Memory Analyzer. Note that heap dumps are platform agnostic and can be analysed on any platform regardless of where they were created.

How do I analyze heap dump in IBM HeapAnalyzer?

IBM HeapAnalyzer It will open the Analyzer. To open the heap dump, Go to File >> Option and select the heap dump. Based on file size, it may take few seconds and then give you summary view.

Does taking heap dump trigger GC?

Just to reply to one of the points here - yes, using VisualVM to perform a Heap Dump will always cause a Full GC to be performed before the dump.


1 Answers

You should be able to query all collections or single one with Object Query Language (OQL) in jhat.

You can't necessarily invoke arbitrary methods but you can write some complicated queries using the available functions.

A. Sundararajan has some interesting blog posts on the subject that showcase what you can do. See here and here.

For instance, you can find all instances of java.util.HashMap that have a key test with the following query:

select s from java.util.HashMap s where contains(s.table, function(it) {
    if (it && it.key && it.key.value && it.key.value.toString() == 'test') {
        return true;
    }
    return false;
})

This should find the same key in a java.util.concurrent.ConcurrentHashMap:

select s from java.util.concurrent.ConcurrentHashMap s where contains(s.segments, function(it) {
    if (!it || !it.table) {
        return false;
    }
    var i, e;
    for (i=0; i < it.table.length; i = i + 1) {
        e = it.table[i];
        if (e) {
            return e.key && e.key.value && e.key.value.toString() == 'test';
        }
    }
    return false;
})
like image 148
Kevin Avatar answered Oct 09 '22 12:10

Kevin