[Java Virtual Machine] JVM memory structure

[Java Virtual Machine] JVM memory structure


JVM divided into three major subsystems: a class loader subsystem, the runtime data area ( Runtime Data Areas ), and execution engine.

Memory structure == Java Virtual Machine runtime data area, the data area is the runtime specification the term.

First, the run-time data area

Here Insert Picture Description
In the Java Virtual Machine Specification, defines five runtime data areas, namely Java heap , the method area, a virtual machine stack , local area method, the program counter

① runtime constant pool will enter the zone method , the method area that is already included in the constant pool.
the Java heap and method area is shared by the threads . Other threads are private.

Here Insert Picture Description

Two, Java heap

Java heap is shared by all threads, it will be created when the virtual machine starts

The maximum Java heap is an area of memory space occupied, the Java heap is used to store an object instance and an array (that is, objects created new keywords), so here is the main activities of the garbage collector, then stack individual named GC heap and a single JVM process and only one Java heap.

Here Insert Picture Description
java heap Depending on whom survival is further divided into the young generation, two years old and areas, the young generation of further divided into the Eden area, From Survivor 0, To Survivor District 1. And a virtual machine would default configuration is Eden: from: to = 8: 1: 1.

Java heap = + years old Cenozoic
Cenozoic = Eden + S0 + S1
default Eden: from: to = 8: 1: 1

JVM heap common parameters:
-Xms : Initial heap size capacity (including the new generation and the old heap s). For -Xms 20M
-Xmxexample: : Heap total (maximum) size. For example: -Xmx 30M
Note : It is recommended -Xmsand -Xmxset to the same value, to avoid re-allocate memory after each JVM garbage collection is completed!
-Xmn: New generation capacity size. For example: -Xmn 10M
-XX: set the parameters SurvivorRatio Eden, and to form a ratio [scale parameter Eden, form and default to 8: 1: 1] For example: -XX: SurvivorRatio=8the representation of 8: 1: 1

Although not directly set the parameters of old age, but you can set heap space size and the size of the new generation of space of two parameters to indirectly control:
老年代空间大小 = 堆空间大小 - 年轻代大空间大小

When our Java heap is not enough space to complete the instance distribution, and the heap can not be extended, will throw our common OutOfMemoryErrorabnormal, that is, we often say that the OOMabnormality (heap memory overflow)

After the JVM heap memory overflow, other threads can continue to work?

Thread generally happens OOM will die, that will be the end off the target thread holds the heap will be occupied by gc, and free memory. Because to conduct gc before OOM occurs, even if other threads to work properly, it will have a greater impact because of frequent gc.

Third, the method area

Take HotSpot virtual machine, in JDK1.7 when the method area is referred to as permanent generations , from JDK1.8 start, Metaspace (element space) is what we call the method area!

A method for storing class information area of ​​the virtual machine has been loaded, a constant, static variables, the real-time data such as the code compiler. The method has a variant called area Non-Heap (non-heap)

Before JDK1.8 method area

HotSpot virtual machine to it, before JDK1.8, the method area also referred to as permanent generations, this method common area will occur java.lang.OutOfMemoryError: PermGen spaceabnormal, note the permanent generation of abnormal information, we can also control the size of the area by starting method parameters :

-XX: minimum space PermSize area setting method
-XX: maximum space MaxPermSize area setting method

In the previous JDK7 HotSpot virtual machine, the string into the string constant pool is stored in the permanent generation, thus resulting in a series of performance problems and memory overflow error. String is a particularly striking example of the intern()method

The method area after JDK1.8
after JDK8 would not become permanent on behalf of this statement is called element space (meta space), and the old year and stripping element space. Dimensional space placed in local memory, so the largest space element space is the system memory space, and thus will no longer appear as permanent generation memory overflow error, the data is moved to swap leakage of such a thing will not appear .

The user can set a maximum available space for the element space is not provided in accordance with the default size of the dynamic class metadata element to increase the capacity of the space. For a 64-bit JVM server side, its default –XX:MetaspaceSizevalue is 21MB. That default meta-space is 21MB.

As long as the class loader still alive, it loads metadata class is also survived, the recovery will not be lost! That is, with the total death

JDK1.8 advantage of changing the method area

  1. Avoid permanent generation memory overflow error
  2. Removing permanent behalf (PermGen) can promote the integration of HotSpot JVM and JRockit VM because JRockit no permanent generations.

Fourth, the thread private: a program counter, Java virtual machine stack, native method stacks

1. virtual machine stack

1, each thread Java virtual machine has its own private Java virtual machine stack, the stack at the same time create the Java virtual machine with thread, so it has the same life cycle with thread.

2 is a Java virtual machine stack described Java method memory model execution: Each method creates a stack frame at the same time executed for storing information local variable table, the operand stack, dynamic links, and so on for export, each the method of the process is performed until the call is completed, it corresponds to a Java virtual machine in the stack frame of the stack to the stack of a process stack.

3, the local variable table stored for all the basic data types compile knowable, and returnAddress object reference type.

1, the basic types: the eight basic types
2, object reference: reference type, it is not equivalent to the object itself, realized according to different virtual machines, it may be a reference to an object start address pointer may point to an object representing It handles or other locations associated with this object.
3, returnAddress types: a point to the address of the bytecode instruction.

Wherein the long and double-length 64-bit data occupies two local variable space (Slot), the remaining data occupies only one type. Desired local variable table created during compilation memory space allocated, when entering a method, this method requires much space for local variables allocated in the frame is completely determined during operation of the method does not change the size of the local variable table.

. 4, the Java Virtual Machine stack allows both be implemented as a fixed size, but also allows the calculation of dynamic to expand and contract, if a fixed size, then each thread Java virtual machine stack capacity can be independently selected when the thread is created. Two kinds of abnormality will occur in the Java Virtual Machine stack, it has pointed out that this virtual machine specification:

  • If the thread stack allocation request exceeds the capacity of the largest Java Virtual Machine stack permitted capacity, Java virtual machine will throw StackOverflowErroran exception; that is, a stack overflow error! Recursive call to generate StackOverflowErroran exception this result.
  • If the Java Virtual Machine stack can be dynamically expanded, and when it tries to extend the application to not enough memory or not enough memory when you create a new thread to create the corresponding Java virtual machine stack, the virtual machine will throw OutOfMemoryErroran exception. OOM is a memory overflow error! (Thread started too)

Of course, you can parameter -Xssto adjust the size of the JVM stack!

2. A native method stacks (Native Method Stacks)

And virtual stack similar, but it serves the Native method , thread private. When the Java virtual machine using other languages (e.g., C language) instruction set implemented interpreter, also using the native method stacks. If the Java virtual machine does not support natvie method, and he is not relying on traditional stack, you may not need to support native method stacks.

As with the Java virtual machine stack, the stack area native method can also throw StackOverflowErrorand OutOfMemoryErrorexceptions.

HotSpot virtual machine directly put into one native method stacks and stacks virtual machine.

3. Program Counter

Virtual machine bytecode currently executing thread line number indicator for recording the address byte instruction is being executed, private to the thread.

The program counter is the only one not defined in any Java Virtual Machine Specification OutOfMemoryErrorregional context.

Published 82 original articles · won praise 7 · views 5008

Guess you like

Origin blog.csdn.net/magic_jiayu/article/details/104268846