JVM-Java virtual machine memory management mechanism

Java virtual machine memory management mechanism

1 Java memory area and memory overflow exception

1.1 runtime data area

Method region (Method Area)

Threads share a region, class information storage has been loaded in the virtual machine, constants , static variables, the time compiler to compile the code and other data.

In HotSpot also called permanent-generation (Permanent Generation), but in fact the two are not equivalent, just because HotSpot will be extended to the GC method area.

Heap (Heap)

Virtual machine memory in the biggest piece for most applications. All threads share a piece of the region, to create a virtual machine Qidong. The sole purpose of the heap is to store all object instances. Java Virtual Machine Specification Description: all object instances and arrays to be allocated on the heap, but with the development of JIT compiler technology matures and escape analysis, allocation on the stack, scalar replacement, optimization techniques will lead to some subtle changes, all objects are allocated on the heap is no longer so "absolute."

Java heap is the main area managed by the garbage collector, often called the "GC heap" (Garbage Collection Heap). From the recovered memory, because the main use of sub-band collection algorithm, the stack may be subdivided into 2 years old and the new generation, then there is then fine Eden area, From, Survivor region, To Survivor region. From the perspective of memory allocation, multiple threads can separate private allocate a buffer (Thread Local Allocation Buffer, TLAB).

Specification, discontinuous heap space physically, logically successive. When implemented, it is possible to make a fixed size, it may be extended through and -Xms -Xmx control. When the heap is not enough memory to complete the assignment object instance, and the heap can not be extended, it will throw OutOfMemoryError.

Virtual Machine stack (VM Stack)

Thread private , the life cycle of the same thread. Is a virtual machine stack Java memory model described method performed: Each method creates colleagues performed a stack frame used to store information local variable table, the operand stack, dynamic linking, method exports. Is often said that stack virtual machine's memory.

Both exceptions: 1 If the stack is greater than the depth of the thread requested by the virtual machine allowable depth, throws StackOverflowError exception; 2 if the virtual machine stack can be dynamically expanded, if not enough memory to apply to the OutOfMemoryError is thrown. .

Native method stacks (Native Method Stack)

Stack Java virtual machine to perform method (bytecode) services, native method stacks into service when using Native Virtual Machine, also thread private , as well as the provisions of the above two exceptions.

Program Counter (Program Counter Register)

It will be appreciated as the row number designator bytecode executed by the current thread. Each thread has a program counter, attributed to each thread private , when the method is performed Native method, the PCR value is null, in the specification, the area is not an OutOfMemoryError.

Runtime constant pool (Running Constant Pool)

Runtime constant pool belonging to the method area part thereof. Class file versions in addition to the classes, fields, methods, and interface description information, there is a constant pool (Constant Pool Table), and for storing various literal reference symbols generated during compilation, in this part after running into the class loading method to the time constant pool of storage.

Memory limitations by the method area, OutOfMemoryError is thrown when a constant pool can not apply to memory.

 

1.2 Object Creation

create

Note: The mentioned here do not include arrays and objects Class objects.

1. When an instruction is encountered new keyword, first check whether there is a reference symbol the constant pool of the class, to check whether the class represented by this reference is loaded, parsed and initialized. If not, perform class loading process.

2. checked by loading class, then allocates memory for the new object. Distribution divided into two types: 1) pointer collision: assuming the heap memory tacticity, i.e., on the side of the used memory, the memory controller on the other side of the line, the middle stood as a demarcation point indicator hand, when dispensing is directed to the side of the moving section of the free space equal to the size of the object distance. 2) free list: When the memory is not structured, can not be used when the pointer collision. VM maintains a list of record which memory blocks are available, allocation of time to find out from the list a large enough free memory allocated to the object instance, and update the list.

Two or more distribution in the case of concurrent non-thread-safe. Two solutions: 1) the operation of memory space allocated sync, i.e. using CAS + guaranteed failure retry atomic update operation; 2) in the memory allocation operation according to different thread-partitioning a space, i.e., each thread a small piece of pre-allocated heap memory in Java - a local thread allocation buffer (thread Loacal allocation buffer, TLAB), then which thread needs to allocate memory allocated on TLAB which thread, only TLAB run out and assign a new TLAB only when synchronization is locked. Whether TLAB, by -XX: +/- UseTLAB parameter setting.

3. When the memory allocation is completed, the allocated memory space are initialized to a value of zero (not including the object header). If you use TLAB, this operation can be carried out in advance to TLAB. This operation ensures that the object instance is not the initial value may be given directly, access to the field corresponding to the zero value.

VM object settings, such as which object is an instance of a class, how to find information such as metadata, hash code of the object, GC generational age of the subject, and other information biased locking is enabled in the current operating state, these information on the object head (Object header) in.

