Home >Java >JavaBase >The internal structure and operating mechanism of JVM

The internal structure and operating mechanism of JVM

尚
forward
2020-06-16 16:23:282355browse

The internal structure and operating mechanism of JVM

1. Introduction to java memory composition: Heapandnon-heap(Non- heap)Memory

According to the official statement: "The Java virtual machine has a heap. The heap is the runtime data area, and the memory of all class instances and arrays is allocated from here.

The heap is created when the Java virtual machine starts." "The memory outside the heap in the JVM is called non-heap memory (Non-heap memory)." It can be seen that JVM mainly manages two types of memory: heap and non-heap.

Simply speaking, the heap is the memory accessible to Java code and is reserved for developers; the non-heap is the memory reserved for the JVM for its own use, so the memory required for method area, JVM internal processing or optimization ( Such as JIT compiled code cache), each class structure (such as runtime constant pool, field and method data) and the code of methods and constructors are in non-heap memory.

2. JVM memory area model

The internal structure and operating mechanism of JVM

1. Method area Also known as "permanent generation" and "non-heap", it is used to store class information, constants, and static variables loaded by the virtual machine. It is a memory area shared by each thread. The default minimum value is 16MB and the maximum value is 64MB. You can limit the size of the method area through the -XX:PermSize and -XX:MaxPermSize parameters.

Runtime constant pool: It is part of the method area, the main content of which comes from the loading of Class by the JVM.

In addition to the description information such as class version, fields, methods, interfaces, etc., the Class file also has a constant pool, which is used to store various symbol references generated by the compiler. This part will be discussed in After the class is loaded, it is placed in the runtime constant pool in the method area.

2. Virtual machine stack

describes the memory model of java method execution: when each method is executed, a "stack frame" is created for storage Local variable table (including parameters), operation stack, method exit and other information. The process from each method being called to the completion of execution corresponds to the process of a stack frame from being pushed into the stack to being popped out of the stack in the virtual machine stack. The declaration cycle is the same as the thread and is private to the thread.

The local variable table stores various basic data types known to the compiler (boolean, byte, char, short, int, float, long, double), object reference (reference pointer, not the object itself), of which 64 bits Long and double type data will occupy the space of 2 local variables, and other data types will only occupy 1 space.

The memory space required for the local variable table is allocated during compilation. When entering a method, it is completely determined how many local variables this method needs to allocate in the stack frame. The stack frame will not Change the size of the local variable table.

3. The local method stack

is basically similar to the virtual machine stack. The difference is that the virtual machine stack serves the java methods executed by the virtual machine, while the local method stack is Serve Native methods.

4. Heap

Also called java heap, GC heap is the largest memory area in the memory managed by the java virtual machine, and is also the memory shared by each thread. Region, created when the JVM starts. This memory area stores object instances and arrays (all new objects).

The size is set by the -Xms (minimum value) and -Xmx (maximum value) parameters. -Xms is the minimum memory requested when the JVM starts. The default is 1/64 of the physical memory of the operating system but less than 1G. -Xmx is the maximum memory that the JVM can apply for. The default is 1/4 of the physical memory but less than 1G. By default, when the free heap memory is less than 40%, The JVM will increase the Heap to the size specified by -Xmx. You can specify this ratio through -XX:MinHeapFreeRation=; when the free heap memory is greater than 70%, the JVM will reduce the size. The size of the heap reaches the size specified by -Xms. You can specify this ratio through XX:MaxHeapFreeRation=. For the running system, in order to avoid frequently adjusting the size of the Heap at runtime, usually -Xms and -Xmx The values ​​are set to the same.

Since collectors now use generational collection algorithms, the heap is divided into the new generation and the old generation. The new generation mainly stores newly created objects and objects that have not yet entered the old generation. The old generation stores objects that have survived multiple minor GCs.

New generation: New objects created by the program allocate memory from the new generation. The new generation consists of Eden Space and two Survivor Space of the same size. (usually also called S0 and S1 or From and To), the size of the new generation can be specified through the -Xmn parameter, and the Eden Space and -XX:SurvivorRation can also be adjusted The size of Survivor Space.

Old generation: used to store objects that have survived multiple times of new generation GC, such as cache objects. Newly created objects may also directly enter the old generation. There are two main situations:

1. Large objects can be set by startup parameters -XX:PretenureSizeThreshold=1024 (unit is bytes, default is 0) to represent that when it exceeds the size, it will not be allocated in the new generation, but directly in the new generation. Old generation allocation.

2. For large array objects, there are no external objects referenced in the array. The memory size occupied by the old generation is the value corresponding to -Xmx minus the value corresponding to -Xmn.

The internal structure and operating mechanism of JVM

Young Generation        即图中的Eden + From Space + To Space
Eden                    存放新生的对象
Survivor Space          有两个,存放每次垃圾回收后存活的对象
Old Generation          Tenured Generation 即图中的Old Space 
                        主要存放应用程序中生命周期长的存活对象

5. The program counter

is the smallest memory area , its function is the line number indicator of the bytecode executed by the current thread. In the virtual machine model, the bytecode interpreter selects the next bytecode that needs to be executed by changing the value of this counter when working. Basic functions such as instructions, branches, loops, exception handling, and thread recovery all rely on counters.

3. Direct memory

Direct memory is not part of the virtual machine memory, nor is it a memory area defined in the Java virtual machine specification. The newly added NIO in jdk1.4 introduces the IO method of channel and buffer. It can call the Native method to directly allocate off-heap memory. This off-heap memory is the local memory and will not affect the size of the heap memory.

4. 10 Key Points of Java Heap Memory

  1. Java heap memory is allocated by the operating system Part of the JVM's memory.

  2. When we create objects, they are stored in Java heap memory.

  3. To facilitate garbage collection, the Java heap space is divided into three areas , respectively called New Generation, Old Generation or Tenured Generation, and Perm Space.

  4. You can adjust the size of the Java heap space by using the JVM command line options -Xms, -Xmx, -Xmn. Don't forget to add "M" or "G" after the size to indicate the unit. For example, you can use -Xmx256m to set the maximum heap size to 256MB.

  5. You can use JConsole or Runtime.maxMemory(), Runtime.totalMemory(), Runtime.freeMemory() to view the size of the heap memory in Java.

  6. You can use the command "jmap" to obtain the heap dump and "jhat" to analyze the heap dump.

  7. Java heap space is different from stack space. Stack space is used to store call stacks and local variables.

  8. The Java garbage collector is used to reclaim the memory occupied by dead objects (objects that are no longer used) and then release it into the Java heap space.

  9. When you encounter java.lang.outOfMemoryError, don’t be nervous. Sometimes just increasing the heap space is enough, but if it occurs often, you need to check whether there is any problem in the Java program. There is no memory leak.

  10. Please use Profiler and Heap dump analysis tools to view the Java heap space and see how much memory is allocated to each object.

For more related knowledge, please pay attention to java basic tutorialcolumn

The above is the detailed content of The internal structure and operating mechanism of JVM. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:oschina.net. If there is any infringement, please contact admin@php.cn delete