سؤال

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 :) )

هل كانت مفيدة؟

المحلول

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()

نصائح أخرى

I've used this function before to look for classes that are loaded multiple times (usually, this happens when two different class loaders load the same lib taking more memory for no reason, and making the JVM serialize and deserialize objects passed from one class instance to the other -because it doesn't know that they are actually the same class-)

This is my OQL script that selects (and count) classes that has the same name:

var classes = {};
var multipleLoadedClasses = {};

heap.forEachClass(function(it) {
    if (classes[it.name] != null) {
        if (multipleLoadedClasses[it.name] != null) {
            multipleLoadedClasses[it.name] = multipleLoadedClasses[it.name] + 1;
        } else {
            multipleLoadedClasses[it.name] = 1;
        }
    } else {
        classes[it.name] = it;
    }
});

multipleLoadedClasses;

hopes that this will help further visitors ;)

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top