Preface
The JAVA memory area is mainly composed of program counters, java virtual machine stacks, local method stacks, Java heaps, method areas and runtime constant pools. This article will introduce you in detail about the division and exceptions of Java memory areas. I won’t say much below, let’s take a look at the detailed introduction together.
Runtime data area
When the JVM runs Java programs, it divides the memory into several different data areas.
Program Counter
Thread private. It can be regarded as a line number indicator of the bytecode executed by the current thread. The work of the bytecode interpreter is to read the next bytecode instruction to be executed by changing this count value.
Multithreading is achieved by switching threads in turn and allocating processor execution time. At any moment, a kernel can only execute instructions in one thread. In order to restore to the correct execution position after thread switching, each thread needs an independent program counter. This is what I mentioned at the beginning of "thread private". If the method that the thread is executing is a Java method, the counter records the instruction address of the virtual machine bytecode; if it is a Native method, the counter value is empty. The program counter is the only area where no OOM (OutOfMemoryError) situation is specified in the Java virtual machine specification.
Java Virtual Machine Stack
Threads are private, with the same life cycle as threads. The Java virtual machine stack describes the memory model of Java methods: each method will create a stack frame when it is executed, storing local variable tables, operand stacks, dynamic links, and method exit information. From the call to the end, each method corresponds to the stack entry and exit process of this stack frame in the virtual machine stack. The local variable table saves various basic data types (int, double, char, byte, etc.), object reference (not the object itself), and returnAddress type (pointing to a bytecode address).
Two possible exceptions in this area:
Local method stack
The above virtual machine stack executes Java method services for the JVM, and the local method executes Native service. Others are similar to the virtual machine stack, and StackOverflowError and OutOfMemoryError will also be thrown.
Java heap
The commonly referred to as "stack memory" and "heap memory", the former refers to the virtual machine stack, and the latter refers to the Java heap. The Java heap is shared by threads. Created when the virtual machine starts.
The role of the Java heap is to store object instances. The Java heap can be in a physically discontinuous memory space, and only requires logically continuous.
Method area
The area shared by threads. Stores data such as class information, constants, static variables, code compiled by the compiler, etc. that have been loaded by the virtual machine. When the method area cannot meet the memory allocation requirements, an OutOfMemoryError is thrown.
Runtime constant pool
The runtime constant pool is part of the method area. C is used to store various literal constants and symbolic references generated during the compilation period, and will be stored in the runtime constant pool that enters the method area after class loading. The Java language does not require constants to be generated only during the compilation period. In other words, new constants can be placed in during the run period.
Direct memory
Direct memory is not part of the virtual machine runtime data area, nor is it a memory area. The allocation of direct memory of the native machine will not be limited by the size of the Java heap, but it is memory after all. If the sum of each memory area is greater than the physical memory limit, an OutOfMemoryError will still appear.
Object creation process
The virtual machine encounters a "new" instruction:
Memory layout of objects
The layout of objects stored in memory can be divided into 3 areas:
Object header: stores the object's own runtime data, such as hash code, GC generation age, lock status flag, lock held by threads, biased thread ID, etc. Another part is a type pointer, that is, a pointer to the object's class metadata. The virtual machine uses this pointer to determine which class instance the object belongs to.
Instance data: the truly valid information of the object, the content of various types of field defined in the program;
Alignment supplement: non-essential, the role of occupants.
Object access positioning
Java programs operate instance objects on the heap through references on the stack. for example
Person p = new Person();
Here p is a reference, and the Person object produced by new is an instance.
This reference does not specify how to locate and access the specific location of objects in the heap. There are two mainstream access methods:
handle. A piece of memory will be divided into the Java heap as a handle pool, which references the handle address of the object, and the handle contains the object instance data and type data. The advantage is that when the object is moved, you only need to change the address in the handle, and the reference itself does not need to be modified.
Direct pointer. The object address is directly stored in the reference. The advantage is that the speed is faster, and because the reference directly represents the address of the instance object, a pointer positioning operation is saved. This is exactly how Sun HotSpot uses.
Summarize
The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support to Wulin.com.