Deep understanding of java virtual machine

The article is reproduced from http://blog.csdn.net/u010425776/article/details/51170118

JVM memory model

The memory space of the Java Virtual Machine (Java Virtual Machine=JVM) is divided into five parts, namely: 
1. Program counter 
2. Java virtual machine stack 
3. Local method stack 
4. Heap 
5. Method area.

These five areas are described in depth below. 

1. Program Counter

1.1. What is the program counter?

The program counter is a small memory space that can be thought of as a line number indicator of the bytecode being executed by the current thread. That is to say, the program counter records the address of the bytecode instruction being executed by the current thread. 
Note: However, if the current thread is executing a native method, then the program counter is empty. 

1.2. The role of the program counter

The program counter serves two purposes:

  1. The bytecode interpreter reads the instructions in sequence by changing the program counter, so as to realize the flow control of the code, such as: sequential execution, selection, loop, exception handling.
  2. In the case of multithreading, the program counter is used to record the position of the current thread execution, so that when the thread is switched back, it can know where the thread ran last time. 

1.3. Features of the Program Counter

  1. is a small storage space
  2. Thread private. Each thread has a program counter.
  3. is the only memory region that does not get an OutOfMemoryError.
  4. Lifecycles are created as threads are created and they die as threads end. 

2. Java Virtual Machine Stack (JVM Stack)

2.1. What is the Java Virtual Machine Stack?

The Java virtual machine stack is a memory model that describes the running process of Java methods. 
The Java virtual machine stack will create an area called "stack frame" for each Java method that is about to run. This area is used to store some information that the method needs during the running process, including:

  1. The local variable table 
    stores basic data type variables, reference type variables, and returnAddress type variables.
  2. operand stack
  3. dynamic link
  4. Method export information
  5. Wait

When a method is about to be executed, the Java virtual machine stack will first create a "stack frame" for the method in the Java virtual machine stack. The stack frame contains the local variable table, operand stack, dynamic link, method exit information, etc. When the method needs to create a local variable during the running process, the value of the local variable is stored in the local variable table of the stack frame. 
When this method is executed, the stack frame corresponding to this method will be popped off the stack and the memory space will be released.

Note: People often say that Java's memory space is divided into "stack" and "heap". The stack stores local variables and the heap stores objects. 
This sentence is not entirely correct! The "heap" here can be understood in this way, but the "stack" here only represents the local variable table part of the Java virtual machine stack. The real Java virtual machine stack is composed of stack frames, and each stack frame has: local variable table, operand stack, dynamic link, method exit information. 

2.2. Features of Java Virtual Machine Stack

  1. The local variable table is created when the method is executed, along with the creation of the stack frame. Moreover, the size of the local variable table is determined at compile time, and it only needs to allocate a predetermined size when it is created. In addition, the size of the local variable table does not change while the method is running.
  2. There are two exceptions in the Java virtual machine stack: StackOverFlowError and OutOfMemoryError. 
    a) StackOverFlowError: 
    If the memory size of the Java virtual machine stack does not allow dynamic expansion, then when the depth of the thread request stack exceeds the maximum depth of the current Java virtual machine stack, a StackOverFlowError exception will be thrown. 
    b) OutOfMemoryError: 
    If the memory size of the Java virtual machine stack allows dynamic expansion, and the memory runs out when the thread requests the stack, it can no longer be dynamically expanded, and an OutOfMemoryError exception is thrown.
  3. The Java virtual machine stack is also thread-private, and each thread has its own Java virtual machine stack, which is created as the thread is created, and dies as the thread dies.

Note: What are the similarities and differences between StackOverFlowError and OutOfMemoryError?  
StackOverFlowError indicates that the stack applied by the current thread exceeds the preset maximum depth of the stack, but there may be a lot of memory space. 
The OutOfMemoryError means that when the thread applies for the stack, it is found that the stack is full and the memory is all used up. 

3. Native method stack

3.1. What is the native method stack?

The function implemented by the native method stack is similar to that of the Java virtual machine stack, except that the native method area is the memory model in which the native method runs.

When a native method is executed, a stack frame is also created in the native method stack to store the local variable table, operand stack, dynamic link, and exit information of the native method.

After the method is executed, the corresponding stack frame will be popped off the stack and the memory space will be released.

