JVM from entry to the proficiency (B): JVM runtime memory and memory area

JVM memory area

b672b987b5834b3f8fa563d7b8e7e77a


JVM thread private memory area is divided into regions [program counter, stack virtual machines, local area] method, thread shared area [JAVA heap, the method area], direct memory.

Thread private data area of ​​the life cycle of the same thread, dependent on the user thread start / end and create / destroy (in HotspotVM, each thread is directly mapped to the local multithreaded operating system, so keep this memory area / no follow local Health threads / death corresponds).

Threads share the region with the launch of the virtual machine on / off and creation / destruction.

Memory is not a direct part of the JVM run-time data area, but also frequently use: introduced in NIO JDK 1.4 provides a way Channel-based IO with Buffer, it can use the Native library directly allocated heap memory outside, then use this memory object as a reference to operate (see: Java I / O expansion), thus avoiding the Java heap and stack Native replicate data, thus in some scenarios can significantly improve performance.

7588732ffd3546a1afd113ba3d439781


1. The program counter (thread-private)

A smaller memory space, is the line number indicator byte code executed by the current thread, each thread should have a separate program counter, this type of memory is also known as "thread private" memory.

Java method being executed, then the counter is recorded the address (address of the current instruction) virtual machine bytecode instructions. If Native or method, or null.

This memory is the only one not specify any area OutOfMemoryError situation in a virtual machine.

2. VM stack (thread-private)

Java memory model is a description of a method of performing, for each frame, which will create a stack (Stack Frame) while performing the local variable table for storing information, the operand stack, dynamic link, the method exports. Each method until the completion of the execution procedure is called, a corresponding stack frame to push the stack in a virtual machine process stack.

栈帧( Frame)是用来存储数据和部分过程结果的数据结构,同时也被用来处理动态链接(Dynamic Linking)、 方法返回值和异常分派( Dispatch Exception)。栈帧随着方法调用而创建,随着方法结束而销毁——无论方法是正常完成还是异常完成(抛出了在方法内未被捕获的异常)都算作方法结束。

9d104f49f82243b28865d7ba3d0fe8c4


3.本地方法区(线程私有)

本地方法区和 Java Stack 作用类似, 区别是虚拟机栈为执行 Java 方法服务, 而本地方法栈则为Native 方法服务, 如果一个 VM 实现使用 C-linkage 模型来支持 Native 调用, 那么该栈将会是一个C 栈,但 HotSpot VM 直接就把本地方法栈和虚拟机栈合二为一。


4.堆(Heap-线程共享)-运行时数据区

是被线程共享的一块内存区域,创建的对象和数组都保存在 Java 堆内存中,也是垃圾收集器进行垃圾收集的最重要的内存区域。由于现代 VM 采用分代收集算法, 因此 Java 堆从 GC 的角度还可以细分为: 新生代(Eden 区、From Survivor 区和 To Survivor 区)和老年代。


5.方法区/永久代(线程共享)

即我们常说的永久代(Permanent Generation), 用于存储被 JVM 加载的类信息常量静态变量即时编译器编译后的代码等数据. HotSpot VM把GC分代收集扩展至方法区, 即使用Java堆的永久代来实现方法区, 这样 HotSpot 的垃圾收集器就可以像管理 Java 堆一样管理这部分内存,而不必为方法区开发专门的内存管理器(永久带的内存回收的主要目标是针对常量池的回收类型的卸载, 因此收益一般很小)。

运行时常量池(Runtime Constant Pool)是方法区的一部分。Class 文件中除了有类的版本、字段、方法、接口等描述等信息外,还有一项信息是常量池(Constant Pool Table),用于存放编译期生成的各种字面量和符号引用,这部分内容将在类加载后存放到方法区的运行时常量池中。 Java 虚拟机对 Class 文件的每一部分(自然也包括常量池)的格式都有严格的规定,每一个字节用于存储哪种数据都必须符合规范上的要求,这样才会被虚拟机认可、装载和执行。


