Java virtual machine memory module

Write in front:

This article is an in-depth understanding of the reading notes of the third edition of the Java Virtual Machine (Zhou Zhiming), and the pictures are from electronic books

Between Java and C ++ there is a high wall surrounded by dynamic memory allocation and garbage collection technology. People outside the wall want to go in, but people inside the wall come up with it.

1 Overview

According to the "Java Virtual Machine Specification", the memory managed by the Java virtual machine includes the following areas:

Understanding the Java Virtual Machine Third Edition Figure 2-1

1.1 Program Counter (Program Counter Register)

The program counter is a small memory space, which can be regarded as the line number indicator of the bytecode executed by the current thread.

1.2 Java Virtual Machine Stack (Java Virtual Machine Stack)

When each method is executed, the Java virtual machine will simultaneously create a stack frame (Stack Frame) for storing local variables, operand stack, dynamic connection, method exit and other information.

1.3 Native Method Stacks

The local method stack is similar to the role of the virtual machine stack. The difference is that the virtual machine stack executes Java methods, and the local method stack serves the virtual machine using native methods.

1.4 Java 堆 (Java Heap)

The Java heap is the largest piece of memory managed by the virtual machine. In the Java world, almost all object instances are allocated on the heap. The description of the Java heap in the "Java Virtual Machine Specification" is: "All object instances and arrays should be allocated on the heap (The Heap is the runtime data area from which memory for all class instances and arrays is allocated) ", but with the advancement of real-time compilation technology, especially the escape analysis technology is becoming more and more powerful, on-stack allocation, scalar replacement optimization means that object instances are not allocated on the heap so absolute Too.

The Java heap is an area of ​​memory managed by the garbage collector, so it is sometimes called the "GC heap", and the size of the heap can be set by the parameters -Xmx and -Xms. '

1.5 Method Area

It is used to store data such as type information, constants, static variables, and code cache compiled by the real-time compiler that have been mixed by the virtual machine.

1.6 Runtime Constant Pool

The runtime constant pool (Runtime Constant Pool) is part of the method area. In addition to the class version, fields, methods, interfaces, and other description information in the Class file, there is also a constant pool table (Constant Pool Table), which is used to store various literals and symbol references generated during compilation, this part The content will be stored in the runtime constant pool of the method area after the class is loaded.

1.7 Direct Memory

The physical machine memory does not belong to the memory area defined in the "Java Virtual Machine Specification".

2 HotSpot virtual machine object exploration

2.1 Related concepts

Pointer collision (Bump The Pointer) : Assuming that the memory in the Java heap is absolutely regular, all used memory is placed on one side, free memory is placed on the other side, and a pointer is placed in the middle as an indicator of the demarcation point , The allocated memory is just to move the pointer to the free space for a distance equal to the size of the object. This allocation method is called "pointer collision";

Free List : If the memory in the Java heap is not regular, the used memory and the free memory are interleaved with each other, then there is no way to simply perform pointer collision, and the virtual machine must maintain a list , Record which memory blocks are available, find a large enough space from the list to allocate to the object instance during the allocation, and update the records on the list, this allocation method is called "free list" (Free List);

The choice of allocation method is determined by whether the Java heap is regular, and whether the Java heap is regular is determined by whether the garbage collector used has the capacity of space compaction (Compact).

In the HotSpot virtual machine, the layout of objects in the heap memory can be divided into three parts: object header (Header), instance data (Instance Data), and alignment padding (Padding).

Object header (Header) : The object header contains two types of information, the first type is to store the runtime data of the object itself, such as hash code, GC generation age, lock status flag, lock held by the thread, biased thread ID, biased time Poke, etc., also known as "Mark Word"; the second type is a type pointer, just a pointer to its type metadata, through this pointer virtual machine to confirm that this object is an instance of that class.

Instance Data : Valid information that the object really stores.

Align padding (Padding) : optional , placeholder role.

 

 

 

Guess you like

Origin www.cnblogs.com/w-m-m/p/12733116.html