First, Jmap find the instance that occupies a large memory
Let's give you a sample code:
Import Java.util.arraylist;import java.util.list;import java.util.concurrent.countdownlatch;/** * Created by the Banyan Tree On 05/09/2017. */public class Oomtest {public static void main (string[] args) throws interruptedexception {Countdownlatch lat ch = new Countdownlatch (1); int max = 10000; list<person> list = new arraylist<> (max); for (int j = 0; J < Max; J + +) {person p = new person (); P.setage (100); P.setname ("Yang over under the Bodhi Tree"); List.add (P); } System.out.println ("ready!"); Latch.await (); } public static class person {private String name; private int age; Public String GetName () {return name; } public void SetName (String name) {this.name = name; } public int Getage () {return age; public void Setage (int.) {this.age = age; } }}
The list puts an instance of the 1w person object and runs the program first.
Javac Oomtest.java
Java oomtest
Then open a window, jps-l find out the PID of the program
Then execute jmap-histo:live 7320
Output results, will be in memory usage, from large to small to the actual number of objects, the amount of memory (number of bytes) printed out, and finally output summary information
In the example above, the number of instances of the Oomtest$person class is 10,000, which takes up 240000 bytes (Note: 24 bytes per instance), and the program takes up a total of 725464 bytes of memory, approximately: 0.69M.
There are also some [C,[b class name, which probably means:
[C is a char[]
[S is a short[]
[I is a int[]
[B is a byte[]
[[I is a int[][]
[A C object is often associated with a string, and a string whose internal uses the final char[] array to hold the data
Constmethodklass/methodklass/constantpoolklass/constantpoolcacheklass/instanceklassklass/methoddataklass
Associated with ClassLoader, resident and Perm district.
Second, find out the number of handles opened by a Java application and the number of threads
ll/proc/{pid}/fd | Wc-l to view open handles
Ll/proc/{pid}task | Wc-l viewing the number of threads
Iii. Jmap View the configuration of heap memory
The jmap-heap PID can see output similar to the following:
Using Thread-local object allocation. Parallel GC with 4 thread (s)//current GC mode (parallel GC) heap configuration://Heap memory configuration Minheapfreeratio = 0//corresponding JVM startup parameter-X X:minheapfreeratio set JVM heap minimum idle ratio (java8 default 0) Maxheapfreeratio = 100//corresponding JVM boot parameters-xx:maxheapfreeratio set JVM heap max idle ratio M Axheapsize = 8388608 (8.0MB)//corresponding to JVM startup parameters-xx:maxheapsize= Set maximum size of JVM heap (or-xmx parameter) NewSize = 524 2880 (5.0MB)//corresponding to JVM startup parameters-xx:newsize= set JVM heap ' Cenozoic ' default maxnewsize = 5242880 (5.0MB)//corresponding JVM startup parameters-xx:maxnewsize = Set the maximum size of the ' Cenozoic ' of the JVM heap oldsize = 3145728 (3.0MB)//corresponding to the JVM startup parameters-xx:oldsize= Set the size of the ' Laosheng generation ' of the JVM heap Newratio = 2//For JVM startup parameters-xx:newratio=: The size ratio of ' cenozoic ' and ' laosheng ' Survivorratio = 8//For JVM startup parameters-xx:survivorratio= set young generation The size ratio between Eden and survivor area Metaspacesize = 21807104 (20.796875MB) compressedclassspacesize = 1073741824 (1024.0 MB) maxmetaspacesize = 17592186044415 MB g1heapregionsize = 0 (0.0MB) heap usage://Heap Memory usagePS Young Generationeden Space://eden area Distribution capacity = 2621440 (2.5MB)//eden area Total capacity used = 2328088 (2.220237731933593 8MB)//eden area has been used free = 293352 (0.27976226806640625MB)//eden area remaining capacity 88.80950927734375% Usedfrom Space://One of the Survivo R zone Memory Distribution capacity = 1572864 (1.5MB) used = 360448 (0.34375MB) free = 1212416 (1.15625MB) 22.916666666666668 % usedto Space://Memory distribution in another survivor area capacity = 1048576 (1.0MB) used = 0 (0.0MB) free = 1048576 (1.0MB) 0.0 % Usedps old Generation//current old area memory distribution capacity = 3145728 (3.0MB) used = 1458968 (1.3913803100585938MB) free = 1686760 (1.6086196899414062MB) 46.37934366861979% used3759 interned Strings occupying 298824 bytes.
Note: Line 5-16 is the main configuration of the heap memory, which can be resized by java-xx: parameter name = parameter value, for example:
Java-xx:minheapfreeratio=20-xx:maxheapfreeratio=80-xmx100m-xx:metaspacesize=50m-xx:newratio=3 Values that affect Minheapfreeratio, Maxheapfreeratio, Maxheapsize, Metaspacesize, Newratio
Notice the Newratio, this value refers to the old Generation: The New Generation (young Generation) ratio, above set to 3, so oldsize is 75m, and newsize is 25m, reference:
Note: This is JDK7, jdk8 in permanent generation was removed, new added Metaspace area, but this difference does not affect the generation, Laosheng generation understanding.
The New Generation (young Generation) can be subdivided into three chunks: Eden, S0, and S1.
Java7 and java8 memory changes, roughly like.
Survirorratio This is difficult to calculate, according to the Oracle official website explanation: https://docs.oracle.com/cd/E19159-01/819-3681/abeil/index.html, the default value is 8, That is: the size of each Survivor:eden area is 1:8, in other words s0 = S1 = 1/(1+1+8) = 1/10
Note: Although the official website so explain, but I actually forget, as if not strictly according to this proportion to calculate, can only be said to be the proportion of this distribution. (The conclusion is: the larger the Survirorratio setting, the greater the Eden area)
Iv. finding the most CPU-intensive threads
Let's start with a demo code:
Import java.util.concurrent.countdownlatch;/** * Created by the Bodhi tree under the Yang over on 05/09/2017. */public class Oomtest {public static void main (string[] args) throws interruptedexception {Countdownlatch lat ch = new Countdownlatch (1); int max = 100; for (int i = 0; i < max; i++) {Thread t = new Thread () {public void run () { try {thread.sleep (50); } catch (Interruptedexception e) {thread.currentthread (). interrupt (); } } }; T.setname ("thread-" + i); T.start (); Thread t = new Thread () {public void run () {int i = 0; while (true) {i = (i++)/10; } } }; T.setname ("BUSY THREAD"); T.start (); System.out.println ("Ready"); Latch.await (); }}
There are 100 threads in it that are idling, and another thread busy threads running the CPU.
Javac Oomtest.java
Java oomtest
Run the program, jps-l find the PID, and then TOP-HP the PID
Can see the PID 16813 this corresponding thread, the CPU ran full, reached 98.5%
Next, convert 16813 into 16, or 41AD, and then
Jstack PID | grep ' 41AD '
Let's find out what the busiest thread busy threads is (note: This tip again shows that it's important to take a good name for a thread!)
Tips: If you use Spring-boot, you can see the/dump endpoint directly in your browser, or you could achieve a similar jstack effect.
V. JVISUALVM View operation status
Jdk_home/bin has a self-JVISUALVM tool that allows you to graphically view the GC situation (note: To install the plugin)
java.net This site has been closed by Oracle, so install plug-ins here, a little trouble, first to https://visualvm.github.io/pluginscenters.html here to find JVISUALVM corresponding JDK version number, Take Jdk8 as an example, the address is https://visualvm.github.io/uc/8u131/updates.xml.gz
Then, the address in the plugins in the settings, and then available plugin here, you can see the available plug-ins, select the GC plug-in and install.
Can come up with a piece of code, and then use JVISUALVM to see the GC situation
Import Java.util.arraylist;import java.util.list;/** * Created by Bodhi under the Poplar on 05/09/2017. */public class Oomtest {public static void Main (string[] args) throws Interruptedexception { list<string> List = new arraylist<> (); while (true) { thread.sleep (); List.add ("Yang over the Bodhi tree" + system.currenttimemillis ());}}}
You can see the memory changes in the old, Eden, S0,s1 and Metaspace areas visually.
In addition, you can see the class that consumes the most memory (that is, the one mentioned at the beginning of this article)
You can also see which threads are busiest
Reference article:
Java GC Series http://www.importnew.com/13504.html
Deep understanding of Java G1 garbage collector http://blog.jobbole.com/109170/
Java: Common means of troubleshooting online