Also throws StackOverFlowError and OutOfMemoryError exceptions.

4. Heap

4.1. What is a heap?

The heap is the memory space used to store objects. 
Almost all objects are stored on the heap. 

4.2. Characteristics of the heap

  1. Threads share 
    only one heap for the entire Java virtual machine, and all threads access the same heap. The program counter, Java virtual machine stack, and native method stack are all for one thread.
  2. Created at virtual machine startup
  3. The main place for garbage collection.
  4. It can be further subdivided into: new generation, old generation. 
    The new generation can be divided into: Eden, From Survior, To Survior. 
    Different areas hold objects with different life cycles. In this way, different garbage collection algorithms can be used according to different areas, which is more targeted and more efficient.
  5. The size of the heap can be fixed or expandable, but the size of the mainstream virtual machine heap is expandable, so when a thread requests to allocate memory, but the heap is full, and the memory is full and cannot be expanded, an OutOfMemoryError is thrown. 

5. Method area

5.1. What is a method area?

The Java Virtual Machine specification defines the method area as a logical part of the heap. 
The method area stores class information, constants, static variables, and code compiled by the just-in-time compiler that have been loaded by the virtual machine. 

5.2. Features of the method area

  1. The thread-shared 
    method area is a logical part of the heap and, like the heap, is shared by threads. There is only one method area in the entire virtual machine.
  2. The information in the permanent generation 
    method area generally needs to exist for a long time, and it is a logical partition of the heap. Therefore, using the heap division method, we call the method area the old generation.
  3. Memory recovery efficiency is low 
    . The information in the method area generally needs to exist for a long time, and only a small amount of information may be invalid after the memory is recovered once. 
    The main goals of memory reclamation for the method area are: reclamation of the constant pool and unloading of types.
  4. The Java virtual machine specification has looser requirements on the method area. 
    Like the heap, a fixed size is allowed, as well as an expandable size, and no garbage collection is implemented. 

5.3. What is the runtime constant pool?

Three kinds of data are stored in the method area: class information, constants, static variables, and code compiled by the real-time compiler. where constants are stored in the runtime constant pool.

We generally declare a constant in a class through public static final. After this class is compiled, a Class file is generated, and all the information of this class is stored in this class file.

When the class is loaded by the Java virtual machine, the constants in the class file are stored in the runtime constant pool of the method area. And during runtime, new constants can be added to the constant pool. For example, the intern() method of the String class can add string constants to the constant pool during runtime.

When some constants in the runtime constant pool are not referenced by objects and are not referenced by variables, they need to be collected by the garbage collector. 

6. Direct memory

Direct memory is memory other than the Java virtual machine, but may also be used by Java.

A channel and buffer-based IO method is introduced in NIO. It can directly allocate memory outside the Java virtual machine by calling a native method, and then directly operate the memory through a DirectByteBuffer object stored in the Java heap, without first copying the data in the external memory to the heap and then operating, thereby improving efficiency of data manipulation.

The size of direct memory is not controlled by the Java virtual machine, but since it is memory, an OOM exception will be thrown when the memory is insufficient. 

In summary

  1. There are two "stacks" in the memory model of the Java virtual machine, namely: the Java virtual machine stack and the native method stack. 
    The functions of the two "stacks" are similar, and they are both memory models of the method running process. And the two "stacks" have the same internal structure and are both thread-private. 
    It's just that the Java virtual machine stack describes the memory model of the running process of Java methods, and the native method stack is the memory model that describes the running process of Java native methods.
  2. There are two "heaps" in the memory model of the Java virtual machine, one is the original heap and the other is the method area. The method area is essentially a logical part of the heap. Objects are stored in the heap, and class information, constants, static variables, and code compiled by the real-time compiler are stored in the method area.
  3. The heap is the largest memory area in the Java virtual machine, and it is also the main working area of ​​the garbage collector.
  4. The program counter, Java virtual machine stack, and native method stack are private to threads, that is, each thread has its own program counter, Java virtual machine stack, and native method area. And their life cycle is the same as the thread they belong to. 

  1. The heap and method area are shared by threads, and there is only one heap and one method stack in the Java virtual machine. It is created when the JVM starts, and is destroyed when the JVM stops.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325565543&siteId=291194637