jvm series (a) of the memory model

JVM memory structure

Java memory model refers to the Java virtual machine memory model, we look at the pictures Java memory model:

 

 VM memory model is divided into three: Java heap memory (Heap), method area (Non-Heap), JMV stack (JVM Stack), native method stacks (Native Method Stacks), program counter (Program Counter Register).

 

Java heap (Heap)

For most applications, Java heap (Java Heap) is the largest piece of memory in the Java virtual machine management. Java heap is shared by all threads in a memory area is created when the virtual machine starts. The sole purpose of this memory area is stored object instance, almost all object instances are here to allocate memory.

Java heap is the main area managed by the garbage collector, and therefore often also called "GC heap." If you look from the perspective of memory recovery, due to the current collector basically adopted generational collection algorithm, so the Java heap can also be broken down into: the old and the new generation's; and then there is little detailed Eden space, From Survivor space, To Survivor space.

Under the Java Virtual Machine specification, Java heap may be in discontinuous physical memory space, as long as logically contiguous to, just like our disk space. When implemented, may be implemented as a fixed size, it can be expanded, but the current mainstream is an extensible virtual machine implemented (by -Xms -Xmx and control).

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

 

Method region (Method Area)

District method (Method Area) and the Java heap, as each thread is a shared memory area for storing virtual machine has been loaded class information , constants , static variables , the 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-stack), the Java heap object should be distinguished. JDK1.6 is this memory structure, but in JDK1.7 began to transfer part of the information stored in the method area of the Java Heap or Native Heap in, but did not completely remove the zone method, the class definition as class loading methods are still region, reference symbol (symbols) was transferred to the native heap (local memory), literal (interned strings) was transferred to the java heap, static class variable (class statics) was transferred to the java heap. And after JDK1.8 / 1.9 completely removed conceptual approach area, replaced by a called Metaspace (element space).

 

Program Counter (Program Counter Register)

  Program counter (Program Counter Register) is a small memory space, its role can be seen as the row number designator bytecode executed by the current thread. In the conceptual model of a virtual machine's (only a conceptual model, a variety of virtual machine might go through some achieve more efficient way), when the bytecode interpreter at work is to be selected by changing the value of a counter to be executed bytecode instructions, branching, looping, branching, exception handling, and other basic functions thread recovery relies on the counter to complete.

Since the multi-threaded Java Virtual Machine is in turn switched mode execution time and processor allocation by threads to achieve, at any time a determined, a processor (for multi-core processors is a kernel) will only execute one thread the instructions. Therefore, in order to restore the thread switch to the correct execution position, each thread requires a separate program counter, between the threads independently of each other, independent store, we call this type of memory area is "thread-private "memory.

If the thread is executing a Java method, the counter records the address of the virtual machine bytecode instruction being executed; if the method is being performed Natvie, this counter value is null (Undefined).

This memory area is the only one not provide for any situation in the region OutOfMemoryError Java Virtual Machine Specification.

 

JVM stack (JVM Stacks)

Like the program counter, Java Virtual Machine stack (Java Virtual Machine Stacks) is also a thread private, its life cycle and the same thread. Is a virtual machine stack Java memory model described method of execution: when each method will be executed at the same time create a stack frame (Stack Frame) for storing local variable table , stack operation , Dynamic Link , for export information. Each method is called until the completion of the execution procedure, a stack frame corresponds to a virtual machine from the stack to the stack of a process stack.

The local variable table stored compile various known basic data types (boolean, byte, char, short, int, float, long, double), object reference (reference type, it is not equivalent to the object itself, depending on the virtual machine implement, it may be a reference to the start address pointer to the object may also be a handle to the object representing the object associated with this or other location) and returnAddress type (address pointing to a byte code 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.

In the Java Virtual Machine Specification, this area provides two anomalies: If the stack is greater than the depth of the thread requested virtual machine allowable depth, StackOverflowError will throw an exception; if the virtual machine can dynamically expand the stack (most of the current Java virtual machines can be dynamically expanding, but the Java virtual machine specification also allows virtual machine stack fixed length), when the expansion can not apply enough memory will throw OutOfMemoryError exception.

 

Native method stacks (Native Method Stacks)

Native method stacks (Native Method Stacks) and the role played by the virtual machine stack is very similar, but the difference is the virtual machine execution stack for the Java Virtual Machine method (ie bytecode) service, and is for the native method stacks virtual machine to use a method of Native service. Virtual Machine Specification language native method stack method used, use a data structure is not mandatory, so the specific virtual machine it can be freely realized. Even some virtual machines (such as Sun HotSpot VM) directly put into one native method stacks and stacks virtual machine. Like the virtual machine stack, the stack area native method can also throw StackOverflowError and an OutOfMemoryError.

Interact with other languages.

 

JVM memory configuration and tuning parameters

To understand how to control the area of ​​each parameter by a memory size in FIG. 

 

 

Heap memory interfaces and memory limit parameters 

control parameter

-Xms             设置堆的最小空间大小。 
-Xmx             设置堆的最大空间大小。 
-XX:NewSize      设置新生代最小空间大小。 
-XX:MaxNewSize   设置新生代最大空间大小。 
-XX:PermSize     设置永久代最小空间大小。 
-XX:MaxPermSize  设置永久代最大空间大小。 
-Xss             设置每个线程的堆栈大小。

JDK8之后JVM没有方法区概念,所以没有PermSize参数设置取而代之是Matespace,参数设置为

-XX:MetaspaceSize,初始空间大小,达到该值就会触发垃圾收集进行类型卸载,同时GC会对该值进行调整:如果释放了大量的空间,就适当降低该值;如果释放了很少的空间,那么在不> 超过MaxMetaspaceSize时,适当提高该值。 
-XX:MaxMetaspaceSize,最大空间,默认是没有限制的。 
除了上面两个指定大小的选项以外,还有两个与 GC 相关的属性:
-XX:MinMetaspaceFreeRatio,在GC之后,最小的Metaspace剩余空间容量的百分比,减少为分配空间所导致的垃圾收集 -XX:MaxMetaspaceFreeRatio,在GC之后,最大的Metaspace剩余空间容量的百分比,减少为释放空间所导致的垃圾收集

 

没有直接设置老年代的参数,但是可以设置堆空间大小和新生代空间大小两个参数来间接控制。

老年代空间大小=堆空间大小-年轻代大空间大小

 

 

 

方法区和对是所有线程共享的内存区域;而java栈、本地方法栈和程序员计数器是运行是线程私有的内存区域。

 

原文: http://blog.leanote.com/post/zhangyue/jvm系列之内存模型

Guess you like

Origin www.cnblogs.com/yrjns/p/12127825.html