layout

In HotSpot, the stored object in memory layout is divided into three regions: the head Object (Object Header), instance data (Instance Data) and alignment padding (Padding).

1. Object head

Object header includes two pieces of information.

1) Mark Word: storing runtime data object itself, such as a hash code (HashCode), GC generational age lock state flag thread holds the lock, the thread ID bias, bias timestamp. Due to a large run-time data, it will multiplex the storage space, as shown in Table 1

Table 1

Memory contents Flag status
The hash code of the object, the object generational Age 01 Unlocked
Pointer recorded 00 Lightweight lock
Heavyweight locks pointer pointing 10 Expansion (and locked by weight)
Empty, no need to record 11 GC mark
Bias thread ID, timestamp bias, the object generational Age 01 Be biased

 

2) type pointers: a pointer to the object that is its class metadata, the object can be determined by the pointer which is an instance of the class. Not all virtual machine implementation has type pointer.

Note: the object is a Java array time, there must be a target for the first data record length of the array.

2. Examples of data

The information storage section is a valid object, i.e., each portion of the field content type defined in the program code. Including inherited from the parent class, subclass definitions.

3. filling it

Filling it does not necessarily exist, only plays a role in the placeholder.

Because the starting address HotSpot VM objects is an integral multiple of 8 bytes, when data is not aligned instance, be filled.

 

1.3 Positioning visit

Two main ways, using the handle and direct pointer.

1. Handle: Java heap memory as will be divided into a pool handle, Java stack handler address is stored in the reference table stored in a local variable, then the handle address contained in the respective specific information of the object instance data and object type data.

2. direct pointer: not handle portion, the address reference stored directly in the object instance data and object type data.

Handle, because the reference is stored in a stable handler address, the object moves (the garbage collector moves is a normal operation and universal) when the only change in the handle pointer; direct pointer is the speed of opening, there is no excess as a handle as positioning.

 

2. The garbage collector and memory allocation strategy

2.1 determine how an object is dead?

Before the garbage collector heap for recycling, first of all to determine which objects are still "alive", which objects had been "dead", mainly by two ways.

1. Reference counting (Reference Counting)

Add a reference to an object counter, whenever a reference to its place when the counter is incremented; when referring to the failure, the counter value by one, when the counter value is 0, indicating that the object has been "dead."

Advantages: simple, determination efficiency efficient and effective most of the time.

Disadvantages: difficult to resolve circular references between objects problems.

2. reachability analysis algorithm (Reachability Analysis)

This algorithm is also used in Java.

The basic idea:  by a series becomes "GC Roots" object as a starting point, to start the search downward from these nodes, called search path traversed reference chain (Reference Chain), when an object has no references to GC Roots when the chain is connected, to prove that the object is not available.

GC Roots Java, comprising: a target virtual machine stack (Local Variable Table stack frame) referenced in the method area attribute references the static class object, an object method constant reference area, Local JIN process (generally called Native method) object references.

Reference level

Strong reference (Strong Reference), the code prevalent, such as Object obj = new Object (), as long as the reference is still strong, the garbage collector will never be recovered referenced.

Soft references (Soft Reference), describe some, but not as well as with the necessary objects. Before the system is going to happen memory overflow exception, have a soft object referenced by a second recycling. If after the recovery of memory is not enough, it would be thrown out of memory exception. Implemented SoftReference class.

Weak references ( Weak  Reference), also describe non-essential objects, cited weaker intensity than the soft, soft objects referenced only to survive until the next garbage collection occurs when the garbage collector job, regardless of whether sufficient memory, will be recycling. Implemented WeakReference class.

Virtual reference ( Phantom Reference ), also known as ghost or phantom cited references, cited weakest. Whether there is a phantom reference object has completely will not affect their survival time, can not be achieved by a phantom reference object instance. Provided virtual reference object is notified and a recovery. Implemented PhantomReference class.

Reachability analysis, for the first time no GC Roots chain of references when connected, for the first time mark, if not finalize () method or finalize () method has been invoked VM, the object will not be sentenced immediately death, that will not be recovered immediately subsequent second time reachability analysis of a second mark, then recovered. If finalize () method, it will be placed in a queue F-Queue, and then executed by the recovered Finalizer thread.

Recovery process area

(The HotSpot) permanent generational garbage collector to recover primarily two Category Content: waste constants and useless classes.

Abandoned constant: when there is no the constant object reference constant pool, there is no other references to these literal, if the recovery occurs at this time, when necessary, the constant will be recovered. Symbols other classes (interfaces), methods, fields in the constant pool references are also similar.

