0N/A<!--
2362N/ACopyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.
0N/ADO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A
0N/AThis code is free software; you can redistribute it and/or modify it
0N/Aunder the terms of the GNU General Public License version 2 only, as
2362N/Apublished by the Free Software Foundation. Oracle designates this
0N/Aparticular file as subject to the "Classpath" exception as provided
2362N/Aby Oracle in the LICENSE file that accompanied this code.
0N/A
0N/AThis code is distributed in the hope that it will be useful, but WITHOUT
0N/AANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/AFITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/Aversion 2 for more details (a copy is included in the LICENSE file that
0N/Aaccompanied this code).
0N/A
0N/AYou should have received a copy of the GNU General Public License version
0N/A2 along with this work; if not, write to the Free Software Foundation,
0N/AInc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A
2365N/APlease contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2365N/Aor visit www.oracle.com if you need additional information or have any
2365N/Aquestions.
0N/A-->
0N/A
0N/A<html>
0N/A<head>
0N/A<style>
0N/A.key { color: red; font-weight: bold }
0N/A</style>
0N/A<title>
0N/AObject Query Language (OQL)
0N/A</title>
0N/A</head>
0N/A<body>
0N/A<h1>Object Query Language (OQL)</h1>
0N/A
0N/A<p>
0N/AOQL is SQL-like query language to query Java heap. OQL allows to filter/select information
0N/Awanted from Java heap. While pre-defined queries such as "show all instances of class X"
0N/Aare already supported by HAT, OQL adds more flexibility. OQL is based on JavaScript expression
0N/Alanguage.
0N/A</p>
0N/A
0N/A<p>
0N/AOQL query is of the form
0N/A
0N/A<pre>
0N/A<code>
0N/A <span class="key">select</span> &lt;JavaScript expression to select&gt;
0N/A [ <span class="key">from</span> [<span class="key">instanceof</span>] &lt;class name&gt; &lt;identifier&gt;
0N/A [ <span class="key">where</span> &lt;JavaScript boolean expression to filter&gt; ] ]
0N/A</code>
0N/A</pre>
0N/Awhere class name is fully qualified Java class name (example: java.net.URL) or array class name.
0N/A[C is char array name, [Ljava.io.File; is name of java.io.File[] and so on.
0N/ANote that fully qualified class name does not always uniquely identify a
0N/AJava class at runtime. There may be more than one Java class with the same
0N/Aname but loaded by different loaders. So, class name is permitted to be
0N/Aid string of the class object.
0N/A
0N/AIf <span class="key">instanceof</span> keyword is used, subtype objects are selected. If this
0N/Akeyword is not specified, only the instances of exact class specified are selected. Both
0N/A<span class="key">from</span> and <span class="key">where</span> clauses are optional.
0N/A</p>
0N/A
0N/A
0N/A<p>
0N/AIn <span class="key">select</span> and (optional) <span class="key">where</span> clauses, the expression
0N/Aused in JavaScript expression. Java heap objects are wrapped as convenient script objects so that
0N/Afields may be accessed in natural syntax. For example, Java fields can be accessed with obj.field_name
0N/Asyntax and array elements can be accessed with array[index] syntax. Each Java object selected is
0N/Abound to a JavaScript variable of the identifier name specified in <span class="key">from</span> clause.
0N/A</p>
0N/A
0N/A<h2>OQL Examples</h2>
0N/A
0N/A<ul>
0N/A<li>select all Strings of length 100 or more
0N/A<pre>
0N/A<code>
0N/A select s from java.lang.String s where s.count >= 100
0N/A</code>
0N/A</pre>
0N/A<li>select all int arrays of length 256 or more
0N/A<pre>
0N/A<code>
0N/A select a from [I a where a.length >= 256
0N/A</code>
0N/A</pre>
0N/A<li>show content of Strings that match a regular expression
0N/A<pre>
0N/A<code>
0N/A select s.value.toString() from java.lang.String s
0N/A where /java/(s.value.toString())
0N/A</code>
0N/A</pre>
0N/A<li>show path value of all File objects
0N/A<pre>
0N/A<code</b>
0N/A select file.path.value.toString() from java.io.File file
0N/A</code>
0N/A</pre>
0N/A<li>show names of all ClassLoader classes
0N/A<pre>
0N/A<code>
0N/A select <a href="#classof">classof</a>(cl).name
0N/A from instanceof java.lang.ClassLoader cl
0N/A</code>
0N/A</pre>
0N/A<li>show instances of the Class identified by given id string
0N/A<pre>
0N/A<code>
0N/A select o from instanceof 0xd404b198 o
0N/A</code>
0N/A</pre>
0N/ANote that 0xd404b198 is id of a Class (in a session). This is found by
0N/Alooking at the id shown in that class's page.
0N/A</ul>
0N/A
0N/A<h2>OQL built-in objects, functions</h2>
0N/A
0N/A<h3>heap object</h3>
0N/A
0N/AThe <b>heap</b> built-in object supports the following methods:
0N/A
0N/A<ul>
0N/A<li><b>heap.forEachClass</b> -- calls a callback function for each Java Class
0N/A<pre>
0N/A<code>
0N/A heap.forEachClass(callback);
0N/A</code>
0N/A</pre>
0N/A<li><b>heap.forEachObject</b> -- calls a callback function for each Java object
0N/A<pre>
0N/A<code>
0N/A heap.forEachObject(callback, clazz, includeSubtypes);
0N/A</code>
0N/A</pre>
0N/A<code>clazz</code> is the class whose instances are selected. If not specified, defaults to java.lang.Object. <code>includeSubtypes</code> is a boolean flag
0N/Athat specifies whether to include subtype instances or not. Default value of
0N/Athis flag is true.
0N/A<a name="findClass"></a>
0N/A<li><b>heap.findClass</b> -- finds Java Class of given name
0N/A<pre>
0N/A<code>
0N/A heap.findClass(className);
0N/A</code>
0N/A</pre>
0N/Awhere <code>className</code> is name of the class to find. The resulting Class
0N/Aobject has following properties:
0N/A<ul>
0N/A<li>name - name of the class.
0N/A<li>superclass - Class object for super class (or null if java.lang.Object).
0N/A<li>statics - name, value pairs for static fields of the Class.
0N/A<li>fields - array of field objects. field object has name, signature
0N/Aproperties.
0N/A<li>loader - ClassLoader object that loaded this class.
0N/A<li>signers - signers that signed this class.
0N/A<li>protectionDomain - protection domain to which this class belongs.
0N/A</ul>
0N/AClass objects have the following methods:
0N/A<ul>
0N/A<li>isSubclassOf - tests whether given class is direct or indirect
0N/Asubclass of this class or not.
0N/A<li>isSuperclassOf - tests whether given Class is direct or indirect
0N/Asuperclass of this class or not.
0N/A<li>subclasses - returns array of direct and indirect subclasses.
0N/A<li>superclasses - returns array of direct and indirect superclasses.
0N/A</ul>
0N/A<a name="findObject"></a>
0N/A<li><b>heap.findObject</b> -- finds object from given object id
0N/A<pre>
0N/A<code>
0N/A heap.findObject(stringIdOfObject);
0N/A</code>
0N/A</pre>
0N/A<a name="classes"></a>
0N/A<li><b>heap.classes</b> -- returns an enumeration of all Java classes
0N/A<a name="objects"></a>
0N/A<li><b>heap.objects</b> -- returns an enumeration of Java objects
0N/A<pre>
0N/A<code>
0N/A heap.objects(clazz, [includeSubtypes], [filter])
0N/A</code>
0N/A</pre>
0N/A<code>clazz</code> is the class whose instances are selected. If not specified, defaults to java.lang.Object. <code>includeSubtypes</code> is a boolean flag
0N/Athat specifies whether to include subtype instances or not. Default value of
0N/Athis flag is true. This method accepts an optional filter expression to filter
0N/Athe result set of objects.
0N/A<a name="finalizables"></a>
0N/A<li><b>heap.finalizables</b> -- returns an enumeration of Java objects that are
0N/Apending to be finalized.
0N/A<li><b>heap.livepaths</b> -- return an array of paths by which a given object
0N/Ais alive. This method accepts optional second parameter that is a boolean
0N/Aflag. This flag tells whether to include paths with weak reference(s) or not.
0N/ABy default, paths with weak reference(s) are not included.
0N/A<pre>
0N/A<code>
0N/A select heap.livepaths(s) from java.lang.String s
0N/A</code>
0N/A</pre>
0N/AEach element of this array itself is another array. The later array is
0N/Acontains an objects that are in the 'reference chain' of the path.
0N/A<li><b>heap.roots</b> -- returns an Enumeration of Roots of the heap.
0N/A<a name="rootobj"></a>
0N/AEach Root object has the following properties:
0N/A<ul>
0N/A<li>id - String id of the object that is referred by this root
0N/A<li>type - descriptive type of Root (JNI Global, JNI Local, Java Static etc)
0N/A<li>description - String description of the Root
0N/A<li>referrer - Thread Object or Class object that is responsible for this root or null
0N/A</ul>
0N/A</ul>
0N/A
0N/AExamples:
0N/A<ul>
0N/A<li>access static field 'props' of class java.lang.System
0N/A<pre>
0N/A<code>
0N/A select heap.findClass("java.lang.System").statics.props
0N/A select heap.findClass("java.lang.System").props
0N/A</code>
0N/A</pre>
0N/A<li>get number of fields of java.lang.String class
0N/A<pre>
0N/A<code>
0N/A select heap.findClass("java.lang.String").fields.length
0N/A</code>
0N/A</pre>
0N/A<li> find the object whose object id is given
0N/A<pre>
0N/A<code>
0N/A select heap.findObject("0xf3800b58")
0N/A</code>
0N/A</pre>
0N/A<li>select all classes that have name pattern java.net.*
0N/A<pre>
0N/A<code>
0N/A select <a href="#filter">filter</a>(heap.classes(), "/java.net./(it.name)")
0N/A</code>
0N/A</pre>
0N/A</ul>
0N/A
0N/A<h3>functions on individual objects</h3>
0N/A
0N/A<ul>
0N/A<li><a href="#allocTrace">allocTrace(jobject)</a>
0N/A<li><a href="#classof">classof(jobject)</a>
0N/A<li><a href="#forEachReferrer">forEachReferrer(callback, jobject)</a>
0N/A<li><a href="#identical">identical(o1, o2)</a>
0N/A<li><a href="#objectid">objectid(jobject)</a>
0N/A<li><a href="#reachables">reachables(jobject, excludedFields)</a>
0N/A<li><a href="#referrers">referrers(jobject)</a>
0N/A<li><a href="#referees">referees(jobject)</a>
0N/A<li><a href="#refers">refers(jobject)</a>
0N/A<li><a href="#root">root(jobject)</a>
0N/A<li><a href="#sizeof">sizeof(jobject)</a>
0N/A<li><a href="#toHtml">toHtml(obj)</a>
0N/A</ul>
0N/A
0N/A<a name="allocTrace"></a>
0N/A<h4>allocTrace function</h4>
0N/A
0N/AThis returns allocation site trace of a given Java object if available.
0N/AallocTrace returns array of frame objects. Each frame object has the following
0N/Aproperties:
0N/A<ul>
0N/A<li>className - name of the Java class whose method is running in the frame.
0N/A<li>methodName - name of the Java method running in the frame.
0N/A<li>methodSignature - signature of the Java method running in the frame.
0N/A<li>sourceFileName - name of source file of the Java class running in the frame.
0N/A<li>lineNumber - source line number within the method.
0N/A</ul>
0N/A
0N/A<a name="classof"></a>
0N/A<h4>classof function</h4>
0N/A
0N/AReturns Class object of a given Java Object. The result object supports the
0N/Afollowing properties:
0N/A<ul>
0N/A<li>name - name of the class.
0N/A<li>superclass - Class object for super class (or null if java.lang.Object).
0N/A<li>statics - name, value pairs for static fields of the Class.
0N/A<li>fields - array of field objects. Field objects have name, signature
0N/Aproperties.
0N/A<li>loader - ClassLoader object that loaded this class.
0N/A<li>signers - signers that signed this class.
0N/A<li>protectionDomain - protection domain to which this class belongs.
0N/A</ul>
0N/AClass objects have the following methods:
0N/A<ul>
0N/A<li>isSubclassOf - tests whether given class is direct or indirect
0N/Asubclass of this class or not.
0N/A<li>isSuperclassOf - tests whether given Class is direct or indirect
0N/Asuperclass of this class or not.
0N/A<li>subclasses - returns array of direct and indirect subclasses.
0N/A<li>superclasses - returns array of direct and indirect superclasses.
0N/A</ul>
0N/A
0N/AExamples:
0N/A<ul>
0N/A<li>show class name of each Reference type object
0N/A<pre>
0N/A<code>
0N/A select classof(o).name from instanceof java.lang.ref.Reference o
0N/A</code>
0N/A<li>show all subclasses of java.io.InputStream
0N/A<pre>
0N/A<code>
0N/A select heap.findClass("java.io.InputStream").subclasses()
0N/A</code>
0N/A<li>show all superclasses of java.io.BufferedInputStream
0N/A<pre>
0N/A<code>
0N/A select heap.findClass("java.io.BufferedInputStream").superclasses()
0N/A</code>
0N/A</pre>
0N/A</ul>
0N/A
0N/A<a name="forEachReferrer"></a>
0N/A<h4>forEachReferrer function</h4>
0N/A
0N/Acalls a callback function for each referrer of a given Java object.
0N/A
0N/A<a name="identical"></a>
0N/A<h4>identical function</h4>
0N/A<p>
0N/AReturns whether two given Java objects are identical or not.
0N/A</p>
0N/AExample:
0N/A<pre>
0N/A<code>
0N/A select identical(heap.findClass("Foo").statics.bar, heap.findClass("AnotherClass").statics.bar)
0N/A</code>
0N/A</pre>
0N/A
0N/A<a name="objectid"></a>
0N/A<h4>objectid function</h4>
0N/A
0N/A<p>
0N/AReturns String id of a given Java object. This id can be passed to
0N/A<a href="#findObject">heap.findObject</a> and may also be used to compare
0N/Aobjects for identity.
0N/A</p>
0N/AExample:
0N/A<pre>
0N/A<code>
0N/A select objectid(o) from java.lang.Object o
0N/A</code>
0N/A</pre>
0N/A
0N/A<a name="reachables"></a>
0N/A<h4>reachables function</h4>
0N/A<p>
0N/AReturns an array of Java objects that are transitively referred from the
0N/Agiven Java object. Optionally accepts a second parameter that is comma
0N/Aseparated field names to be excluded from reachability computation.
0N/AFields are written in class_name.field_name pattern.
0N/A</p>
0N/AExamples:
0N/A<ul>
0N/A<li>print all reachable objects from each Properties instance.
0N/A<pre>
0N/A<code>
0N/A select reachables(p) from java.util.Properties p
0N/A</code>
0N/A</pre>
0N/A<li>print all reachables from each java.net.URL but omit the objects reachable
0N/Avia the fields specified.
0N/A<pre>
0N/A<code>
0N/A select reachables(u, 'java.net.URL.handler') from java.net.URL u
0N/A</code>
0N/A</pre>
0N/A</ul>
0N/A
0N/A<a name="referrers"></a>
0N/A<h4>referrers function</h4>
0N/A<p>
0N/AReturns an enumeration of Java objects that hold reference to a given Java
0N/Aobject.
0N/A</p>
0N/AExamples:
0N/A<ul>
0N/A<li> print number of referrers for each java.lang.Object instance
0N/A<pre>
0N/A<code>
0N/A select count(referrers(o)) from java.lang.Object o
0N/A</code>
0N/A</pre>
0N/A<li>print referrers for each java.io.File object
0N/A<pre>
0N/A<code>
0N/A select referrers(f) from java.io.File f
0N/A</code>
0N/A</pre>
0N/A<li>print URL objects only if referred by 2 or more
0N/A<pre>
0N/A<code>
0N/A select u from java.net.URL u where count(referrers(u)) > 2
0N/A</code>
0N/A</pre>
0N/A</ul>
0N/A
0N/A<a name="referees"></a>
0N/A<h4>referees function</h4>
0N/A<p>
0N/AReturns an array of Java objects to which the given Java
0N/Aobject directly refers to.
0N/A</p>
0N/AExample: to print all static reference fields of java.io.File class
0N/A<pre>
0N/A<code>
0N/A select referees(<a href="#findClass">heap.findClass</a>("java.io.File"))
0N/A</code>
0N/A</pre>
0N/A
0N/A<a name="refers"></a>
0N/A<h4>refers function</h4>
0N/A<p>
0N/AReturns whether first Java object refers to second Java object or not.
0N/A</p>
0N/A
0N/A<a name="root"></a>
0N/A<h4>root function</h4>
0N/A<p>
0N/AIf given object is a member of root set of objects, this function returns
0N/Aa descriptive <a href="#rootobj">Root object</a> describing why it is so.
0N/AIf given object is not a root, then this function returns null.
0N/A</p>
0N/A
0N/A<a name="sizeof"></a>
0N/A<h4>sizeof function</h4>
0N/A
0N/AReturns size of the given Java object in bytes
0N/AExample:
0N/A<pre>
0N/A<code>
0N/A select sizeof(o) from [I o
0N/A</code>
0N/A</pre>
0N/A
0N/A<a name="toHtml"></a>
0N/A<h4>toHtml function</h4>
0N/A
0N/AReturns HTML string for the given Java object. Note that this is called
0N/Aautomatically for objects selected by select expression. But, it may be useful
0N/Ato print more complex output.
0N/A
0N/AExample: print hyperlink in bold font weight
0N/A<pre>
0N/A<code>
0N/A select "&lt;b&gt;" + toHtml(o) + "&lt;/b&gt;" from java.lang.Object o
0N/A</code>
0N/A</pre>
0N/A
0N/A<h3>Selecting multiple values</h3>
0N/A<p>
0N/AMultiple values can be selected using JavaScript object literals or arrays.
0N/A</p>
0N/A
0N/AExample: show name and thread for each thread object
0N/A<pre>
0N/A<code>
0N/A select { name: t.name? t.name.toString() : "null", thread: t }
0N/A from instanceof java.lang.Thread t
0N/A</code>
0N/A</pre>
0N/A
0N/A<h3>array/iterator/enumeration manipulation functions</h3>
0N/A
0N/A<p>
0N/AThese functions accept an array/iterator/enumeration and an
0N/Aexpression string [or a callback function] as input. These functions iterate
0N/Athe array/iterator/enumeration and apply the expression (or function) on
0N/Aeach element. Note that JavaScript objects are associative arrays. So,
0N/Athese functions may also be used with arbitrary JavaScript objects.
0N/A</p>
0N/A
0N/A<ul>
0N/A<li><a href="#concat">concat(array1/enumeration1, array2/enumeration2)</a>
0N/A<li><a href="#contains">contains(array/enumeration, expression)</a>
0N/A<li><a href="#count">count(array/enumeration, expression)</a>
0N/A<li><a href="#filter">filter(array/enumeration, expression)</a>
0N/A<li><a href="#length">length(array/enumeration)</a>
0N/A<li><a href="#map">map(array/enumeration, expression)</a>
0N/A<li><a href="#max">max(array/enumeration, [expression])</a>
0N/A<li><a href="#min">min(array/enumeration, [expression])</a>
0N/A<li><a href="#sort">sort(array/enumeration, [expression])</a>
0N/A<li><a href="#sum">sum(array/enumeration, [expression])</a>
0N/A<li><a href="#toArray">toArray(array/enumeration)</a>
0N/A<li><a href="#unique">unique(array/enumeration, [expression])</a>
0N/A</ul>
0N/A
0N/A<a name="concat"></a>
0N/A<h4>concat function</h4>
0N/A<p>
0N/AConcatenates two arrays or enumerations (i.e., returns composite
0N/Aenumeration).
0N/A</p>
0N/A
0N/A<a name="contains"></a>
0N/A<h4>contains function</h4>
0N/A<p>
0N/AReturns whether the given array/enumeration contains an element
0N/Athe given boolean expression specified in code. The code evaluated
0N/Acan refer to the following built-in variables.
0N/A</p>
0N/A<ul>
0N/A<li>it -> currently visited element
0N/A<li>index -> index of the current element
0N/A<li>array -> array/enumeration that is being iterated
0N/A</ul>
0N/AExample: select all Properties objects that are referred by
0N/Asome static field some class.
0N/A<pre>
0N/A<code>
0N/A select p from java.util.Properties p
0N/A where contains(<a href="#referrers">referrers</a>(p), "<a href="#classof">classof</a>(it).name == 'java.lang.Class'")
0N/A</code>
0N/A</pre>
0N/A
0N/A<a name="count"></a>
0N/A<h4>count function</h4>
0N/A<p>
0N/Acount function returns the count of elements of the input array/enumeration
0N/Athat satisfy the given boolean expression. The boolean expression code can
0N/Arefer to the following built-in variables.
0N/A</p>
0N/A<ul>
0N/A<li>it -> currently visited element
0N/A<li>index -> index of the current element
0N/A<li>array -> array/enumeration that is being iterated
0N/A</ul>
0N/AExample: print number of classes that have specific name pattern
0N/A<pre>
0N/A<code>
0N/A select count(<a href="#classes">heap.classes()</a>, "/java.io./(it.name)")
0N/A</code>
0N/A</pre>
0N/A
0N/A<a name="filter"></a>
0N/A<h4>filter function</h4>
0N/A<p>
0N/Afilter function returns an array/enumeration that contains elements
0N/Aof the input array/enumeration that satisfy the given boolean
0N/Aexpression. The boolean expression code can refer to the following built-in
0N/Avariables.
0N/A</p>
0N/A<ul>
0N/A<li>it -> currently visited element
0N/A<li>index -> index of the current element
0N/A<li>array -> array/enumeration that is being iterated
0N/A<li>result -> result array/enumeration
0N/A</ul>
0N/AExamples:
0N/A<ul>
0N/A<li>show all classes that have java.io.* name pattern
0N/A<pre>
0N/A<code>
0N/A select filter(<a href="#classes">heap.classes</a>(), "/java.io./(it.name)")
0N/A</code>
0N/A</pre>
0N/A<li> show all referrers of URL object where the referrer is not from
0N/Ajava.net package
0N/A<pre>
0N/A<code>
0N/A select filter(<a href="#referrers">referrers</a>(u), "! /java.net./(<a href="#classof">classof</a>(it).name)")
0N/A from java.net.URL u
0N/A</code>
0N/A</pre>
0N/A</ul>
0N/A
0N/A<a name="length"></a>
0N/A<h4>length function</h4>
0N/A<p>
0N/Alength function returns number of elements of an array/enumeration.
0N/A</p>
0N/A
0N/A<a name="map"></a>
0N/A<h4>map function</h4>
0N/A<p>
0N/ATransforms the given array/enumeration by evaluating given code
0N/Aon each element. The code evaluated can refer to the following built-in
0N/Avariables.
0N/A</p>
0N/A<ul>
0N/A<li>it -> currently visited element
0N/A<li>index -> index of the current element
0N/A<li>array -> array/enumeration that is being iterated
0N/A<li>result -> result array/enumeration
0N/A</ul>
0N/A<p>
0N/Amap function returns an array/enumeration of values created by repeatedly
0N/Acalling code on each element of input array/enumeration.
0N/A</p>
0N/AExample: show all static fields of java.io.File with name and value
0N/A<pre>
0N/A<code>
0N/A select map(<a href="#findClass">heap.findClass</a>("java.io.File").statics, "index + '=' + <a href="#toHtml">toHtml</a>(it)")
0N/A</code>
0N/A</pre>
0N/A
0N/A<a name="max"></a>
0N/A<h4>max function</h4>
0N/A<p>
0N/Areturns the maximum element of the given array/enumeration.
0N/AOptionally accepts code expression to compare elements of the array.
0N/ABy default numerical comparison is used. The comparison expression can
0N/Ause the following built-in variables:
0N/A</p>
0N/A<ul>
0N/A<li>lhs -> left side element for comparison
0N/A<li>rhs -> right side element for comparison
0N/A</ul>
0N/AExamples:
0N/A<ul>
0N/A<li>find the maximum length of any String instance
0N/A<pre>
0N/A<code>
0N/A select max(map(heap.objects('java.lang.String', false), 'it.count'))
0N/A</code>
0N/A</pre>
0N/A<li>find string instance that has the maximum length
0N/A<pre>
0N/A<code>
0N/A select max(heap.objects('java.lang.String'), 'lhs.count > rhs.count')
0N/A</code>
0N/A</pre>
0N/A</ul>
0N/A
0N/A<a name="min"></a>
0N/A<h4>min function</h4>
0N/A<p>
0N/Areturns the minimum element of the given array/enumeration. Optionally
0N/Aaccepts code expression to compare elements of the array. By default numerical
0N/Acomparison is used. The comparison expression can use the following built-in
0N/Avariables:
0N/A</p>
0N/A<ul>
0N/A<li>lhs -> left side element for comparison
0N/A<li>rhs -> right side element for comparison
0N/A</ul>
0N/AExamples:
0N/A<ul>
0N/A<li>find the minimum size of any Vector instance
0N/A<pre>
0N/A<code>
0N/A select min(map(heap.objects('java.util.Vector', false), 'it.elementData.length'))
0N/A</code>
0N/A</pre>
0N/A<li>find Vector instance that has the maximum length
0N/A<pre>
0N/A<code>
0N/A select min(heap.objects('java.util.Vector'), 'lhs.elementData.length < rhs.elementData.length')
0N/A</code>
0N/A</ul>
0N/A
0N/A<a name="sort"></a>
0N/A<h4>sort function</h4>
0N/A<p>
0N/Asorts given array/enumeration. Optionally accepts code expression to
0N/Acompare elements of the array. By default numerical comparison is used.
0N/AThe comparison expression can use the following built-in variables:
0N/A</p>
0N/A<ul>
0N/A<li>lhs -> left side element for comparison
0N/A<li>rhs -> right side element for comparison
0N/A</ul>
0N/AExamples:
0N/A<ul>
0N/A<li> print all char[] objects in the order of size.
0N/A<pre>
0N/A<code>
0N/A select sort(<a href="#objects">heap.objects</a>('[C'), '<a href="#sizeof">sizeof</a>(lhs) - sizeof(rhs)')
0N/A</code>
0N/A</pre>
0N/A<li> print all char[] objects in the order of size but print
0N/Asize as well.
0N/A<pre>
0N/A<code>
0N/A select <a href="#map">map</a>(sort(<a href="#objects">heap.objects</a>('[C'), '<a href="#sizeof">sizeof</a>(lhs) - sizeof(rhs)'), '{ size: sizeof(it), obj: it }')
0N/A</code>
0N/A</pre>
0N/A</ul>
0N/A
0N/A<a name="sum"></a>
0N/A<h4>sum function</h4>
0N/A<p>
0N/AThis function returns the sum of all the elements of the given input array or
0N/Aenumeration. Optionally, accepts an expression as second param. This is used
0N/Ato map the input elements before summing those.
0N/A</p>
0N/AExample: return sum of sizes of the reachable objects from each Properties object
0N/A<pre>
0N/A<code>
0N/A select sum(<a href="#map">map</a>(<a href="#reachables">reachables</a>(p), '<a href="#sizeof">sizeof</a>(it)'))
0N/A from java.util.Properties p
0N/A
0N/A // or omit the map as in ...
0N/A select sum(<a href="#reachables">reachables</a>(p), '<a href="#sizeof">sizeof</a>(it)')
0N/A from java.util.Properties p
0N/A</code>
0N/A</code>
0N/A</pre>
0N/A
0N/A<a name="toArray"></a>
0N/A<h4>toArray function</h4>
0N/A<p>
0N/AThis function returns an array that contains elements of the input
0N/Aarray/enumeration.
0N/A</p>
0N/A
0N/A<a name="unique"></a>
0N/A<h4>unique function</h4>
0N/A<p>
0N/AThis function returns an array/enumeration containing unique elements of the
0N/Agiven input array/enumeration
0N/A</p>
0N/AExample: select unique char[] instances referenced from Strings. Note that
0N/Amore than one String instance can share the same char[] for the content.
0N/A<pre>
0N/A<code>
0N/A // number of unique char[] instances referenced from any String
0N/A select count(unique(map(heap.objects('java.lang.String'), 'it.value')))
0N/A
0N/A // total number of Strings
0N/A select count(heap.objects('java.lang.String'))
0N/A</code>
0N/A</pre>
0N/A
0N/A<h3>More complex examples</h3>
0N/A
0N/A<h4>Print histogram of each class loader and number of classes loaded by it</h4>
0N/A
0N/A<pre>
0N/A<code>
0N/A select <a href="#map">map</a>(<a href="#sort">sort</a>(map(heap.objects('java.lang.ClassLoader'),
0N/A '{ loader: it, count: it.classes.elementCount }'), 'lhs.count < rhs.count'),
0N/A 'toHtml(it) + "&lt;br&gt;"')
0N/A</code>
0N/A</pre>
0N/A<p>
0N/AThe above query uses the fact that, <b>java.lang.ClassLoader</b> has a private
0N/Afield called <b>classes</b> of type <b>java.util.Vector</b> and Vector has a
0N/Aprivate field named <b>elementCount</b> that is number of elements in the
0N/Avector. We select multiple values (loader, count) using JavaScript object
0N/Aliteral and map function. We sort the result by count (i.e., number of classes
0N/Aloaded) using sort function with comparison expression.
0N/A</p>
0N/A
0N/A<h4>Show parent-child chain for each class loader instance</h4>
0N/A
0N/A<pre>
0N/A<code>
0N/A select <a href="#map">map</a>(heap.objects('java.lang.ClassLoader'),
0N/A function (it) {
0N/A var res = '';
0N/A while (it != null) {
0N/A res += toHtml(it) + "-&gt;";
0N/A it = it.parent;
0N/A }
0N/A res += "null";
0N/A return res + "&lt;br&gt;";
0N/A })
0N/A</code>
0N/A</pre>
0N/A<p>
0N/ANote that we use <b>parent</b> field of <b>java.lang.ClassLoader</b> class
0N/Aand walk until parent is null using the callback function to map call.
0N/A</p>
0N/A
0N/A<h4>Printing value of all System properties</h4>
0N/A
0N/A<pre>
0N/A<code>
0N/A select <a href="#map">map</a>(<a href="#filter">filter(<a href="#findClass">heap.findClass</a>('java.lang.System').props.table, 'it != null'),
0N/A function (it) {
0N/A var res = "";
0N/A while (it != null) {
0N/A res += it.key.value.toString() + '=' +
0N/A it.value.value.toString() + '&lt;br&gt;';
0N/A it = it.next;
0N/A }
0N/A return res;
0N/A });
0N/A</code>
0N/A</pre>
0N/A<p>
0N/AThe above query uses the following facts:
0N/A<ul>
0N/A<li>java.lang.System has static field by name 'props' of type java.util.Properties.
0N/A<li>java.util.Properties has field by 'table' of type java.util.Hashtable$Entry
0N/A(this field is inherited from java.util.Hashtable). This is the hashtable
0N/Abuckets array.
0N/A<li>java.util.Hashtable$Entry has 'key', 'value' and 'next' fields. Each
0N/Aentry points the next entry (or null) in the same hashtable bucket.
0N/A<li>java.lang.String class has 'value' field of type char[].
0N/A</ul>
0N/A<p>
0N/A<b>Note that this query (and many other queries) may not be stable - because
0N/Aprivate fields of Java platform classes may be modified/removed without any
0N/Anotification! (implementation detail)</b>. But, using such queries on user
0N/Aclasses may be safe - given that user has the control over the classes.
0N/A</p>
0N/A
0N/A</body>
0N/A</html>