How do I troubleshoot the high Memory usage of Java processes

Find the Java process PID

JPS -l Run the JPS command to find the PID of the Java process

Locate large objects that occupy memory

Jmap – histo: live 12628 (note: if the output content is too much, just want to see the top 10, can add | head – 10)[C is a char[] [S is a short[] [I is an int[][B is a byte[] [[I is an int[][] []

Run the jmap command to dump the heap information file jmap -dump:live,format=b,file=/data/aaa.hprof 12628

Using the MAT tool

Use Mat tools to analyze the dump file Concrete Mat use tutorial reference www.cnblogs.com/zh94/p/1405…

Find the number of open handles and threads for a Java application

  • Ll/proc / {} pid/fd | wc -l to check the handle to open the number
  • Ll/proc / {pid} / task | wc -l to check the number of threads

Jmap looks at the configuration of heap memory

Jmap-heap PID can see output similar to the following:

Parallel GC with 4 threads (s) // Current GC mode (Parallel GC)

Heap Configuration: MinHeapFreeRatio = 0 MinHeapFreeRatio = 100 MinHeapFreeRatio = 0 MinHeapFreeRatio = 0 MaxHeapFreeRatio: MaxHeapSize = 8388608 (8.0MB) // The JVM startup parameter -xx :MaxHeapSize= sets the maximum size of the JVM heap (or the -xmx parameter) NewSize= 5242880 (5.0MB) // The JVM startup parameter -xx :NewSize= sets the default of the ‘new generation’ of the JVM heap MaxNewSize= 5242880 (5.0MB) // The corresponding JVM startup parameter -xx :MaxNewSize= set the maximum size of the JVM heap ‘new generation’ OldSize = 3145728 (3.0MB) // The corresponding JVM startup parameter -xx :OldSize= sets the size of the old generation of the JVM heap NewRatio= 2 // The corresponding JVM startup parameter -xx :NewRatio=: size ratio of the new generation to the old generation of the HEAP SurvivorRatio = 8 // Corresponding JVM startup parameter -xx :SurvivorRatio= Set the size ratio of Eden zone to Survivor zone in the young generation MetaspaceSize = 21807104 (20.796875MB) CompressedClassSpaceSize = 1073741824 (1024.0MB) MaxMetaspaceSize = 17592186044415 MB G1HeapRegionSize = 0 (0.0MB)

Heap Usage: // Heap Usage PS Young Generation Eden Space: //Eden area Distribution Capacity = 2621440 (2.5MB) // Total Eden area capacity Used = 2328088 (2.2202377319335938MB) //Eden area free = 293352 (0.27976226806640625MB) // Remaining capacity in Eden 88.80950927734375% Capacity = 1572864 (1.5MB) Used = 360448 (0.34375MB) Free = 1212416 (1.15625MB) 22.91666666666666668% used To Space: Capacity = 1048576 (1.0MB) Used = 0 (0.0MB) Free = 1048576 (1.0MB) 0.0% used PS Old Generation Capacity = 3145728 (3.0MB) Used = 1458968 (1.3913803100585938MB) Free = 1686760 (1.6086196899414062MB) 46.37934366861979%, informs

3759 interned Strings occupying 298824 bytes.

Note: Lines 5-16 are the main configuration of heap memory. These parameters can be resized using Java -xx: parameter name = parameter value, for example:

java -XX:MinHeapFreeRatio=20 -XX:MaxHeapFreeRatio=80 -Xmx100m -XX:MetaspaceSize=50M -XX:NewRatio=3 The values of MinHeapFreeRatio, MaxHeapFreeRatio, MaxHeapSize, MetaspaceSize, and NewRatio are affectedNote NewRatio, this value refers to the ratio of Old Generation to Young Generation. Set it to 3, so OldSize is 75m and NewSize is 25m. Please refer to the following figure:

In JDk8, Permanent Generation has been removed and Metaspace has been added.

The Young Generation can be subdivided into Eden, S0 and S1.

The memory changes for ava7 and java8 are roughly as shown in the figure above.

SurvirorRatio this must be a little difficult to calculate, according to the explanation of Oracle’s official website: docs.oracle.com/cd/E19159-0… , the default value is 8, that is, the size of each survivor: Eden block is 1:8, in other words, s0 = S1 = 1/ (1+1+8) = 1/10

Note: The larger the SurvirorRatio setting is, the larger the Eden area will be

Find the thread that consumes the most CPU

Use the command to find threads that occupy the CPU

  1. jps -l pid
  2. jps -Hp pid

Query the status of each thread in a process

Tips: printf “% x” 12628 or 12628 jstack can filter a thread state jstack [pid] | grep 0 x5cf2

Jvisualvm looks at the health

It is not recommended to use this tool in the online environment because performance may be affected. In the high availability architecture, you can use the early pressure test phase or use remote tools

General jar startup Settings – XX: + HeapDumpOnOutOfMemoryError – XX: HeapDumpPath = / data/ucas – oom – dump. Hprof happen oom dumpjvm information automatically Many servers are backed up (highly available), and stopping this server does not affect online operations, remote connections, or JMAP DumpJVM information on other servers

Use jstat to view GC

While JVisualVM is very useful, servers are usually connected to terminals that don’t run graphical interfaces, and not all applications have JMX enabled, so it’s important to know how to see GC from the command line with jstat

Jstat -gc PID sampling interval number of milliseconds, for example, jstat -gc 12628 5000, to sample a GC with a PID of 8544 every 5sThe above picture shows an example: in the area of red hair cutting, the amount of S0 decreased to 0, while the amount of S1 increased, indicating that Young GC occurred and objects were migrated from S0 to S1.

The title bar has the following meanings:

S0C – Survivor 0 Capacity, KB unit S1C – Survivor 1 Capacity, S0U – Number of survivor Spaces Used (survivor 0 Used), S0U – number of survivor Spaces Used (survivor 0 Used), KB Unit Capacity of THE EC-Eden area (KB) Used in the EU-Eden area (KB) Used in the OC-old area (KB) Used in the Ou-old area (KB) Mc-metaspace Capacity (KB) Mu-Metaspace Used in the KB CCSC – Memory capacity of the compressed class (KB) CCSU – Used capacity of the compressed class (KB) YGC – (From the start of the application to the time of sampling) Young GC times YGCT – (From the start of the application to the time of sampling) FGCT – (From application startup, to sampling) Full GC time (s) GCT – (from application startup, to sampling) Full GC time (s) Yong GC + Full GC total time to sampling

It is worth noting that G1 garbage collector, in large heap (>4G), using G1 may work better.

-XX:+UseG1GC -XX:MaxGCPauseMillis=200

Online search – Arthas

How to download arthas, the commonly used commands www.jianshu.com/p/507f7e0cc…

  • The JVM observation screenshots are too large to fit in. I’ll take a look at some of them

  • Thread Locate a thread fault

  • Dashboard Observes the system status

  • Heapdump /data/111.hprof + Mat analyzes JVM information

  • Jad decompiling

  • Redefine hot replacement

  • watch – watch method

  • Help view command