Java Tool Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 5.11

Object Query Language (OQL)

This section describes what is OQL (Object Query Language). OQL statement syntax and examples are also provided.

OQL (Object Query Language): A SQL-like query language to query Java heap. OQL allows to filter/select information wanted from Java heap. While pre-defined queries such as "show all instances of class X" are already supported by HAT, OQL adds more flexibility. OQL is based on JavaScript expression language.

OQL query is of the form"

select <JavaScript expression to select>
   [ from [instanceof] <class name> <identifier>
   [ where <JavaScript boolean expression to filter> ] ]

where 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. Note that fully qualified class name does not always uniquely identify a Java class at runtime. There may be more than one Java class with the same name but loaded by different loaders. So, class name is permitted to be id string of the class object. 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.

OQL Examples:

select all Strings of length 100 or more:
    select s from java.lang.String s where s.count >= 100

select all int arrays of length 256 or more:
    select a from [I a where a.length >= 256

show content of Strings that match a regular expression:
    select s.value.toString() from java.lang.String s 
    where /java/(s.value.toString())

show path value of all File objects:
    select file.path.value.toString() from java.io.File file

show names of all ClassLoader classes:
    select classof(cl).name 
    from instanceof java.lang.ClassLoader cl

show instances of the Class identified by given id string:
    select o from instanceof 0xd404b198 o

    Note that 0xd404b198 is id of a Class (in a session). 
    This is found by looking at the id shown in that class's page.

See next section on how to run OQL statements on the "jhat" Web server.

Sections in This Chapter

JVM Troubleshooting Tools in JDK 1.5

'jinfo' - VM Option Value Checker

Changing HotSpot VM Option using 'jinfo'

'jstack' - Stack Tracer of JVM Threads

Java Thread Deadlock Demo Program

Detecting Java Thread Deadlocks with 'jstack'

'jmap' - JVM Heap Dump Tool

Printing Histogram of Java Object Heap

Generating Heap Dump File with 'jmap'

'jhat' - Java Heap Analysis Tool

Starting 'jhat' Web Server on a Heap Dump File

Listing Instance Counts of All Classes

Browsing Object Instance Values

Object Query Language (OQL)

Searching for Instances with OQL Statements

Dr. Herong Yang, updated in 2008
Object Query Language (OQL)