Class useless: to meet three conditions. 1) all the forces in the class has been recovered, i.e., the stack of any instance of the class does not exist; 2) loading the class ClassLoader has been recovered; 3) corresponding to the class object does not have anywhere java.lang.Class It is referenced, not by the reflection method to access the class at any place.

 

2.2 garbage collection algorithm

1. Mark - sweep algorithm (Mark-Sweep)

Mark - sweep is the most basic collection algorithm. Algorithm is divided into "mark" and "clear" two stages.

Tags: mark all objects need to be recycled, recovered all unified object is marked in the mark after completion.

Clear: The labeled objects recovery operation.

Subsequent collection algorithm is an algorithm to improve its shortcomings and to get on this train of thought.

Less than: 1) efficiency, marking and removal efficiency two stages are not high enough; 2) mark after clearing will generate a lot of discrete memory fragmentation.

2. Copy the algorithm (Copying)

Address efficiency issues. The memory capacity is divided by two equal size, which uses only one. When one piece of memory after use, copy the alive objects to another one, and then that piece originally used to clean up.

Regardless of the debris problem, simply move the pointer to the top of the heap, you can allocate memory in order; but only use half of the original memory, memory usage is not high enough. But when a higher survival rate when an object, the copy operation will reduce efficiency.

The memory is divided into three parts Eden, Survivor, Survivor the To, the ratio of 8: 1: 1, each copy live objects in Eden and Survivor Survivor to the To, a more efficient use of memory. To Survivor when space is not enough, it will be moved into the old era.

3. Mark - finishing (Mark-Compact)

According to old's characteristics, labeling and marking process - clear marker in the same, but the subsequent steps are not directly recycled objects to clean up, but moved to the end let all live objects, and finally clean out the memory directly outside the terminal boundary.

4. generational collection algorithm (Generational Collection)

Depending on whom the survival period of the memory is divided into a few pieces. Generally the Java heap into the new generation and the old era, then the most appropriate collection algorithm based on the characteristics of each era.

Cenozoic each time garbage collection when there are large quantities of objects "dead", can be directly used replication algorithm , only a small amount of live objects replication costs to complete the collection.

Years old due to the higher survival rate of the object, and there is no extra space for "security" (such as insufficient time to enter To Survivor's old, so old that guarantee To Survivor's part), use the mark - sweep or mark - clean up the algorithm.

 

2.3 garbage collector

The garbage collector is the implementation of garbage collection algorithms.

 

 Figure above 7 kinds act on different generational collectors, if there is the connection between the two collectors, indicating that they may be used with. Area indicates that it belongs to the new generation of collector's collector or old.

1. Serial Collector

Single-threaded collector, the collector when the work will suspend all other worker threads, knowing that collects end. --Stop The World.

VM is still running in Client mode default under the new generation of collectors.

Compared to other collectors simple and efficient .

2. ParNew collector

Multi-threaded version of the Serial collector, this is the only difference between Serial collector.

VM Server mode of choice in the new generation of collectors, as for the current CMS collector work together.

Garbage collector in parallel and concurrently .

Parallel (Parallel): refers to the number of threads in parallel garbage collection, but this time the user thread is still in a wait state.

Concurrency (Concurrent): refers to the user thread garbage collector threads execute (but not necessarily parallel, may alternately performed), the user program continues to run, and garbage collection program running on another CPU.

3. Parallel Scavenge collector

Is a new generation of collectors, use the copy collector algorithms, parallel, multi-threaded collector.

Features: CMS concern collector is as short as possible when the user thread garbage collection pause time, and Parallel Scavenge goal is to reach the collector of a controlled throughput (Throughput).

Throughput: CPU for running the user with ah CPU time and the ratio of the longitudinal time consuming.

Throughput = user code running time / (user code running time + time garbage collection). The common running VM 100 minutes, wherein the garbage collector spent 1 minute, then 99% throughput.

Therefore, also known as "throughput priority" collector.

4. Serial Old collector

Is Serial old's version of the collector, the collector is single-threaded, using the mark - sorting algorithms.

Client virtual machine in the main mode of use.

5. Parallel Old collector

Parallel Old is old Parallel Scavenge collector's version, use the mark - sorting algorithms.

Mainly with the use of the Parallel Scavenge collector.

6. CMS collector (Concurrent Mark Sweep)

For the shortest recovery time objectives pause collector.

Most Java applications in several server-side Internet or B / S system, such applications where responsiveness services, want the system to pause the shortest time, in order to give users a better experience. CMS meets this requirement.

Based mark - sweep algorithm is realized.

Run process is divided into two steps:

1) The initial mark (CMS initial mark) need to Stop The World, marking only the object GC Roots can be directly linked to the speed.

