Reference:
www.jianshu.com/p/0a1e0e410…
Blog.csdn.net/weixin_3994…
zhuanlan.zhihu.com/p/161994662
JDK1.8 runtime data area
Divided by thread calls into:
- Thread public: heap, method area, direct memory (not part of runtime memory), metadata
- Thread private: virtual machine stack, local method stack, program counter
Note: The runtime data area is different in versions prior to HotSpot 1.8, moving the method area into the meta-space.
Heap Heap
An overview of the
There is only one heap memory space in the JVM, shared by all threads, and the memory for all class instances and arrays is stored in the heap, which is the largest memory area in the VIRTUAL machine and the main area for garbage collection.
Heap allocation
The heap is created at Java virtual machine startup and can be fixed or scalable in size. Heap memory does not require contiguous space.
- -xms: the JVM initially allocates heap memory, which is 1/64 of the physical memory by default
- -xmx: specifies the maximum heap memory allocated by the JVM. The default heap memory is 1/4 of the physical memory
- If -xmx is not specified or the value is too small, it may cause
OutOfMemoryError
Error, this error is from JVM and cannot be tried… Catch catch
Heap memory collection
Heap size = New generation + old age, default new generation: old age =1:2
Methods area -> Meta Space Meta Space
- Method area: before JDK1.8, it stored information about classes loaded by virtual machines, constants, static variables, and dynamically generated classes
- Meta space: shared by threads, using direct memory, not in the virtual machine, GC does not manage the memory of the meta space
Note: The allocation of native direct memory is not limited by the Java heap, but since it is memory, it is limited by the total native memory size and processor addressing space.
Program Counter Register
A program counter is a small area of memory that can be used as a line number indicator of the bytecode being executed by the current thread. The bytecode interpreter selects the next instruction by changing the value of the counter. Function dependent on program counters: loops, exception handling, thread recovery, etc.
- Each thread has a private program counter to ensure that the thread switches back to the correct execution position
- The thread executes the Native method and the counter is empty
- The thread executes the Java method, and the counter records the address of the virtual machine bytecode instruction being executed
- The only region where OutOfMemoryError is not defined, the life cycle is tied to the thread
Stack Stack
The virtual machine stack is thread-private and has the same lifecycle as the thread. The Stack element in a VM Stack is called a Stack Frame. A Stack Frame is a data structure used to support vm method invocation and method execution. From the beginning of invocation to the completion of execution, a Stack Frame corresponds to the process of loading and unloading a method in a VM Stack.
Methods are pushed out of the stack in the following scenarios: return statements, exceptions thrown
Each stack frame contains the following information:
- Local variable table: store data types (basic types) and object references known during compilation.
- The operand stack
- Dynamic link
- Method return address
- . .
Stack features:
- The memory space required for the local variable table is determined and allocated at compile time
- Methods The size of local variable scale was not changed during operation
- A StackOverflowError is raised when the requested stack depth is greater than the virtual machine allowed if the stack size does not allow dynamic expansion
- OutOfMemoryError is raised if the memory size of the VM stack is insufficient due to dynamic expansion
Native Method Stack
The function of the local method stack is similar to that of the virtual machine stack, except that the virtual machine stack executes Java method (bytecode) services for the VIRTUAL machine, while the local method stack serves Native methods.
When a local method is executed, the local method stack creates a stack frame, which also holds the local method’s: local variable table, operand stack, dynamic link, exit information, etc.
Understand Native methods
Native Method: is a Java interface that calls non-Java code. A Native Method means that the implementation of the Method is implemented in a non-Java language.
How the VM makes Native Methods work: When a class is used for the first time, the class’s bytecode is loaded into memory and only loaded back once. The entry to the loaded bytecode maintains a list of all the method descriptors of the class, which contain information about where the method code is stored, what parameters it takes, the method descriptors (public, etc.), and so on. If a method descriptor has a native, the descriptor block will have a pointer to the implementation of the method. These implementations are in some DLL files, but they are loaded by the operating system into the Address space of the Java program. When a class with a local method is loaded, its associated DLL is not loaded, so Pointers to the method implementation are not set. These DLLS are loaded only before the local methods are called, which is done by calling java.system.loadLibrary().
Run-time constant pool
The runtime constant pool is part of the method area.
JDK1.8 hotspot removes the Metaspace from the heap, where the string constant pool is still in the heap and the runtime constant pool is still in the method area, but the method area implementation is changed from the permanent generation to Metaspace.