Java Virtual Machine-Java memory area, java-java
A Brief Introduction to the memory area of Java:
- Data Area During Running
- HotSpot Virtual Machine objects
I. Overview
2. Data area 2.1 program counters during running
Program Counter Register represents the row number indicator of the bytecode executed by the current thread, PC in the operating system. These two concepts are one. During execution, this counter is changed to select the next bytecode command to be executed. Java supports multithreading, so each thread has its own exclusive PC, which is similar to an attribute in the thread table of the operating system.
2.2 Java Virtual Machine Stack
Like a PC, the thread is private, and the thread's lifecycle is the same. The Virtual Machine Stack describes the Memory Model of Java method execution. Each method creates a Stack frame during execution and uses and stores local variables, operations, dynamic links, method exits, and other information. Local variables are placed in the stack's local variable table, which stores the basic data type (int) known by the compiler. Float, string ..) And object reference ).
2.3 local method Stack
Native Method Stack is similar to Java Virtual Machine Stack, but it is used to serve the Native Method called by virtual machines.
2.4 Java heap
The Java heap is shared by all threads. It is the memory of the Java program. It is created when the VM is started. The purpose of this memory area is to store the object instance. Java heap is the main area of GC management.
2.5 method Zone
Method Area: The Area shared by each thread. It is used to store data such as class information, constants, static variables, and Code Compiled by the real-time compiler,
A special area in the method area is called the Runtime Constant Pool, which is used to store various literal and symbolic references (strings) generated during compilation)
2.6 Direct Memory
The direct memory is not part of the Virtual Machine runtime, but will affect the Java heap size.
3. create virtual machine object 3.1 in HOTSPOT
Java is an object-oriented language, and object creation is completed by the new keyword. Main steps:
3.2 Object Memory Layout
Objects in the memory are divided into three parts:
3.3 Access and positioning of Objects
Objects are created to use objects. java programs need to operate on specific objects through the reference data stored on the stack. The reference type is a reference pointing to objects, this song does not define how to locate and access the specific location of objects in the heap. Therefore, the object access method depends on the Virtual Machine implementation method. Currently, there are two mainstream methods:
1. access objects using handles
The advantage of using a handle is that the handle is stable. when an object is moved (after GC), only the instance data pointer in the handle is changed, and the Reference capability does not need to be modified.
2. access objects with pointers
The biggest advantage of using direct pointers is faster, which saves the time overhead of one pointer location.