preface
The job has been confirmed, the house has been found, the computer desk was in place yesterday, and the blog has been updated normally since today. Plan to finish writing the JVM by April.
The JVM family focuses on class loading, Java memory areas, garbage collection, JVM parameter configuration, and troubleshooting using Java tools, arthas, and Linux commands
JAVA memory region
The JAVA memory area (JAVA runtime data area) is not to be confused with the JAVA Memory Model (JMM).
The JAVA Memory Model (JMM) defines how the JAVA Virtual Machine (JVM) works in computer memory (RAM). The JAVA Memory Model refers to a set of specifications for how threads can access memory.
Reference picture luoyoubao. Gitbooks. IO/JVM/content…
Reference picture luoyoubao. Gitbooks. IO/JVM/content…
The JVM is divided into three main subsystems:
- Class loader subsystem
- Runtime data area
- Execution engine
The Class loading subsystem is responsible for loading Class information from the file system or network in the method area.
PC register (thread private)
Each thread in the Java virtual machine has its own PC register. Only one method code is executed by a thread at any one time. If the method being executed is not native, the PC register holds the address of the bytecode instruction being executed. If the native method is executed, the PC register value is null. The PC storage area is also the only area that does not throw an OOM exception.
JAVA Virtual Machine Stack (thread private)
Each thread has its own virtual stack, which is created at the same time as the thread and is used to store local variables or Pointers to the heap. In the Java Virtual Machine specification, StackOverflowError is thrown if a method is called too deeply recursively; An OOM exception is raised if the system cannot allocate enough memory.
-Xss Adjusts the stack size.
Native method stack (thread private)
The stack used to call native methods. Throw StackOverflowError when the stack overflows. Symptom An OOM exception is thrown when memory application fails.
Heap (thread sharing)
The heap is an area that is shared by threads, and the garbage collection mainly collects it, and we mostly deal with the heap. -xms and -xmx are used to resize the heap.
Method area (thread sharing)
A method area is an area of memory shared by threads that stores Class structure information. For example, run-time constant pools, fields, methods, constructors. The method area uses local memory (off-heap memory), which is equivalent to the memory requested on the system. Methods will throw OOM. Method areas are tuned using metadata Spaces.
-xx :MaxMetaspaceSize: set. The default value is -1.
-xx :MetaspaceSize: specifies the initial space size of the metacase. The unit is byte.
Memory region exception throwing demonstration
Citing mp.weixin.qq.com/s/hj2GcW5nH images…
In practice we focus on the heap, direct memory, stack, metadata area.
Heap thrown OOM
When the idea to start the program, the incoming virtual machine parameters – Xms100m – Xmx100m – XX: + HeapDumpOnOutOfMemoryError. And use JVisualVM to observe the heap usage.
The results will be thrown Java. Lang. OutOfMemoryError: Java heap space.
HeapDumpOnOutOfMemoryError specifies the exception is thrown when the dump memory to a file. We can analyze the problem by analyzing the file to see which objects occupy the most space.
/ * * *@authorZhang qin * climbing@dateThe 2021-03-23 - mingled * /
public class HeapOOM {
static class Obj {
private byte[] a = new byte[1024 * 1024 * 10];
}
public static void main(String[] args) {
final ArrayList<Object> objects = Lists.newArrayList();
int count = 0;
while (true) {
try {
objects.add(new Obj());
System.out.println("How many times has it been added?" + ++count);
} catch(Throwable e) { e.printStackTrace(); }}}}Copy the code
Stack overflow
-Xss Sets the stack size. A StackOverflowError is raised when the stack call is too deep.
-Xss512k: The call depth is 4868.
-Xss256k: the depth of the printed result is 1889.
public class StackOverflowErrorDemo {
private int count = 0;
public void add(a) {
count++;
add();
}
public int getCount(a) {
return count;
}
public static void main(String[] args) {
final StackOverflowErrorDemo stackOverflowErrorDemo = new StackOverflowErrorDemo();
try {
stackOverflowErrorDemo.add();
} catch (Error e) {
System.out.println("Call depth"+ stackOverflowErrorDemo.getCount()); }}}Copy the code
Method area overflow
The method area mainly stores class-loading information, which can be simulated by dynamic proxies.
-xx :MaxMetaspaceSize=20m Sets the metasize size.
public class MetaOOM {
public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
while (true) {
try {
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(IMetaService.class);
enhancer.setUseCache(false);
enhancer.setCallback(new MethodInterceptor() {
@Override
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
returnproxy.invoke(obj, args); }}); enhancer.create(); }catch(Throwable e) { System.err.println(e.getMessage()); }}}}interface IMetaService {
void add(a);
}
Copy the code
Direct memory overflow
The capacity of DirectMemory (also off-heap Memory) can be set with -xx :MaxDirectMemorySize. The default value is 64 MB.
Nio typically uses direct memory. -XX:MaxDirectMemorySize=50m
public static void main(String[] args) {
final ArrayList<Object> objects = new ArrayList<>();
while (true) {
try {
objects.add(ByteBuffer.allocateDirect(1024 * 1024 * 10));
} catch(Throwable e) { System.out.println(e.getMessage()); }}}Copy the code