Like attention, no more lost, your support means a lot to me!
🔥 Hi, I’m Chouchou. GitHub · Android-Notebook has been included in this article. Welcome to grow up with Chouchou Peng. (Contact information at GitHub)
directory
1. Run time data area
According to the Java Virtual Machine Specification, the Java VIRTUAL machine divides the memory into different data areas when executing programs:
Memory area | Thread the exclusive |
---|---|
Program counter register | private |
Java virtual machine stack | private |
Local method stack | private |
The Java heap | Shared |
Methods area | Shared |
— Picture quoted from Internet
1.1 Program Counter Register
The program count register records the bytecode line number of the next line to be executed on the current thread. When the virtual machine performs sequential execution, branch, loop, function call, or exception handling, the “line number of the next bytecode instruction” is stored in the program counter.
Why does the Java virtual machine need this program counter? It is to ensure proper thread switching. The operating system’s “time slice rotation mechanism” allocates time slices to each thread. When one thread runs out of time slices, or other threads snatch CPU slices in advance, the current thread will be suspended, and when the suspended thread acquires time slices, it will need to recover to the correct instruction position through the program counter.
The program counter is only meaningful when executing Java methods; if the current thread is executing native methods, the value of the program counter is null (Undefined).
1.2 Virtual MachineStack (Java Virtual MachineStack)
The virtual machine stack describes the in-memory model of Java method execution. The VIRTUAL machine will create a Stack Frame when executing a method. Each method from call to end corresponds to a Stack Frame in and out of the Stack.
- Push: Create the corresponding stack frame and push it into the VM stack.
- Unstack: Restores the local variable table and operand stack in the upper method, pushes the return value, if any, onto the stack, and finally adjusts the program counter to point to the next execution of the method call instruction. When all stack frames are off the stack, the thread terminates.
Tip: The default stack size is 1M, which can be resized using the virtual machine parameter -xss.
1.2.1 stack frame
Stack frames are data structures that support method invocation and method execution by virtual machines. Each stack frame contains four areas:
- 1. Local Variable Table
When a variable is no longer used, the corresponding space is set aside for other variables. The size of this area is determined at compile time.
- 2, Operand Stack
The operands used to hold bytecode instructions. The size of this area is determined at compile time. In a virtual machine implementation, this area could be a register or a stack. By “stack-based interpretation execution,” stack refers to the operand stack.
-
3. Dynamic Linking
-
4. Return the address
Return address The next line of instructions where a function call is placed, used to return to the upper method to continue execution if the method returns normally. If it is an exception return, it is determined by the exception handler table.
— Quoted from Paul. pub/ Android-dal… Strong Wave by Huawei
1.2.2 Stack vs Register
In the Java Virtual Machine Specification, the operand stack is a stack data structure, but in a virtual machine implementation, it can also be a register structure.
- Stack-based interpretation execution — Java Virtual Machine
- Register-based Interpretation execution — Android Virtual Machine (Dalvik & ART)
Error prone: Stacks and registers are virtual implementations of virtual machines, not the same concept as data registers in the CPU.
Register-based virtual machine stack frames do not have operand stacks and local variable tables in place of virtual registers. Registrie-based Android virtual machines have significantly fewer instructions than Java virtual machines, while avoiding data movement between operand stacks and local tables of variables.
1.2.3 Stack optimization technology
- Compiler optimization: methods are inlined
Methods are inlined to copy the code of the target method to the location of the call, avoiding the out-of-stack behavior of the method call.
- Stack frame data sharing
The memory area of the two stack frames is usually independent, but in most virtual implementations, the “operand stack” of the lower stack frame is overlapped with the “partial local variables” of the upper stack frame, so that no additional arguments are copied during method calls.
— Picture quoted from Internet
1.3 Native Method Stacks
This is similar to the virtual machine stack, except that the virtual machine stack executes Java methods while the native method stack executes native methods. When a VIRTUAL machine calls a native method, the stack frame is not created in the virtual machine stack, but is directly dynamically linked to call a Native method.
Tip: The Java Virtual Machine Specification does not mandate the method language, usage or data structure in the local method stack, and some virtual machines (such as HotSpot) directly merge the virtual machine stack and the local method stack.
1.4 Method Area
1.4.1 Data in method area
The method area stores class-related data loaded by VMS, including:
- The class information
- A static variable
- Static constants
- Run-time constant pool
- Just-in-time compiler generated code
The term “Class file constant pool” and “runtime constant pool” are confusing concepts. In fact, one of them is static and the other is dynamic.
The “Constant Pool Table” is static and refers to the literal & symbol references stored in the Class file Constant Pool after compilation, which are entered into the runtime Constant Pool after the Class is loaded.
The runtime constant pool is dynamic. Java does not require that constants be declared only at compile time. New constants can also be added to the constant pool at run time. For example String# intern ().
Tip: The so-called string constant pool is part of the runtime constant pool.
1.4.2 Method area? Eternal generation? Yuan space?
These three concepts are also easy to confuse, simply put: method area is the run-time data area specified by the VIRTUAL machine, and permanent generation & meta space is the specific implementation of method area in different virtual machines.
Take the HotSpot VM as an example:
Prior to JDK 1.7, the HotSpot virtual machine implemented the method area using Permanent Generation, which stored data with a long life cycle. The permanent generation can perform garbage collection with the heap. However, the “cost performance” of garbage recycling in the permanent generation is not as high as that of the new generation. Generally, the garbage recycling of the new generation can recover 70% to 95% of the space, while the garbage recycling rate of the permanent generation is much lower than this.
Starting with JDK 1.8, the HotSpot VIRTUAL machine uses Metadata to implement the method area and uses local memory storage to extend the memory limit of the method area.
Why does HotSpot use meta-spaces instead of persistent generations?
Because persistent generation space is limited, there are often insufficient or out-of-memory exceptions. Using local memory makes it easy to expand the size of the method area. Of course, the meta-space is not perfect, because the total machine memory is limited, and using a lot of local memory will squeeze the maximum heap memory.
1.5 Java Heap
The heap is the largest chunk of memory in a virtual machine, and most objects are stored on the heap (Class objects are stored in the method area, and objects that satisfy escape analysis are allocated on the stack). The main area where the garbage collection mechanism operates is also the heap.
Both heap and method areas are shared by threads, so why does Java distinguish between the two areas?
This reflects the idea of static and static separation, the heap is stored in a relatively short life cycle, often need to carry out garbage collection data; The method area stores data with a relatively long life cycle. Storing the two types of data separately facilitates more efficient memory management.
2. Direct memory/off-heap memory/local memory
These three concepts are the same: Direct Memory is not part of the runtime data region specified by the Java virtual machine. Not subject to Java heap size limits, but subject to total machine memory.
In JDK 1.4 NIO, Channel and Buffer based I/O is introduced, which can allocate a chunk of direct memory directly through native methods, and create a Java layer DirectByteBuffer object for application layer access.
3. The memory overflows
3.1 Program counter
In the Java Virtual Machine Specification, the program counter is the only area of the JVM where OOM does not occur.
3.2 stack overflow
Virtual stack and local method stack are similar in that they can throw two types of exceptions:
- StackOverflowError: The thread stack frame depth is greater than the maximum allowed by the virtual machine.
- OutOfMemoryError: Failed to allocate sufficient memory.
3.3 the heap overflow
A heap overflow occurred when the requested memory exceeded the maximum heap memory. Check whether the life cycle of some objects is too long, the holding time of some objects is too long, or the storage structure is improperly designed to minimize redundant or unnecessary memory consumption.
3.4 Method area overflow
There are two situations that can cause method area memory to overflow:
- Run time constant pool overflow
- 2. The loaded class information overflows
3.5 Direct Memory Overflow
Like the heap, an overflow of memory occurs when the requested direct memory exceeds the direct memory capacity.
ByteBuffer.allocateDirect(128*1024*1024)
Copy the code
java.lang.OutofMemoryError : Direct buffer memory
Copy the code
4. To summarize
-
1. The program counter is thread private and describes the bytecode instruction line number to be executed next on the current thread.
-
The virtual machine stack describes the memory model of Java method execution.
-
3. The local method stack is similar to the VIRTUAL machine stack, except that the virtual machine stack executes Java methods while the local method stack executes native methods.
-
4. The heap is the largest chunk of memory in a virtual machine. Most objects are stored on the heap, and the main area of garbage collection mechanism operation is also the heap.
-
5. The method area mainly stores class-related data loaded by virtual machines.
Creation is not easy, your “three lian” is chouchou’s biggest motivation, we will see you next time!