In depth view and analysis of Java memory

  • 2020-04-01 02:04:01
  • OfStack

1: gc log output
Into the JVM startup parameters - XX: + PrintGC - XX: + PrintGCDetails - XX: XX: + PrintGCTimestamps - + PrintGCApplicationStopedTime, the JVM will be output in order of these parameters gc profile, details, gc time information, application of gc pause times. If you add the -xloggc: file path after the previous parameter, the gc information will be output to the specified file. Other parameters are
Verbose: gc and - XX: + PrintTenuringDistribution, etc.
2: jconsole
Jconsole is a memory analysis tool that comes with the JDK and provides a graphical interface. You can see the memory information of the monitored JVM, thread information, class loading information, and MBean information.
Jconsole is located in the bin directory under the JDK directory, jconsole.exe under Windows, and jconsole.sh under Unix and Linux. Jconsole can monitor both local and remote applications. To monitor the local application, execute jconsole pid, pid is the running Java process id, if you do not take pid parameters, then after executing the jconsole command, you will see a dialog box pops up, listed above the local Java process, you can choose one to monitor. If you want to monitor remotely, add something to the JVM parameters of the remote server, because jconsole's remote monitoring is based on JMX. See the article devoted to jconsle for details on the use of jconsole, and I'll blog about jconsole specifically.
3: jviusalvm
After JDK6 update 7, the JDK introduced another tool: jvisualvm, the Java visual virtual machine, which not only provides similar functions of jconsole, but also provides real-time diagnosis of JVM memory and CPU, as well as manual dumping of JVM memory and performing gc.
As with jconsole, run jviusalvm, executing jviusalvm in the bin directory of the JDK, jviusalvm under Windows, jviusalvm.exe, and jviusalvm.sh under Linux and Unix.
4: jmap
Jmap is the JVM memory analysis tool that comes with the JDK and is located in the JDK bin directory. Usage of jmap command in jdk1.6:

Usage:
jmap -histo <pid>
 ( to connect to running process and print histogram of java object heap
jmap -dump:<dump-options> <pid>
 ( to connect to running process and dump java heap ) 
dump-options:
format=b binary default
file=<file>  dump heap to <file>
Example:   jmap -dump:format=b,file=heap.bin <pid>
jmap -histo <pid> Displays the specification on the screen pid the jvm Memory condition. Take my machine as an example, execute the command, the screen shows: 
num #instances #bytes  class name
----------------------------------------------
1: 242062791864  <constMethodKlass>
2: 223712145216  [C
3: 242061940648  <methodKlass>
4:  19511364496  <constantPoolKlass>
5: 265431282560  <symbolKlass>
6:  63771081744  [B
7:  1793 909688  <constantPoolCacheKlass>
8:  1471 614624  <instanceKlassKlass>
9: 14581 548336  [Ljava.lang.Object;
10:  3863 513640  [I
11: 20677 496248  java.lang.String
12:  3621 312776  [Ljava.util.HashMap$Entry;
13:  3335 266800  java.lang.reflect.Method
14:  8256 264192  java.io.ObjectStreamClass$WeakClassKey
15:  7066 226112  java.util.TreeMap$Entry
16:  2355 173304  [S
17:  1687 161952  java.lang.Class
18:  2769 150112  [[I
19:  3563 142520  java.util.HashMap
20:  5562 133488  java.util.HashMap$Entry
Total239019   17140408

I've deleted some lines for easy viewing. It is easy to see from the above information that #instance refers to the number of objects, #bytes refers to the amount of memory these objects occupy, and class name refers to the type of object.
Take a look at jmap's dump option, which outputs memory information from the JVM's heap to a file and executes it on my machine
Jmap - dump: the file = c: dump. TXT, 340
Note that 340 is the pid of my native Java process, and the dumped files are a few tens of M large. Besides, I just started tomcat and ran a very simple application without any access, so I can imagine how large the dumped files should be on a large and busy server. What you need to know is that the dump file information is too primitive to be directly viewable by a human, and jmap-histo displays too simple content, such as only showing how much memory certain types of objects take up and how many objects there are, but no more detailed information, such as who each object was created by. So what's the point of dump? Of course it is, because there are tools for analyzing the JVM's memory dump files.
5: the jhat
As mentioned above, there are many tools that can analyze the memory dump files of the JVM. Jhat is the tool that comes with sun jdk6 and above. It is located in the bin directory of the JDK and executes jhat-j-xmx512m [file]. Jhat has a simple web server built into it. After this command is executed, jhat displays the access address of the analysis results in the command line. You can specify the port with the -port option. After accessing the specified address, you can see the information displayed on the page, which is much richer and more detailed than the jmap-histo command.
6: eclipse memory analyzer
As mentioned above, jhat can analyze the dump files of the JVM, but it is all text display. Eclipse memory analyzer is a plug-in provided by eclipse to analyze the dump of the JVM heap. Its analysis speed is faster than that of jhat. In fact, jvisualvm can also analyze the dump file, also has a graphical interface.
7: jstat
If jmap tends to analyze object information in JVM memory, then jsta tends to analyze gc in JVM memory. Both are JVM memory analysis tools, but obviously they are analyzed from different dimensions. There are many commonly used jsat parameters, such as -gc,-gcutil,-gccause, and these options can be used to see the jsat help information, I often use -gcutil, this parameter's function continuously displays the current specified JVM memory garbage collection information.
I executed jstat -gcutil 340 10000 on the machine. This command outputs the gc information for the JVM every 10 seconds. The following information is displayed on the screen (I only took the first line, because it is displayed at a certain frequency, so there will be many lines when actually executed) :

S0 S1 E  O  P YGC YGCTFGCFGCT GCT
54.62   0.00  42.87  43.52  86.24   17925.093337.670   12.763

The forehead... To understand what this information means, you must also have some understanding of the JVM's gc mechanism. In fact, if you know more about the gc of sun's hot spot JVM, it should be easy for you to understand this information, but if you don't know the gc mechanism, it's a little confusing, so let me talk about the gc mechanism of sun's JVM first. Said to gc, in fact, not only the concept of Java, actually before Java, there are many languages have the concept of gc, gc is the meaning of garbage collection, is more of an algorithm, and with the specific language doesn't matter too much, so about the history of gc, gc is the mainstream algorithm of I don't speak, that pulled too far, too far is bullshit. With sun's current JVM, the memory management model is a generational model, so of course gc is a generational collection. What does generational mean? It is to divide the objects into three levels according to the life cycle, namely: Cenozoic era, old generation and lasting generation. Object, distribution of first time, most of them in Cenozoic, when the new generation gc after submit is triggered, perform within the scope of a new generation of gc, it's called a minor gc, if carried out, after a few minor gc and live objects, these objects into the old generation, because these objects have already been the organization of the test the wow. Old students generation gc frequency will be more lower, if old gave birth to a generation of execution of gc, it is full of gc, because not local gc, but all the memory range of gc, which can cause application pause, because the whole collection, memory must be sealed memory, are not allowed to have new objects allocated memory, persistent generation is some during the JVM, the object of the basic won't disappear, such as the definition of the class, the JVM method area information, such as the static block. The main thing is that the new generation is divided into three Spaces: Eden, susvivor0, and susvivor1, which literally means Eden park, surviving zone 1, surviving zone 2. New objects are allocated in the Eden area. When the Eden area is full, the mark-copy algorithm is used to check the objects that exist in the Eden area and copy them into s0 or s1, and then clear the Eden area. The gc for the JVM is more than that, there are serial collections, parallel collections, concurrent collections, and the famous train algorithm, but that goes too far, and it's good to know a little bit about it now. With that said, take a look at the output above:

S0   S1   EO  P   YGC YGCTFGCFGCT GCT
54.62   0.00  42.87  43.52  86.24   17925.093337.670   12.763
S0: The new generation of susvivor0 The space utilization rate is 5462%
S1: The new generation of susvivor1 The space utilization rate is 0.00% Because it hasn't been executed a second time minor Collection) 
E:eden Area, space usage 42.87%
O: Old generation, space usage 43.52%
P: Persistent band, space usage 86.24%
YGC:minor gc Number of executions 1792 time 
YGCT:minor gc Time spent 5.093 ms 
FGC:full gc Number of executions 33
FGCT:full gc Time spent 7.670 ms 
GCT:gc Total time spent 12.763 ms 

How to select tools
Some of the tools listed above have their pros and cons, but it doesn't matter what kind of tools you use in your development environment, as long as you get results. In a production environment, however, you can't just pick and choose, because the tools themselves consume a lot of system resources, and it can be surprising if you rush to execute them when a production server is stressed. Had better not on the server machine monitoring, remote monitoring will be better, but if you want to remote monitoring, the server startup scripts to join some JVM parameters, such as using jconsloe remote monitoring tomcat or jboss and so on, all you need to set up the JVM JMX parameters, if only server memory allocation and gc analysis information, it is highly recommended, first jmap is used to derive the JVM heap dump files on the server, and then use the jhat, or jvisualvm, or the eclipse memory analyzer to analyze memory condition.

Related articles: