What's in my Java heap?

As I mentioned earlier, Mustang (Java SE 6) comes with jhat - Java Heap Analysis Tool. We have added more features to jhat as of build 53 (released on Sep, 22, 2005). jhat now comes with mechanism to query the heap. jhat supports OQL - Object Query Language -- a SQL-like language to query your Java heap! HAT always supported simple queries such as "show all instances of class X" (include subclasses or not), "show all referrers to a specific object", "show all objects reachable from a specific object" etc. But, these pre-defined queries (supported by hyperlinks) may always be sufficient to a specific problem in your hand. That is where OQL helps.

OQL query is of the form


select <expression>
[ from [instanceof] <class name> <identifier>
[ where <filter-expression> ] ]

where select, from, where, instanceof are keywords. expression, filter-expression are JavaScript expressions. class name is fully qualified Java class name (example: java.net.URL) or array class name. [C is char array name, [Ljava.io.File; is name of java.io.File[] and so on. If instanceof keyword is used, subtype objects are selected. If this keyword is not specified, only the instances of exact class specified are selected. Both from and where clauses are optional.

In select and (optional) where clauses, the expression used in JavaScript expression. Java heap objects are wrapped as convenient script objects so that fields may be accessed in natural syntax. For example, Java fields can be accessed with obj.field_name syntax and array elements can be accessed with array[index] syntax. Each Java object selected is bound to a JavaScript variable of the identifier name specified in from clause.

identifier is a name bound for each Java heap object that is matching from and where clause. Note that both from and where clauses are optional. Few simple examples, would help in understanding basic OQL.

OQL Examples

    select s from java.lang.String s
    where s.count > 100

will select and show all String instances that have length more than 100. Note that we use the 'count' field of java.lang.String class. If you treat your Java heap as "database", then "classes" are "tables", "objects" are "rows" and "fields" are "columns". How do you "schema" of the "database"? You can use "javap -p" option and get all fields of a specific class.

     select a from [I a where a.length >= 256

will show all int arrays that have 256 or more elements.

    select classof(cl).name 
    from instanceof java.lang.ClassLoader cl

show names of all ClassLoader classes. Note that classof is a OQL built-in function that returns Class object of a given Java object (similar to Object.getClass() method). We will see more OQL examples, built-in functions in future blogs. But, if you are in hurry and want to learn things as soon as possible, then download Mustang binary and run jhat! You may want to refer to Alan's blog on heap dumps to know more about how to get heap dump of your Java application ...
Comments:

This is so cool...

Posted by Wilfred Springer on September 29, 2005 at 11:24 AM IST #

Thanks Wifred!

Posted by A. Sundararajan on September 30, 2005 at 09:40 AM IST #

Very nice. If I want to display a socket's address, how do I do that? E.g. I can use "sock.impl.address.address" which returns an integer. I'd like to format that into a dotted a.b.c.d notation. How can I do that? I guess I could write a function literal for this, but can I add to the library of builtin functions? Thank's

Posted by Andrej G on February 03, 2006 at 08:28 PM IST #

we have program running in client place. some time it hangs and then suddenly it start working. it is not reproducible in our box. I am looking for tool through with i can dirctly look in to client java heap for perticular object or perticular thread object on live runtime see what the state of object.

Posted by vinayak on January 28, 2007 at 02:57 PM IST #

Hi Vinayak: Before suspecting any heap issues / GC delays, you may want to run the application with -verbose:gc to see whether the intermittent delays/"hangs" are due to GC. If so, you may want to tune GC parameters accordingly. If you are suspecting slow leaks (i.e., the hang happens after application running for quite some time), then you can try heap profilers. Either you can generate heapdump by jmap tool and analyze using JDK 6's jhat tool or try NetBeans profiler. Also, SIGQUIT output of the app when it is "hang" would also give clues (i.e., thread stack traces at that point)

Posted by A. Sundararajan on January 30, 2007 at 12:41 PM IST #

How is it possible to nail down the bottlenecks with this QL. What I am wondering is, Is there a way to figure out the top 10 CPU consuming methods with this QL? If so, anyone can use it for production tuning, which would be awesome.

Posted by Rajesh Balamohan on February 21, 2007 at 12:31 AM IST #

Hi Rajesh Balamohan: No. This query language operates on heap snapshot. You can't get CPU stat. etc. using this. You may want to look at jconsole [or management API for programmatic access]

Posted by A. Sundararajan on February 25, 2007 at 07:25 AM IST #

Post a Comment:
Comments are closed for this entry.
About

sundararajan

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today
Bookmarks
Links

No bookmarks in folder

Blogroll

No bookmarks in folder

News

No bookmarks in folder