Sundararajan's Weblog

  • Java
    January 24, 2007

jhat's JavaScript interface

Guest Author
In one of the recent e-mails, Frank Kieviet suggested an improvement for jhat tool in JDK 6. He said something like:

Sundar: another idea to extend jhat: when looking at a memory dump and looking at the routes to a particular classloader, there are sometimes hundreds of references. [...] jhat could print some histogram data so that it’s immediately apparent which object reference is suspect.

The above could be generalized to any Java object (not just a classloader object). "routes to a particular classloader" probably requires explanation. Every Java object is live because there are one or more paths or routes (or chain of references) from the GC root set [recall that the root set is the initial object references from which garbage collector starts and constructs transitive closure of all objects reachable from there - such transitively reachable objects are alive and hence not eligible for collection]. Frank wants a facility in jhat to view histogram of objects across the liveness paths of a given object.

Being lazy, I don't want to change jhat for this :-) [also users may not be willing to move to a different release of JDK just for this jhat change!] Fortunately, jhat has a query interface called OQL.
OQL is built on top of the JavaScript engine bundled in JDK 6. Actually, you can even run any JavaScript code (and not just select...from..where style OQL queries) in jhat's OQL window! If the string entered in OQL window does not start with the "select" keyword, it is treated as JavaScript code and eval'ed as is using the JavaScript script engine bundled in JDK 6.

OQL has number of built-in JavaScript functions and objects. There is one object called "heap" which has useful methods such as "livepaths", "findObject". "livepaths" methods returns an array of paths by which given Java object is alive. Each path is another array of objects [objects in that liveness path chain]. There is another method called "findObject" which returns a specific object given it's object identifier [note that jhat shows object ids in hex format - so the hex format is used by "findObject" as well]. Using these we can write something like this:

(function() {
var histo = new Object();
function(path) {
for (var i = 0; i < path.length; i++) {
var desc = toHtml(path[i]);
if (desc in histo) {
} else {
histo[desc] = 1;
var str = "<table>";
for (i in histo) {
str += "<tr>";
str += "<td>" + i + "</td><td>" + histo[i] + "</td></tr>";
str += "</table>";
return str;

In the above code, the object id '0x0f054b90' used as argument to heap.findObject method is an object of interest in a specific heap dump. This is an object whose liveness paths we are interested on [turns out to be a classloader object]. When I entered the above script in jhat's OQL window, I got a nice histogram in HTML table form!

Be the first to comment

Comments ( 0 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.