Jmap with built-in JDK tools
Jmap is a lightweight tool that comes with JDK. Located in the bin directory of java, it can print out all 'objects' in the memory of a java Process (using pid) (such as those objects and their quantity ). It not only outputs tools for all objects in the memory, but also outputs heap in the VM in binary format as text. Use jmap-histo pid. You can also save it to the text. After a period of time, you can use the text comparison tool to compare the objects collected by GC. It can also be used with jhat (Java Heap Analysis Tool) to visually display the current memory in the form of images.
1. Jmap Command Format
Jmap [option] pid
2. Common Parameters
Note: Most jmap functions can only be run in Linux. In windows, only the-dump and-histo options are valid.
-Dump: [live,] format = B, file = <filename> use the hprof binary format to output the jvm heap content to the file =. the live sub-option is optional. If the live option is specified, only the live object is output to the file.
-Finalizerinfo: prints the information of the object awaiting recycling.
-Heap: displays the heap summary, GC algorithm, heap configuration, and wise heap usage.
-Histo [: live]: prints the number of instances of each class, memory usage, and full name information of the class. the prefix "*" is added at the beginning of the VM internal class name "*". if the live sub-parameter is added, only the number of live objects is counted.
-Permstat: prints information about the persistent layer of classload and jvm heap. contains the name, activity, address, parent classloader, and number of loaded classes for each classloader. in addition, the number of internal strings and the amount of memory occupied are printed.
-F force. Use the-dump or-histo parameter when the pid does not have a corresponding value. In this mode, the live subparameter is invalid.
-H |-help: print the auxiliary information
3. Source Code
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. Running Parameters
* Parameter:-Xms30m-Xmx60m. this parameter is used to start a jvm vm.
5. Running result
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
View-histo [: live] To print heap object statistics, including the number of objects and memory size.
C: \ Program Files \ Java \ jdk1.6.0 _ 25 \ bin> jmap-histo 1668
Num # instances # bytes class name
----------------------------------------------
1: 7561 7536976 [B
2: 4970 496896 <constMethodKlass>
3: 8147 441176 <symbolKlass>
4: 4970 400192 <methodKlass>
5: 1458 198624 [C
6: 320 188400 <constantPoolKlass>
7: 298 128672 <constantPoolCacheKlass>
8: 320 127832 <instanceKlassKlass>
9: 7174 114784 com. jdkTools. EasyJmap
10: 314 64696 [Ljava. lang. Object;
11: 380 41904 [I
12: 388 37248 java. lang. Class
13: 1485 35640 java. lang. String
...