JDK's built-in tool jhat and jdk's built-in jhat

Source: Internet
Author: User

JDK's built-in tool jhat and jdk's built-in jhat

Jhat is a lightweight tool that comes with JDK. Jhat is located in the bin directory of java and is used to analyze the java heap commands. It can display the objects in the heap in html format, including the number and size of objects, it also supports the object query language. jhat is generally used in combination with jmap.

1. Jstat Command Format

Jhat dumpFile

2. Procedure

Step 1: export the heap

Step 2: Analyze heap files

Step 3: view html

Sometimes the dump heap is very large, and an error of insufficient heap space will be reported at startup. You can use the following parameters:
Jhat-J-Xmx512m

 

3. Use JMAP source code to export the dump file

Package com. jdkTools;

 

Import java. util. ArrayList;

Import java. util. Random;

 

/**

* Simple applications have a large number of loops and objects to be created for testing the jmap that comes with JDK.

* Parameter:-Xms30m-Xmx60m

*

* @ Author fan fangming

*/

Public class EasyJmap {

Public byte [] placeHolder = newbyte [1*1024]; // placeHolder

 

Public static void main (String [] args) throws Exception {

While (true ){

Random random = newRandom ();

Int loops = random. nextInt (10000 );

EasyJmap jstat = newEasyJmap ();

System. out. println ("... building object:" + loops + "(items )");

Jstat. getLoop (loops); // multiple loops generate a large number of objects

Thread. sleep (10 );

}

}

 

Public void getLoop (int size ){

ArrayList <EasyJmap> list = new ArrayList <EasyJmap> ();

For (int I = 0; I <size; I ++ ){

EasyJmap jstat = newEasyJmap ();

List. add (jstat );

}

}

}

 

4. Export the dump file

C: \ Program Files \ Java \ jdk1.6.0 _ 25 \ bin> jmap-dump: format = B, file = d:/ffm83/jmaplog.txt 1668

Dumping heap to D: \ ffm83 \ jmaplog.txt...

Heap dump file created

5. Analyze heap files

C: \ ProgramFiles \ Java \ jdk1.6.0 _ 25 \ bin> jhat D:/ffm83/jmaplog.txt

Readingfrom D:/ffm83/jmaplog.txt...

Dumpfile created Tue Jan 20 16:47:38 CST 2015

Snapshotread, resolving...

Resolving10893 objects...

Chasingreferences, please ct 2 dots ..

Eliminatingduplicate references ..

Snapshotresolved.

StartedHTTP server on port 7000

Serveris ready.

6. view the HTML of the heap File

The default port is 7000, which can be accessed through http: // localhost: 7000 /.
For example:

Packagecom. jdkTools

Class com. jdkTools. EasyJmap [0x7d077f8]

OtherQueries
  • All classes including platform
  • Show all members of the rootset
  • Show instance counts for all classes (including platform)
  • Show instance counts for all classes (excluding platform)
  • Show heap histogram
  • Show finalizer summary
  • Execute Object Query Language (OQL) query

 

7. OQL in jhat (Object Query Language)

To filter or Query heap Objects Based on certain conditions, go to "• Execute Object query Language (OQL) Query" and Execute oql on the jhat html page to query objects that meet the conditions.

Basic OQL syntax
Select <javascript expression to select>
[From [instanceof] <class name> <identifier>]
[Where <javascript boolean expression to filter>]
8. OQL explanation

