Review the JVM
JVM = Classloader + Execution engine + Runtime data area
Runtime data area
During the execution of Java programs, the Java VIRTUAL machine divides the memory it manages into different data areas. Each of these regions has its own purpose and time of creation and destruction. Some regions exist with the start of a virtual machine process, while others are created and destroyed depending on the start and end of a user thread.
Program Counter Register
- A thread is private and has the same life cycle as a thread.
- Can be thought of as a line number indicator of the bytecode being executed by the current thread.
- In the concept of virtual machine model (just the conceptual model, all kinds of virtual machine may be through some of the more efficient way to achieve), bytecode interpreter to work is by changing the value of the counter to select a bytecode instruction need to be performed, such as: branches, loops, jumps, exception handling, thread switch (multithreaded) and other basic function recovery.
- If the thread is executing a Java method, this counter records the address of the virtual machine bytecode instruction being executed. If the Natvie method is being executed, this counter value is null (undefined).
- The amount of space occupied by data stored in the program counter does not change as the program executes, so outofMemoryErrors do not occur in this area.
Java Virtual Machine (JVM Stacks)
- The thread is private and has the same life cycle as the thread.
- Virtual machine Stack describes the memory model of Java method execution: each method execution creates a Stack Frame to store information such as local variation table, operation Stack, dynamic link, method exit, etc. Each method is called until the execution is complete, corresponding to the process of a stack frame in the virtual machine stack from the stack to the stack.
- Local variable tables store basic data types known at compile time (Boolean, byte, CHAR, short, int, float, long, double), object references (reference types), which are not equivalent to the object itself. It may be a reference pointer to the object’s starting address, a handle to the object or some other location associated with the object, and the returnAddress type (which points to the address of a bytecode instruction). The memory space required for the local variable table is allocated at compile time. When entering a method, how much local variable space the method needs to allocate in the frame is completely determined, and the size of the local variable table does not change during the method run.
- This area may throw the following exceptions:
- A StackOverflowError is raised when the stack depth of a thread request exceeds the maximum.
- An OutOfMemoryError is raised if sufficient memory cannot be allocated for dynamic stack scaling.
Native Method Stacks
- It is very similar to the virtual machine stack, except that the virtual machine stack performs Java methods (that is, bytecode) services for the virtual machine, whereas the Native method stack serves Native methods used by the virtual machine. The virtual machine specification does not mandate the language, usage, or data structure of methods in the local method stack, so specific virtual machines are free to implement it. There are even virtual machines (such as the Sun HotSpot VIRTUAL machine) that simply merge the local method stack with the virtual machine stack.
- Like the virtual stack, the local method stack area throws StackOverflowError and OutOfMemoryError exceptions.
Java Heap
Method Area
- Used to store loaded class information, constants, static variables, just-in-time compiler compiled code, and so on.
- Like the Java heap, it does not require contiguous memory and can be dynamically expanded, with OutOfMemoryError thrown if the dynamic expansion fails.
- The main goal of garbage collection on this area is to recycle the constant pool and unload the classes, but it is generally difficult to achieve, and the HotSpot VIRTUAL machine treats it as Permanent Generation to garbage collect.
- The method area is logically part of the heap, but is often referred to as “non-heap” to distinguish it from the heap.
Runtime Constant Pool
- The runtime constant pool is part of the method area.
- The constant pool in the Class file (the various literal and symbolic references generated by the compiler) is placed in this area after the Class is loaded.
- In addition to constants generated at compile time, dynamic generation is allowed, such as intern() of the String class. These constants are also put into the runtime constant pool.
Note:
- Prior to JDK1.7, HotSpot implemented the method area using persistent generation; HotSpot uses GC generation to implement the method area with great convenience;
- HotSpot has been removing persistent generations since JDK1.7. Where Symbols references are moved to the Native Heap, string constants and class references are moved to the Java Heap.
- In JDK1.8, permanent generations have been completely replaced by meatspaces. Similar in nature to permanent generations, meta-spaces are implementations of method areas in the JVM specification. However, the biggest difference between a meta-space and a permanent generation is that the meta-space is not in the virtual machine, but uses local memory. Therefore, by default, the size of the meta-space is limited only by local memory.
Direct Memory
- Direct Memory is not part of the run-time data portion of the virtual machine, nor is it defined in the Java Virtual Machine specification, but it is also frequently used and can cause OutofMemoryErrors.
- The NIO class was introduced in JDK 1.4, introducing a Channel and Buffer based I/O approach that uses Native libraries to allocate out-of-heap memory directly. It then operates via a DirectByteBuffer object stored in the Java heap as a reference to this memory. This can significantly improve performance in some scenarios because it avoids copying data back and forth between the Java heap and Native heap.
Reference links:
- JVM Memory model-1;
- JVM Memory model-2;
- JVM runtime stack frame structure;
- Overflow of virtual machine stack and local method stack;
- Heap memory partition;
- Learn the Java heap, method area, Java stack, and local method stack from several samples;
- Heap and stack;
- Method area and constant pool;