JVM 运行时内存

Java 堆从 GC 的角度还可以细分为: 新生代(Eden 区、From Survivor 区和 To Survivor 区)和老年

代。

243b3d6ffaab4df89cdbb63154bd7baf


1. 新生代

是用来存放新生的对象。一般占据堆的 1/3 空间。由于频繁创建对象,所以新生代会频繁触发MinorGC 进行垃圾回收。新生代又分为 Eden 区、ServivorFrom、ServivorTo 三个区。

1.1. Eden 区

Java 新对象的出生地(如果新创建的对象占用内存很大,则直接分配到老年代)。当 Eden 区内存不够的时候就会触发 MinorGC,对新生代区进行一次垃圾回收。

1.2. ServivorFrom

上一次 GC 的幸存者,作为这一次 GC 的被扫描者。

1.3. ServivorTo

保留了一次 MinorGC 过程中的幸存者。

1.4. MinorGC 的过程(复制->清空->互换)

MinorGC 采用复制算法。

1:eden、servicorFrom 复制到 ServicorTo,年龄+1

首先,把 Eden 和 ServivorFrom 区域中存活的对象复制到 ServicorTo 区域(如果有对象的年龄以及达到了老年的标准,则赋值到老年代区),同时把这些对象的年龄+1(如果 ServicorTo 不够位置了就放到老年区);

2:清空 eden、servicorFrom

然后,清空 Eden 和 ServicorFrom 中的对象;

3:ServicorTo 和 ServicorFrom 互换

最后,ServicorTo 和 ServicorFrom 互换,原 ServicorTo 成为下一次 GC 时的 ServicorFrom区。

2. 老年代

主要存放应用程序中生命周期长的内存对象。

老年代的对象比较稳定,所以 MajorGC 不会频繁执行。在进行 MajorGC 前一般都先进行了一次 MinorGC,使得有新生代的对象晋身入老年代,导致空间不够用时才触发。当无法找到足够大的连续空间分配给新创建的较大对象时也会提前触发一次 MajorGC 进行垃圾回收腾出空间。

MajorGC using clear labeling algorithm: first scans once all the old era, marked survival of the subject, and then recovering the object is not marked. MajorGC relatively long time-consuming, because you want to scan recycling. MajorGC will produce memory fragmentation, in order to reduce memory consumption, we generally need to be merged or marked for next direct distribution. When the old year was full fit and they will throw OOM (Out of Memory) exception.

3. Permanent Generation

Refers to a permanent memory storage area, the main information is stored and Class Meta (metadata), is placed permanently Class region when loaded, and it is different and the storage area for example, the main program will not run on the GC permanent area clean. So this also led to permanent generation area will be loaded with the increase of the Class of fullness and eventually throws OOM exception.

3.1. JAVA8 metadata

In Java8, permanent behalf has been removed, the region "metadata area" (element space) is called by a substituent. Yuan and permanent nature of space to like, the biggest difference between the yuan and the permanent space on behalf of that: Yuan is not space in a virtual machine, but the use of local memory. Therefore, by default, the size of the element space only by the local memory limit. Class metadata into nativememory, static variables and class string pool into java stack, so that the metadata can be loaded class number is no longer controlled by the MaxPermSize, and the actual space available is controlled by the system.

Welcome to work one to five years of Java engineer friends to join my personal fan base (Java technology stack architecture: 644 872 653) within the group to provide free learning materials Java architecture (which has high availability, high concurrency, high performance and distributed, Jvm more knowledge of performance tuning, Spring Source, MyBatis, Netty, Redis, Kafka, Mysql, Zookeeper, Tomcat, Docker, Dubbo, Nginx and other infrastructure data) rational use of every minute of their own time to enhance their learning Do not use the "no time" to hide his ideological laziness! Young, hard fight, give an account of their own future!


Guess you like

Origin blog.51cto.com/14480698/2455018