Heap, a JVM has only one heap memory, and the size of the heap memory can be adjusted.
After the class loader reads these files, it usually puts what is in the heap?
Classes, methods, constants, variables, and save the real object of the referenced type.
The heap memory is divided into three areas:
- Newborn area (Eden space)
- Retirement area
GC garbage collection in the permanent area , mainly in the Eden Park and the retirement area.
Assuming that the memory is full, OOM, and the heap memory is not enough
after JKD8, the permanent storage area is renamed (meta space);
Newborn area:
category: place of birth and growth, even death;
-
Garden of Eden: All objects are new in the Garden of Eden
-
Survival area (0, 1)
-
After research, 99% of objects are temporary
Elderly area:
Permanent area
This area is resident in memory. Used to store Class objects carried by the JDK itself. Interface metadata stores some environment or class information when Java is running~, there is no garbage collection in this area! Turning off VM virtualization will release the memory in this area.
A startup class loads a large number of third-party jar packages. Tomcat has deployed too many applications, a large number of dynamically generated reflection classes. Constantly being loaded. Until the memory is full, 00M will appear;
●Before jdk1.6: permanent generation, the constant pool is in the method area;
●jdk1.7: permanent generation, but slowly degraded, go to the permanent generation, the constant pool is in the heap
● After jdk1.8: There is no permanent generation, the constant pool is in the meta space
Metaspace: logically exists, not physically
Let's run a program to see the memory size of the virtual machine:
public class Test {
public static void main(String[] args) {
//返回虚拟机试图使用的最大内存
long max = Runtime.getRuntime().maxMemory();
//返回jvm的初始化总内存
long total = Runtime.getRuntime().totalMemory();
System.out.println("max=" + max +"字节" + (max/(double)(1024*1024))+"MB");
System.out.println("total=" + max +"字节" + (total/(double)(1024*1024))+"MB");
//默认情况下:分配内的总内存大约是电脑内存的1/4,而初始化的内存为1/64
}
}
By default: the total memory allocated is about 1/4 of the computer memory, and the initialized memory is 1/64
. The result of the operation: (My computer memory is 16G)
It can be seen that 305664k + 699392 = 1029177344
so the physical memory of the meta space is not in the heap
Manually adjust the memory size in Configuration:
-Xms1024m -Xmx1024m -XX:+PrintGCDetails
UNCLE:
public class Hello {
public static void main(String[] args) {
// -Xms8m -Xmx8m -XX:+PrintGCDetails
String str = "longzx6666666666666";
while (true){
str += str + new Random().nextInt(999999999)+new Random().nextInt(999999999);
}
}
}
Adjust the memory to 8M, print the result
-Xms8m -Xmx8m -XX:+PrintGCDetails
In a project, 0OM failure suddenly appeared, then how to troubleshoot~Research why it went wrong
●Can see the error in the first few lines of the code: memory snapshot analysis tool, MAT, Jprofiler
●Dubug, analyze the code line by line!
MAT, Jprofiler function
●Analyze Dump memory files, quickly locate memory leaks;
●Get data in the heap
●Get big Object ~
Memory Analysis Tool: JProfiler download and install, IDEA has installed the plug-in
1. Download the JProfiler (IDEA) plug-
in. Download the Settings-plugins directly on IDEA, search for JProfiler and click the install button to install, and then start the IDEA tool.
Method 2:
Download the plug-in from the official website and manually install the
official plug-
in. Can see the latest version of JProfiler, including historical version download links. Click DOWNLOAD to download.
Then introduce the plug-in in IDEA,
find the location of your compressed package, and then restart after the
installation is successful, there will be the following signs
2. Install JProfiler monitoring software
official website address
3. Configure IDEA operating environment
Settings–Tools–JProflier–JProflier executable Select JProfile to install the executable file. (If only one version is installed in the system, it will be selected by default when starting IDEA) Save
test:
import java.util.ArrayList;
public class DumpDemo {
byte[] array = new byte[1*1024*1024]; //1m
public static void main(String[] args) {
ArrayList<DumpDemo> list = new ArrayList<>();
int count = 0;
try{
while (true){
list.add(new DumpDemo());
count = count + 1;
}
}catch (Exception e){
System.out.println(" count:"+count);
}
}
}
-Xms1m -Xmx8m -XX:+HeapDumpOnOutOfMemoryError
Run again
Open the file
Click to open the file, use JProfiler to analyze the
analysis thread:
you can also catch Error in the program,