Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OQL query to find all instances and sub-instances of a given class referred to from a session

I'm trying to use jhat/OQL to trace a memory leak in our Tomcat container. The question I want to ask is:

"Show me all the instances (and sub-instances) of foo.bar.Cacheable class that are reachable from javax.servlet.http.HttpSession"

I managed to come up with the following, but this doesn't show subclasses of foo.bar.Cacheable (which is important since this is infact an interface).

select filter(reachables(s), "/foo.bar.Cacheable/(classof(it).name)") from javax.servlet.http.HttpSession s

I tried various permutations of the concept below, but just keep getting errors ("foo" is not defined).

select filter(reachables(s), classof(it) instanceof foo.bar.Cacheable) from javax.servlet.http.HttpSession s

Can anyone help me out with what I'm doing wrong to pose this question through OQL?

like image 497
Scruffers Avatar asked Oct 14 '22 06:10

Scruffers


1 Answers

In jhat based OQL implementations (jHat, VisualVM) you can exploit the fact that you are not limited to the SQL like syntax but you have a complete javascript engine at your hands.

The following piece of code would do the trick

var containerSuperClass = "javax.servlet.http.HttpSession"
var elementSuperClass = "foo.bar.Cacheable"
// find the container class by name
var alClz = heap.findClass(elementSuperClass)
// retrieve all subclasses
var subClzs = alClz.subclasses()

// filter the list of objects reachables from instances of the container super class
// and all its subclasses so it contains only objects of classes from subClzs
map(heap.objects(containerSuperClass), 'filter(reachables(it), "it != null && contains(subClzs, containsClause(it))")')

// we need to externalize the contains clause because of clash in naming the closure parameter 'it'
function containsClause(rcbl) {
    return function(it) {
        if (rcbl == null || it == null) return false;
        return it.name.equals(classof(rcbl).name)
    }
}
like image 81
JB- Avatar answered Oct 21 '22 05:10

JB-