OQL query to search for all instances and sub-instances of this class mentioned in the session

I am trying to use jhat / OQL to track a memory leak in a Tomcat container. The question I want to ask is this:

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

I managed to come up with the following, but this does not show subclasses of foo.bar.Cacheable (which is important since this is an infact 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 undefined).

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

Can someone help me with what I am doing wrong to ask this question through OQL?

+4
source share
1 answer

In jag-based OQL implementations (jHat, VisualVM), you can use the fact that you are not limited by the SQL syntax, but you have the full javascript engine at your fingertips.

The following code snippet will 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) } } 
+3
source

Source: https://habr.com/ru/post/1307701/


All Articles