2) Concurrent mark (CMS concurrent mark) for GC RootsTracing process,

3) re-mark (CMS remark) need StopThe World, in order to correct during the concurrent mark because the user program continues to run and lead to marked changes in the object generated record mark portion, a little longer than the initial mark phase, but a short time than the concurrent mark phase .

4) Concurrent Clear (CMS concurrent sweep)

Advantages: concurrent collection, low pauses, also referred to as concurrent pause collector (the Pause Collector Concurrent Low) .

Disadvantages: 1) is very sensitive to CPU resources when concurrent garbage threads CMS default number of recovery threads start (the number of CPU + 3) / 4; 2 ) can not handle floating garbage (Floating Garbage), may appear "Concurrent Mode Failure" the failure which led to another Full GC produced; 3) based on a result of mark - sweep collector algorithm can not defragment, you can sort through debris and setting UseCMSCompactAtFullCollection CMSFullGCBeforeCompaction.

Floating garbage: When CMS concurrent user threads still running clean-up stage, along with the program will generate new spam run, this part of the garbage appears after the marking process, CMS can not handle them at times when the collection, the GC is once again carried out under the left clean-up.

7. G1 collector (Garbage-First)

It is for the server garbage collector application.

Features:

1) parallel and concurrent: to take advantage of multi-CPU, multi-core hardware advantages in the environment, the use of multiple CPU to Stop The World shorten the time to pause;

2) generational collection: generational concept still remain in G1;

3) Spatial Integration: Gl is based from the whole mark - finishing collector algorithm from topical (between Region) view is based on replication algorithm implemented;

4) predictable pause: reduce the dwell time is the common concerns of CMS and G1, G1 through the establishment of a predictable pause time model, allowing users to explicitly specify a length within M millisecond time slice consumed in the garbage collector may not exceed N milliseconds.

Operating G1 collector divided into four steps:

1) initial labels (the Initial Marking) , GC Roots mark object can only directly linked to the need to halt the thread, but shorter ;

2) Concurrent mark (Concurrent Marking), began to heap objects reachability analysis from GC Roots, find live objects, time-consuming, and the user can program the concurrent execution;

3) The final mark (Final Marking), amended during the concurrent mark due to the continued operation of the user program which led to marked changes in that part of the generated record mark, you need to stop thread can be executed in parallel ;

4) screening recovery (Live Data Counting and Evacuation), to recover the value and cost of each Region to sort, develop recycling programs according to user desired GC pauses, the user program can also be concurrently executed.

 

 

 

 

2.4 Object allocation and recovery strategies

Java technology system advocated in the final automatic memory management can be attributed to solve two problems for the automation of: memory allocated for the object to the object of memory allocation and recovery.

1. 对象优先分配在Eden区,大多数情况,对象在新生代Eden区中分配,当Eden区空间不足够的时候回发生Minor GC。

Minor GC和Full GC区别:

新生代GC(Minor GC):指发生在新生代的垃圾收集动作,Minor GC较为频繁,因为Java对象朝生夕灭的特性,回收速度也快;

老年代GC(Major GC/Full GC):指发生在老年代的GC,出现Major GC时经常会伴随至少一次Minor GC(但非绝对,在Parallel Scavenge收集器上的手机策略里有直接进行Major GC的过程)。Major GC速度一般比Minor GC慢10倍以上。

2. 大对象直接进入老年代

大对象:需要大量连续内存空间的Java对象,比如长字符串和数组。

3. 长期存活的对象将进入老年代,VM给每个对象定义了一个对象年龄(Age)计数器,每进过一次Minor GC计数器加1,年龄达到一定程度度(默认15岁),就进入老年代咯。通过参数MaxTenuringThreshold设置。

4. 动态对象年龄判断,如果Survivor空间中相同年龄所有的对象大小的总和大于Survivor空间的一半,年龄大于或等于该年龄的对象就可以直接进入老年代,无需叨叨MaxTenuringThreshold所要求的年龄。

5. 空间担保分配,发生Minor GC前,虚拟机会先检查老年代最大可用的连续空间是否大于新生代所有对象的总空间,如果该条件成立,那么Minor GC是确定安全的;如果不成立,虚拟机会查看HandlePromotionFailure设置是否允许担保失败。如果允许,那么继续检查老年代最大可用的连续空间是否大于历次晋升到老年代对象的平均大小,如果大于,将尝试着进行一次Minor GC,尽管该操作有风险;如果小于或是HandlePromotionFailure设置不允许,就改为Full GC。

 

参考《深入理解Java虚拟机(第二版)》所写。

 

Guess you like

Origin www.cnblogs.com/baishouzu/p/12312864.html