Search code examples
javajvmheap-memoryoqljhat

Using Jhat heap built-in object


Can anybody show an example of how to use heap.heapForEachClass in a select statement? It would be great if you could provide some links with different examples of queries (other than those in the oqlhelp page of course :) )


Solution

  • I don't believe heap.forEachClass() is meant to be used in a select statement, at least not directly. Consider the fact that it doesn't return anything:

    var result=heap.forEachClass(function(it){return it;});
    typeof result
    //returns undefined
    

    The OQL used in jhat and VisualVM does support plain ol' JavaScript, just like the "query" I use above. I believe that the heap.forEachClass() finds more use in either JavaScript-style queries or in JavaScript functions within select-type queries.

    That said, I don't know why this function exists since the heap.classes() enumeration is much easier to use, both with with select-style queries and plain JavaScript ones.

    You could even even recreate the same functionality as heap.forEachClass() with the following JavaScript function:

    function heapForEachClass(func){
        map(heap.classes(),func)
        return undefined;
    }
    

    Any sample queries that I could provide you would likely be easier written with heap.classes(). For example, you could use heap.forEachClass() to get list of all classes:

    var list=[];
    heap.forEachClass(function(it){
        list.push(it);
    });
    list
    

    but this is more complicated than how you'd do it with heap.classes():

    select heap.classes()
    

    or just

    heap.classes()