(1) class name is a fully qualified java class name, for example, java. lang. string, java. util. arrayList, [C is a char array, [Ljava. io. file is java. io. file []
(2) The fully qualified names of classes are insufficient to uniquely identify a class. Because different ClassLoader loads the same class, they are of different types in jvm.
(3) instanceof indicates that a subclass of a class is also queried. If instanceof is not specified, only the class specified by class name is precisely queried.
(4) The from and where clauses are optional.
(5) java field representation: obj. field_name; java array representation: array [index]

9. Common OQL examples

(1) query strings with a length greater than 100
Select s from java. lang. String s where s. count> 100

(2) query arrays with a length greater than 256
Select a from [I a where a. length> 256
(3) display a string that matches a regular expression
Select a. value. toString () from java. lang. String s where/java/(s. value. toString ())
(4) display the file paths of all file objects
Select file. path. value. toString () from java. io. File file
(5) display the class names of all ClassLoader
Select classof (cl). name from instanceof java. lang. ClassLoader cl
(6) query objects by referencing
Select o from instanceof 0xd404d404 o

OQL-built-in object -- heap
(1) heap. findClass (class name) -- locate the class
Select heap. findClass ("java. lang. String"). superclass
(2) heap. findObject (object id) -- locate the object
Select heap. findObject ("0xd404d404 ")
(3) heap. classes -- enumeration of all classes
Select heap. classes
(4) heap. objects -- enumeration of all objects
Select heap. objects ("java. lang. String ")
(5) heap. finalizables -- enumeration of java objects waiting for garbage collection
(6) heap. livepaths -- survival path of an object
Select heaplivepaths (s) from java. lang. String s
(7) heap. roots -- enumeration of heap root Sets

OQL-functions for Object Recognition
(1) classof (class name) -- returns the class Object of the java object
Select classof (cl). name from instanceof java. lang. ClassLoader cl
(2) identical (object1, object2) -- returns whether two objects are in the same instance.
Select identical (heap. findClass ("java. lang. String"). name, heap. findClass ("java. lang. String"). name)
(3) objectid (object) -- returns the object id.
Select objectid (s) from java. lang. String s
(4) reachables -- return the objects that can be reached from the object
Select reachables (p) from java. util. Properties p -- query the objects that can be reached from the Properties object
Select reachables (u, "java.net. URL. handler") from java.net. URL u -- queries accessible objects from URL objects, but does not include objects accessible from URL. handler
(5) referrers (object) -- returns the object that references an object.
Select referrers (s) from java. lang. String s where s. count> 100
(6) referees (object) -- returns the object referenced by an object.
Select referees (s) from java. lang. String s where s. count> 100
(7) refers (object1, object2) -- returns whether the first object references the second object.
Select refers (heap. findObject ("0xd4d4d4d4"), heap. findObject ("0xe4e4e4e4 "))
(8) root (object) -- returns whether the object is a member of the root set.
Select root (heap. findObject ("0xd4d4d4d4 "))
(9) sizeof (object) -- returns the object size.
Select sizeof (o) from [I o
(10) toHtml (object) -- returns the html format of the object
Select "<B>" + toHtml (o) + "</B>" fromjava. lang. Object o
(11) Select multiple values
Select {name: t. name? T. name. toString (): "null", thread: t} frominstanceof java. lang. Thread t

OQL-array, iterator, and other functions
(1) concat (enumeration1, enumeration2) -- connect arrays or enumerations
Select concat (referrers (p), referrers (p) from java. util. Properties p
(2) contains (array, expression) -- whether the elements in the array meet a certain expression
Select p from java. util. Properties where contains (referres (p), "classof (it). name = 'java. lang. class '")
Returns the java. util. Properties object referenced by java. lang. Class.
Built-in Variable
It-current iteration Element
Index -- index of the current iteration Element
Array -- the array to be iterated
(3) count (array, expression) -- number of elements that meet a certain condition
Select count (heap. classes (), "/java. io./(it. name )")
(4) filter (array, expression) -- filter elements that meet a certain condition
Select filter (heap. classes (), "/java. io./(it. name )")
(5) length (array) -- returns the length of the array.
Select length (heap. classes ())
(6) map (array, expression) -- converts and maps elements in the array Based on the expression
Select map (heap. classes (), "index + '-->' + toHtml (it )")
(7) max (array, expression) -- maximum value, min (array, expression)
Selectmax (heap. objects ("java. lang. String"), "lhs. count> rhs. count ")
Built-in Variable
Lhs -- left element
Rhs -- elements on the right
(8) sort (array, expression) -- sort
Select sort (heap. objects ('[C'), 'sizeof (lhs)-sizeof (rhs )')
(9) sum (array, expression) -- sum
Select sum (heap. objects ('[C'), 'sizeof (it )')
(10) toArray (array) -- returns an array
(11) unique (array) -- unique array

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.