1. Memory model and runtime data areaThis chapter will introduce the Java Virtual Machine Memory Model. It can be understood that the JVM runtime database is a specification, and the JVM memory model is an implementation of this specification. The Java virtual machine focuses on storing data in the heap and method area, so this chapter also focuses on describing these two aspects in detail. The heap and method area are shared memory, while the Java virtual machine stack, Native method stack, and program counter are thread-private. 2. Mind Maps and LegendsOne is the non-heap area (method area), which is also generally referred to as the "permanent generation". The other is the heap area, which is divided into the young area and the old area. The young area is divided into two parts, one is the Eden area, and the other is the Survivor area (S0+S1). The S0 area can also be called the From area, and S1 can also be called the To area. 3. Objects apply for space from JVM4. Why do we need a Survivor zone?Why do we need a survivor area? Isn't Eden enough? Assuming that the Survivor area is not designed, a MinorGC is performed in the Eden area, and the objects are directly sent to the Old area. In this way, the Old area will be filled up quickly. Once the Old area is full, FullGC will be performed (the Old area will perform MajorGC, usually accompanied by MinorGC). FullGC is very time-consuming, so the purpose of designing the Survivor area is to reduce the number of objects sent to the Old area. There is a transitional Survivor area.
5. Why do we need two Survivor zones?
6. Examples for verificationHeap memory overflowimport lombok.Data; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import java.util.ArrayList; import java.util.List; @RestController public class HeapController { List<Foo> list = new ArrayList<Foo>(); @GetMapping(value = {"heap"}) public String heapTest() { while (true) { list.add(new Foo()); } } @Data class Foo { String str; } } When accessing the interface, a memory overflow occurs;
Method area memory overflowUsing asm, maven configuration: <dependency> <groupId>asm</groupId> <artifactId>asm</artifactId> <version>3.3.1</version> </dependency> Write the code and add the Class information to the method area. Note that if the computer performance is not good enough, do not execute this code. It is easy to cause the computer to restart. If it consumes too much memory, you can also reduce the number of loops. import org.objectweb.asm.ClassWriter; import org.objectweb.asm.MethodVisitor; import org.objectweb.asm.Opcodes; import java.util.ArrayList; import java.util.List; public class MyMetaspace extends ClassLoader { public static List<Class<?>> createClasses() { List<Class<?>> classes = new ArrayList<Class<?>>(); for (int i = 0; i < 10000000; ++i) { ClassWriter cw = new ClassWriter(0); cw.visit(Opcodes.V1_1, Opcodes.ACC_PUBLIC, "Class" + i, null, "java/lang/Object", null); MethodVisitor mw = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null); mw.visitVarInsn(Opcodes.ALOAD, 0); mw.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V"); mw.visitInsn(Opcodes.RETURN); mw.visitMaxs(1, 1); mw.visitEnd(); MyMetaspace test = new MyMetaspace(); byte[] code = cw.toByteArray(); Class<?> exampleClass = test.defineClass("Class" + i, code, 0, code.length); classes.add(exampleClass); } return classes; } } Method area test interface: import com.example.jvm.jvmexceptionexample.asm.MyMetaspace; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import java.util.ArrayList; import java.util.List; @RestController public class NonHeapController { List<Class<?>> list = new ArrayList<Class<?>>(); @GetMapping(value = {"/noheap"}) public String noheap() { while (true) { list.addAll(MyMetaspace.createClasses()); } } }
Java Virtual Machine StackAs we learned earlier, the Java virtual machine stack is stored in the form of stack frames. One method corresponds to one stack frame, which is pushed into the stack in a queue mode. Therefore, if you want to test whether the program causes problems with the Java virtual machine stack, you can test it through a recursive method: import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class StackController { public static long count = 0; public static void add(long i) { count ++ ; add(i); } @GetMapping(value = {"stack"}) public void stack() { add(1); } } StackOverflow, stack overflow exception:
The above is the detailed content of the detailed explanation of the memory model of the JVM series. For more information about the memory structure of the JVM memory model, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
<<: Tutorial on installing mysql5.7.23 on Ubuntu 18.04
>>: Tutorial on building a zookeeper server on Windows
Perhaps when I name this article like this, someon...
To achieve the following goals: Mysql database wi...
Table of contents 1. Static implementation method...
This method was edited on February 7, 2021. The v...
tomcat is an open source web server. The web base...
Table of contents Preface LED Trigger Start explo...
First, understand a method: Entering a Docker con...
First, download a series of things from the Alipa...
1. Type introduction 1.1 Domain-based virtual hos...
Table of contents 1. Solution 2. Let the browser ...
This tag is not part of HTML3.2 and is only suppo...
When the img src value is empty, two requests are ...
1. Download the virtual machine Official download...
Scenario: When starting tomcat in docker (version...
Table of contents LAMP architecture 1.Lamp Introd...