jvm series 1--jvm memory area

(1) Basic concepts of jvm:

JVM is an imaginary computer that can run Java code, including a set of bytecode instructions, a set of registers, a stack,

A garbage collection, heap and a storage method field. The JVM runs on the operating system and has no direct connection with the hardware.
interaction.
(2) Operation process:
        We all know that Java source files, through the compiler, can produce corresponding .Class files, which are bytecode files.
The bytecode file is compiled into machine code on a specific machine through the interpreter in the Java virtual machine.
That is as follows:
① Java source file --> Compiler --> Bytecode file
② Bytecode file-->JVM-->Machine code
The interpreter of each platform is different, but the virtual machine implemented is the same. This is why Java can
The reason for cross-platform is that when a program starts running, the virtual machine starts to be instantiated. When multiple programs start,
Multiple virtual machine instances exist. If the program exits or is closed, the virtual machine instance will die and data will be lost between multiple virtual machine instances.
Can be shared.
 
2.1.Threads _
The thread mentioned here refers to a thread entity during program execution. The JVM allows an application to execute multiple threads concurrently.
Java threads in Hotspot JVM have a direct mapping relationship with native operating system threads . When thread local storage, buffering
After the buffer allocation, synchronization objects, stack, program counter, etc. are prepared, an operating system native thread is created .
The Java thread ends and the native thread is recycled. The operating system is responsible for scheduling all threads and assigning them to any available
on the CPU used. When the native thread is initialized, the run() method of the Java thread will be called. When the thread ends,
All resources of native threads and Java threads will be released.
The system threads running in the background of Hotspot JVM mainly include the following:
2.2.JVM memory area
 
The JVM memory area is mainly divided into thread private area [program counter, virtual machine stack, local method area] and thread shared area.
Domain [JAVA heap, method area], direct memory.
The life cycle of the thread private data area is the same as that of the thread, and is created/destroyed depending on the start/end of the user thread (in Hotspot
Within the VM , each thread is directly mapped to the local thread of the operating system, so the storage/absence of this part of the memory area follows the local thread.
life/death correspondence).
The thread shared area is created/destroyed with the startup/shutdown of the virtual machine .
Direct memory is not part of the JVM runtime data area, but it is also used frequently: NIO provided by JDK 1.4
Provides an IO method based on Channel and Buffer. It can use the Native function library to directly allocate off-heap memory, and then use
The DirectByteBuffer object operates as a reference to this memory (see: Java I/O extension), thus avoiding the need to
Data is copied back and forth between the heap and the Native heap, so performance can be significantly improved in some scenarios .

2.2.1. Program counter ( thread private )

        A smaller memory space is the line number indicator of the bytecode executed by the current thread . Each thread must have an independent program counter. This type of memory is also called "thread-private" memory. If the java method is being executed, the counter records the address of the virtual machine bytecode instruction (the address of the current instruction). If it is still a Native method, it will be empty. This memory area is the only area in the virtual machine that does not have any OutOfMemoryError conditions specified.

2.2.2. Virtual machine stack ( thread private )
It is a memory model that describes the execution of Java methods. Each method will create a stack frame (Stack Frame) while executing.
Used to store information such as local variable tables, operand stacks, dynamic links, method exits, etc. Each method is called until execution is completed
The process corresponds to the process from pushing a stack frame into the virtual machine stack to popping it out.
Stack frame (Frame) is a data structure used to store data and partial process results. It is also used to handle dynamic links.
(Dynamic Linking), method return value and exception dispatch (Dispatch Exception). Stack frames are created with method calls
Created and destroyed when the method ends - regardless of whether the method completes normally or abnormally (an exception that was not caught within the method was thrown)
(usually) are counted as the end of the method.

2.2.3. Local method area ( thread private )

The local method area has a similar function to the Java Stack . The difference is that the virtual machine stack serves the execution of Java methods, while the local method stack serves

Native method service , if a VM implementation uses the C-linkage model to support Native calls, then the stack will be a
C stack, but HotSpot VM directly combines the local method stack and the virtual machine stack into one.
 
2.2.4. Heap ( Heap- thread sharing) - runtime data area
 
It is a memory area shared by threads. The created objects and arrays are stored in the Java heap memory and are also processed by the garbage collector.
The most important memory area for garbage collection . Since modern VMs use generational collection algorithms , the Java heap is okay from a GC perspective.
It is subdivided into: new generation ( Eden area , From Survivor area and To Survivor area ) and old generation.
 
2.2.5. Method area / permanent generation (thread sharing)
 
That is what we often call the permanent generation (Permanent Generation) , which is used to store class information , constants , and static files loaded by the JVM.
Data such as state variables , code compiled by just-in-time compiler, etc. HotSpot VM extends GC generational collection to the method area, that is, using Java
The permanent generation of the heap is used to implement the method area , so that HotSpot's garbage collector can manage this part of the memory like the Java heap.
Instead of developing a special memory manager for the method area (the main goal of permanent memory recycling is the recycling and type of the constant pool
offloading , so the gains are generally small).
The Runtime Constant Pool is part of the method area. In addition to the class version in the Class file
In addition to information such as descriptions of files, fields, methods, interfaces, etc., there is also a constant pool (Constant Pool Table), which is used to store various literals and symbol references generated during compilation. This part of the content will be stored after the class is loaded. to the runtime constant pool in the method area. The Java virtual machine has strict regulations on the format of each part of the Class file (naturally including the constant pool). What kind of data each byte is used to store must comply with the specification requirements, so that it will be recognized by the virtual machine. Load and execute.

Guess you like

Origin blog.csdn.net/weixin_38340874/article/details/122059066