This is the 18th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021″

One, foreword

Because MINE is a MAC, I run all my programs on the MAC, and sometimes some tools don’t work very well on the MAC. If there is a bad situation, you can refer to the article:

1. MAC installs multiple JDK versions

2. Fixed Jmap not working on MAC versions

These are some of the problems I encountered running Jmap on my MAC, so if you have, check them out.

Ii. Use of Jmap

1. Jmap-histo Process NUMBER

This command is used to check system memory usage, the number of instances, and memory usage.

Command:

jmap -histo 3241
Copy the code

Running results:

   
 num     #instances         #bytes  class name
----------------------------------------------
   1:       1101980      372161752  [B
   2:        551394      186807240  [Ljava.lang.Object;
   3:       1235341      181685128  [C
   4:         76692      170306096  [I
   5:        459168       14693376  java.util.concurrent.locks.AbstractQueuedSynchronizer$Node
   6:        543699       13048776  java.lang.String
   7:        497636       11943264  java.util.ArrayList
   8:        124271       10935848  java.lang.reflect.Method
   9:        348582        7057632  [Ljava.lang.Class;
  10:        186244        5959808  java.util.concurrent.ConcurrentHashMap$Node
Copy the code

So this is an array of bytes, how many instances, how much memory.

  • Num: serial number
  • Instances: indicates the number of instances
  • Bytes: indicates the occupied space
  • Class name: class name, [C is a char[], [S is a short[], [I is an int[], [B is a byte[], [[I is an int

2. Jmap-heap process NUMBER

Note: the Jmap command does not work well on macs, see introduction for details.

The command that runs on Windows or Linux is

Jmap-heap process numberCopy the code

Error: jdk8 cannot run properly, jdk9 can run properly

jhsdb jmap --heap --pid 2139
Copy the code

The execution result

Attaching to process ID 2139, please wait... Debugger attached successfully. Server Compiler detected. JVM version is 11.0.2+9 using ththread -local object allocation. Garbage-First (G1) GC with 8 thread(s) Heap Configuration: MinHeapFreeRatio = 40 MaxHeapFreeRatio = 70 MaxHeapSize = 4294967296 (4096.0MB) NewSize = 1363144 (1.2999954223632812MB) MinHeapFreeRatio = 40 MaxHeapFreeRatio = 70 MaxHeapSize = 4294967296 (4096.0MB) MaxNewSize = 2576351232 (2457.0MB) OldSize = 5452592 (5.1999969482421875MB) NewRatio = 2 SurvivorRatio = 8 MetaspaceSize = 21807104 (20.796875MB) CompressedClassSpaceSize = 1073741824 (1024.0MB) MaxMetaspaceSize = 17592186044415 MB G1HeapRegionSize = 1048576 (1.0MB) Heap Usage: G1Heap: Regions = 4096 Capacity = 4294967296 (4096.0MB) Used = 21654560 (20.651397705078125MB) Free = 4273312736 (4075.348602294922MB) 0.5041845142841339% used G1 Young Generation: Eden Space Regions = 15 Capacity = 52428800 (50.0MB) Used = 15728640 (15.0MB) Free = 36700160 (35.0MB) 30.0% used Survivor Space: Regions = 5 Capacity = 5242880 (5.0MB) Used = 5242880 (5.0MB) Free = 0 (5.0MB) 100.0% Used G1 Old Generation: Regions = 1 Capacity = 210763776 (201.0MB) Used = 0 (0.0MB) Free = 210763776 (201.0MB) 0.0% usedCopy the code

Based on the analysis of the above results, the contents of our query are as follows:

  • Process number: 2139
  • JDK version: 11
  • Garbage collector used: G1 (jdk11 default)
  • G1 garbage collector thread count: 8
  • You can also know heap size, used size, metadata size, and so on.
  • The size of a Cenozoic region. Capacity, used, idle, etc.

3. Jmap-dump exports heap information

Dump dump dump dump dump dump dump dump dump dump dump dump dump Then import visualization tools to analyze with JVisualVM.

Export orders

Jmap-dump :file=a.dump process idCopy the code

We can also set the dump file to automatically export when memory is out of order.

1. - XX: 2 + HeapDumpOnOutOfMemoryError - XX: HeapDumpPath =. / (path)Copy the code

Here are examples of how to use it.

Three, the use of JVisualVM command tools

1. Basic usage

Above we have derived the dump heap information to a file, you can use jvisualvm tool import heap dump information, carries on the analysis.

Open the JVisualVM tool command:

jvisualvm
Copy the code

The tool interface is as follows:

Click file -> load, you can import the file, check the operation of the system.

2. Case Study – Locating heap space overflow

The following uses tools to analyze the causes of memory overflow.

Step 1: Define a section of code that may run out of memory as follows:

import com.aaa.jvm.User; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import java.util.ArrayList; import java.util.List; import java.util.UUID; @SpringBootApplication public class JVMApplication { public static void main(String[] args) { List<Object> list = new ArrayList<>(); int i = 0; int j = 0; while (true) { list.add(new User(i++, UUID.randomUUID().toString())); new User(j--, UUID.randomUUID().toString()); }}}Copy the code

Step 2: Configure parameters

To make it easy to see, we’ll set two sets of parameters.

Group 1: Set the heap size. Make the heap size smaller to see the effect of memory overflow more quickly

‐ Xms10M ‐ Xmx10M ‐ XX: + PrintGCDetailsCopy the code

The heap is set to 10M and GC is printed

Dump **** file (if there is a large amount of memory, it may not be able to export)

1. - XX: 2 + HeapDumpOnOutOfMemoryError - XX: HeapDumpPath =. / (path)Copy the code

Add these two sets of parameters to the project startup configuration.

Print heap space information to a file during run:

jmap -dump:file=a.dump,format=b 12152
Copy the code

We can use tools to import heap files for analysis later (described below).

We can also set the dump file to automatically export when memory is out of order.

Complete parameter Settings are as follows:

-Xms10M -Xmx10M -XX:+PrintGCDetails -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/Users/zhangsan/Downloads
Copy the code

Note that the heap directory is written to an absolute path, not a relative path.

Step 3: Start the project and wait for memory to run out

We saw that it didn’t take long to run before the memory ran out.

View the directory to export to:

Step 4: Import the heap memory file into the JVisualVM tool

File -> Load -> select the file you just exported

Step 5: Analyze

We mainly look at the module [class].

Byte [], java.lang.String, com.lxl.jvm.user. It’s not easy to see what’s wrong with the first two, but with the third class com.lxl.jvm.User, we can see what’s wrong. Next, we will focus on checking where the class is called, and whether the memory is not freed.

The program is simple, but what are Byte [] and java.lang.String? The field type in our User object structure is String.

public class User {
    private int id;
    private String name;
}
Copy the code

Since there are many users, there are also strings.

So what about Byte []? The String class contains a byte[] member variable. So there are also a lot of byte[] objects.