This is the 20th day of my participation in the November Gwen Challenge. Check out the details: The last Gwen Challenge 2021
Introductions to the 0.
At runtime, Java divides memory into several regions, roughly divided into heap and stack. The heap stores Java objects. The stack area mainly records the reference address of the object.
There are more detailed divisions. As shown below:
1. Java VM stack
The Java virtual machine stack is the memory we divided into heap area and stack area, and the stack area here refers to the Java virtual machine stack.
Each method creates a stack frame to store local variables, operand stacks, dynamic linked lists, method exit information, and so on. The process of each method from invocation to completion corresponds to the process of a stack frame being pushed into and out of the virtual machine stack. It is mainly used to store local variable tables.
Two exceptions are thrown in this area, a StackOverflowError if the requested stack depth exceeds the maximum depth allowed by the virtual machine, and an OutOfMemoryError if sufficient memory cannot be allocated.
2. Local method stack
Like the Java virtual machine stack, the Native method stack serves Native methods. Providing memory for local methods also throws StackOverflowError, OutOfMemoryError exceptions.
3. Program counter
A program counter can be seen as a line number counter of bytecode executed by the current thread. The bytecode interpreter needs to change this value to select the next bytecode instruction to execute. Branches, loops, jumps, exception handling, thread recovery, and other basic functions in the program depend on this counter.
4. The method area
The method area is used to hold information about classes loaded by the JVM, including methods, static variables, and type information (interface/parent). This is where we get the information we need to use reflection. Similarly, OutOfMemoryError is thrown when the method area does not meet memory requirements.
4.1 Runtime constant pool
The constant pool is the part of the method area used to record the various literal and symbolic references generated at compile time. Fields identified with final in Java classes are placed in the constant pool. Constant pools are not immutable at run time. A common scenario is the Intern method of the String class. When this method is called, the JVM determines whether the object is in the constant pool and returns it if it is; otherwise, the string needs to be put into the constant pool and returned.
5. The Java heap
The orange area on the right is the Java heap. This is the largest memory area in the Java virtual machine. We have previously divided memory roughly into heap areas and stack areas, where stack refers to the Java virtual machine stack and heap refers to the Java heap. This is where all Java objects are allocated memory and where most of the VIRTUAL machine’s GC action takes place.
A further subdivision divides the Java heap into the new generation and the old generation. The new generation can be divided into From Survivor zone, To Survivor zone and Eden zone.
The ratio of Eden zone, From Survivor zone and To Survivor zone in the New generation is 8:1:1.
Objects are allocated memory in the new generation when they are first created. To be precise, allocate memory in Eden region. After one GC, the age of the uncollected object is increased by one and it is moved From the Eden zone, From Survivor zone, To Survivor zone. The To Survivor zone becomes the new From Survivor zone. Also, the From Survivor zone becomes the new To Survivor zone.
When the age exceeds a certain value (15 by default) the object is moved to the old age.
PS: Not all objects are allocated memory in the new generation when they are first created. Some objects are allocated memory in the old generation when they are large enough to exceed the memory of the new generation