Reprinted: Java Virtual Machine: runtime memory data area object memory allocation and access

Reproduced: https://blog.csdn.net/a745233700/article/details/80291694 (although most of the content is actually a deep understanding jvm virtual machine in this book, but finishing very fast hardware)

Runtime data area in a memory java   

After each Java program starts, you will get a JVM instance, used to manage memory. Java virtual machine memory division during the execution of the Java program in which it will manage for a number of different data areas. The use of these areas have words, as well as create and destroy times. Java virtual machine memory management will include the following runtime data area:

 

 

1, the program counter: The program counter is a small space, a thread of execution, bytecode current line number indicator, bytecode interpreter job is to select the word by changing the value of a counter to be executed bytecode instructions. (Thread private, each thread has a separate program counter, independently of each other, isolated storage)

2, Java virtual machine stack: thread private, life cycle and the life cycle of the same thread. It describes a method executed java memory model, each method when executed creates a stack frame, the frame is stored in the form of local method call state, local variable table for storing information, the operand stack, dynamic link, the method exports . Each method call from the execution is completed, a stack frame corresponds to a procedure to push the stack in a virtual machine.

 

 

3, native method stacks: the role of native method stacks and stacks of virtual machine role is very similar, except that the java virtual machine to perform a method stack virtual machine services, native method stacks, the virtual machine to use a method of Native service.

4, Java heap: the largest area occupied by memory is created when the virtual machine starts, used to store an object instance, can be divided into the old and the new generation's.

5, method area: means for storing class information of the virtual machine is loaded, a constant, static variables, even if the compiler code and other data.

 

 Two memory allocation:       

First class file is loaded into the method area (basic information storage class static variable, the compiled code and other information), then a new category for this class in the Java heap memory allocation (mainly to allocate memory field properties). When you call a method, according to the reference method to find the corresponding method in the method area, and then assign a stack frame for this method of storing information in a Java virtual machine stack.

1、new对象创建的过程:

(1)首先将去检查这个指令的参数是否能在常量池中定位到一个类的符号引用;

(2)检查这个符号引用代表的类是否已被加载、解析和初始化;

(3)如果没有,那必须先执行相应的类加载过程;

(4)如果有,虚拟机将为新生对象分配内存,并使用CAS保证操作原子性,分配方式有:指针碰撞和空闲列表;

(5)虚拟机将分配到的内存空间都初始化为零值,这一步保证了对象的实例字段在Java代码中可以不赋初始值就可以直接使用;

(6)对对象进行必要的设置;

(7)执行<init>方法,把对象按照程序员意愿进行初始化。

2、分配方式:

(1)指针碰撞:如果java堆是绝对规整的,所有用过的内存都放在一边,所有没用过的内存存放在另一边,中间存放一个指针作为分界点指示器。分配内存时,将指针从用过的内存区域向空闲内存区域移动等距离区域。

(2)空闲列表:如果java不是规整的,这时,虚拟机就必须维护一张列表,列表上记录了可用的内存块,在分配内存时,从列表上找到一个足够大的连续内存块分配给对象,并更新列表上的记录。

3、对象的内存布局:

在HotSpot中,对象的内存布局分成三部分:对象头,实例数据,对齐填充。

(1)对象头:包括两部分的信息:

        第一部分用于存储对象自身的运行时数据,如哈希码,GC代年龄,锁状态标志等。

        第二部分是对象指向它的类元数据的类元指针,虚拟机通过这个指针来确定这个对象是哪个类的实例。如果对象是一个Java数组,那对象头中还必须有一块用于记录数组长度的数据。

(2)实例数据:是对象真正存储的有效信息,是在程序代码中所定义的各种类型的字段内容,相同宽度的字段会被分配到一起。

(3)对齐填充:并不是必然存在的,仅起着占位符的作用。

4、对象的访问定位:

    Java程序通过栈上的reference数据来操作堆上的具体对象。目前访问对象主要有两种方式:通过句柄访问对象和直接指针访问对象。

(1)通过句柄访问对象:

 

 

(2)通过直接指针访问对象:

 

 

(3)优劣对比:

   ① 使用句柄,reference中存储的是稳定的句柄地址,在对象被移动时只会改变句柄中的实例数据指针,而reference本身不需要修改。

    ②直接指针,速度快,节省一次指定定位的开销。

Guess you like

Origin www.cnblogs.com/houzheng/p/11274161.html