Java Memory Model and partition

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/u013541140/article/details/97246001

For a Java developer to understand the memory region stored in Java objects, variables, etc. it is very important. This article will explain the overall memory model and zoning Java virtual machine.

Java virtual machine to manage memory data is divided into several distinct regions, as shown in FIG.

The following were deployed explain the function of each area.

1. Java heap

Java heap is shared by all threads of a memory area is created when the virtual machine starts. The sole purpose of this memory area is stored object instances , all instances of objects and arrays to be allocated on the heap. Java heap is the main area managed by the garbage collector, and therefore often also called " GC heap ."

From the perspective of memory recovery, due to current collector basically using generational collection algorithm, so Java heap can also be subdivided into the new generation and the old year ; and then there is little detailed Eden space, the From Survivor space and To Survivor space.

From the perspective of memory allocation point of view, shared by the threads of the Java heap may be divided into multiple threads private allocate a buffer (the Thread Local Allocation Buffer, TLAB ).

Under standard Java virtual machine, Java stack may not be in contiguous physical memory space, as long as can be logically contiguous. When implementing either can be implemented into fixed-size, it may also be extended, but the current mainstream virtual machines are in accordance scalable to achieve.

If there is no complete examples in the heap memory allocation, and the stack can no longer expand, it will throw OutOfMemoryError exception.

2. The method area

The method area and the Java heap is the same as all the threads share the memory area for storing virtual machine has been loaded class information , constants , static variables and the real-time compiler to compile the code and other data. Although Java Virtual Machine Specification as described in the method area is a logical part of the heap, but it has an alias called Non-Heap ( non-pile ), is separated from the object Java heap.

Java Virtual Machine Specification limitation on the method very relaxed area, in addition to and do not require continuous as the Java heap memory can be selected and fixed size or may be expanded, but also can choose not to implement garbage collection . In contrast, garbage collection behavior in this area is relatively small appearance, but not the data into the area just as a permanent method of generation of the same name "permanent" exists. Garbage collection target this area is mainly for recycling and unloading of constant pool types , in general, the recovery of the region, "achievement" relatively unsatisfactory, especially the type of unloading, conditions can be quite harsh, but this part of the region the recovery is indeed necessary.

Runtime constant pool is part of the method area for storing various constants and literal compile generated reference symbols .

Under the Java Virtual Machine specification, when the method of memory allocation area can not meet demand, it will throw OutOfMemoryError exception.

3. Java Virtual Machine stack

Java virtual machine stack is the thread private , its life cycle and the same thread. Is a virtual machine stack Java memory model described method performed: Each method also creates a stack frame for the implementation is going to be stored in the local variable table , stack operation , Dynamic Link , the method returns the address information. Each method is called from procedure until completion of the execution, a stack frame corresponds to a virtual machine from the stack to the stack during the stack, as shown in FIG.

Local variable table for storing method parameters and local variables defined within the method , the size has been determined during the code compilation, the method does not change during operation. Local variable table to a variable slot ( Slot ) is the smallest unit of storage, each capable of storing a Slot boolean, byte, char, short, int, float, reference ( object reference) and returnAddress (address pointing to a byte code instruction) type of 32-bit data, 64-bit data types for long and double, the high order virtual machines assigned an aligned manner two consecutive Slot space.

When performing the method, if the method is an instance, i.e., non-static method, the local variable table Slot 0 of the object instance stored reference default and can be accessed by a keyword in this method, the process parameters according to the parameter list order, from Slot allocation starts a process for the rest of the internal variables Slot allocation order of definition.

在Java虚拟机规范中,对这个区域规定了两种异常状况:如果线程请求的栈深度大于虚拟机所允许的深度,将抛出StackOverflowError异常;如果虚拟机栈可以动态扩展,当扩展时无法申请到足够的内存时会抛出OutOfMemoryError异常。

4. 本地方法栈

本地方法栈与虚拟机栈所发挥的作用是非常相似的,其区别是虚拟机栈为虚拟机执行Java方法(也就是字节码)服务,而本地方法栈则为虚拟机使用到的Native方法服务

虚拟机规范中对本地方法栈中的方法使用的语言、使用方式与数据结构并没有强制规定,因此具体的虚拟机可以自由地实现它,甚至有的虚拟机(如Sun HotSpot虚拟机)直接就把本地方法栈和虚拟机栈合二为一。

与虚拟机栈一样,本地方法栈区域也会抛出StackOverflowErrorOutOfMemoryError异常。

5. 程序计数器

程序计数器可以看作是当前线程所执行的字节码的行号指示器。在虚拟机的概念模型里,字节码解释器工作时就是通过改变这个计数器的值来选取下一条需要执行的字节码指令,分支、循环、跳转、异常处理和线程恢复等基础功能都需要依赖这个计数器来完成。

由于Java虚拟机的多线程是通过线程轮流切换并分配处理器执行时间的方式来实现的,在任何一个确定的时刻,一个处理器只会执行一条线程中的指令。因此,为了线程切换后能恢复到正确的执行位置,每条线程都需要有一个独立的程序计数器,各条线程之间计数器互不影响,独立存储。

如果线程正在执行的是一个Java方法,这个计数器记录的是正在执行的虚拟机字节码指令的地址;如果正在执行的是Native方法,这个计数器值则为空(Undefined)。

此内存区域是唯一一个在Java虚拟机规范中没有规定任何OutOfMemoryError情况的区域。

 

最后,欢迎加我微信 jimmysun8388 一起交流学习!

微信二维码
微信二维码

加好友时请注明申请理由,例如「姓名/昵称 + Android 交流」,示例:张三 Android 交流。

Guess you like

Origin blog.csdn.net/u013541140/article/details/97246001