Acquaintance JVM :( two) Java garbage collection mechanism Detailed

Disclaimer: This article reference https://www.cnblogs.com/codeobj/p/12021041.html

For personal study and research purposes, not for commercial use, such as copyright infringement, please timely feedback, delete immediately.

A, Java Memory Architecture

file

1, 堆 Java (Java Heap)

  java heap is the largest piece of memory java virtual machine management 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, which is described in the Java Virtual Machine Specification is: all object instances and arrays to be allocated on the heap.

  java heap is the main area managed by the garbage collector, and therefore also known as "GC heap" (Garbage Collected Heap). From the point of view java heap memory reclamation can be divided into: the new generation and the old generation. 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 (Thread Local Allocation Buffer, TLAB). No matter how divided, nothing to do with stored content, all in order to better recovery of memory, allocating memory or a faster no matter which area of ​​the storage object instances are further divided.

  Under the Java Virtual Machine Specification, java stack may not be in contiguous physical memory space. The current mainstream virtual machines are scalable (by -Xmx and -Xms control). If no memory heap allocated instances completed, and the heap can no longer expand, it will throw an OutOfMemoryError.

2, Java Virtual Machine stack (Java Virtual Machine Stacks)

  java virtual machine thread is private, its life cycle and the same thread. Is a virtual machine stack Java memory model described method performed: Each method creates a stack frame (Stack Frame) while performing information storage for local variables table, operand stack, dynamic linking, method exports.

  We often say that the heap memory, stack memory, stack memory refers to the virtual machine stack. Local variable table to store various types of basic data (basic data types 8) compile-known, object reference (pointer address), returnAddress type.

  Local variable table memory space required to complete the assignment during compilation. During the operation will not change the size of the local variable table.

  This area provides two abnormal conditions: If the stack is greater than the depth of the thread requested virtual machine allowable depth StackOverflowError exception is thrown; if the virtual machine stack dynamic expansion, the expansion is not enough memory to applications, will throw the OutOfMemoryError exception.

3, native method stacks (Native Method Stack)

  Native method stacks and stacks of virtual machine is very similar to the role, but the difference between them is the virtual machine execution stack for the Java Virtual Machine method (ie bytecode) service, and the local stack method, compared to the native virtual machine The method of service. Native method stacks also throw two exceptions.

4, area method (Method Area)

  Java heap and method area as each thread is a shared memory area, which is used to store class information has been loaded in the virtual machine, constants, static variables, the time compiler to compile the code and other data. It has individual life called Non-Heap (non-heap). When the method of memory allocation area can not meet demand, OutOfMemoryError is thrown.

5, direct memory (Direct Memory)

 Virtual memory is not directly part of the runtime data area, nor is the java virtual machine memory area defined in the specification. But it is also a partial region chanting frequently used, but may also cause an OutOfMemoryError
in JDK1.4 newly added NIO (New Input / Output) type, based on the introduction of a channel (Channel) and a buffer (Buffer) in the I / O mode, it can use Native libraries directly outside the heap memory allocated, then a memory reference is stored as a piece of operating in DirectByteBuffer object of the java heap.

6, runtime constant pool (Runtime Constant Pool)

 Runtime constant pool is part of the zone method. Class file versions in addition to the classes, fields, methods, and interface description information, as well as a constant pool information, and for storing various literal compile generated reference symbols, will enter this part after loading run-time constant method area of the pool storage.
7 ##, the program counter (Program Counter Register)
The program counter is a small memory space, it can be regarded as the row number designator bytecode executed by the current thread.
Since the multi-threaded Java virtual machine is switched by a thread and turn allocation of processor execution time to achieve, a thread in a processor instruction execution only. Accordingly, in order to restore the thread switches execution to the correct position, each thread has a separate program counter, no interaction between threads, separate storage. Called "thread private" memory. The program counter memory area is the only virtual machine does not require OutOfMemoryError situation of the region.

8, execution engine

The core component is the virtual machine execution engine, which is responsible for the implementation of bytecode virtual machine, general household first compiled into machine code for execution.

9, the garbage collection system

Java's garbage collection system is the core, is essential, Java have a mechanism to clean up their own garbage, developers do not need to manually clean up

Second, the garbage collection algorithm analysis

1. What is the garbage collection mechanism

From time to time to clean up the heap unreachable objects. Unreachable object and not immediately will direct recycling, garbage collection in a Java program is automatic and can not be enforced, even if the programmer can definitely judge a piece of memory has been useless, should be recovered , the programmer can not force a garbage collector to reclaim the memory block. Programmers can do is by calling System.gc way to "recommend" the garbage collector, but whether it can be executed, when executed they are all unknown. This is the main disadvantage of the garbage collector.

public class Test {
    public static void main(String[] args) { Test test = new Test(); test = null; System.gc(); // 手动回收垃圾 } @Override protected void finalize() throws Throwable { // gc回收垃圾之前调用 System.out.println("垃圾回收机制..."); } }

2, finalize method of action

Java technology before use finalize () method to remove the object from memory out of the garbage collector, do the necessary cleanup. This method is determined by the garbage collector in this object is not called when this object is referenced. It is defined in the Object class, so all classes inherit it. A subclass overrides the finalize () method to organize system resources or perform other cleanup work. finalize () method is to delete the object before the garbage collector calls this object.

3, the new generation and the old year

Java JVM heap is managed by the biggest piece of memory space, mainly used to store various types of instance of an object.
In Java, the heap is divided into two distinct areas: the new generation (Young), years old (Old). New Generation (Young) are divided into three regions: Eden, From Survivor, To Survivor .

Thus divided JVM is designed to make the object to better manage heap memory, including memory allocation and recovery.

Heap memory model roughly as follows:

file

By default, the new generation (Young) ratio year old (Old) value of 1: 2 (the parameter value may -XX: NewRatio specified), namely: (Young) = 1/3 new generation heap space size. Years old (Old) = 2/3 heap space. Wherein the new generation (Young) is subdivided into two Eden and Survivor regions, two regions were designated as Survivor from and to, to show distinction.

Default, Eden: from: to = 8: 1: 1 (by parameter -XX: setting SurvivorRatio), namely: Eden = 8/10 new generation space, from = to = 1/10 new generation size of space.

Depending on garbage collection, Java heap possible to have different structures, the most common is the entire Java heap into

The new generation and the old era. Which is stored with little newborn newborn object or age of the subject, the store's old elderly subjects.

Den into new generation region, regions S0, s1 region, also referred to as S0 and s1 from and to the region, they are equal to each other and may be the size of the two space character.

In most cases, the object is first partitioned eden area, the new generation after the recovery, if the object is still alive, the process proceeds regions s0, s1, after every elapse of time

After the new generation of recovery, if the object is to survive its age is increased by one, object reaches a certain age, then enter the old era.

Are Third, how to determine the target survival

1, reference notation

The concept
Reference counting is that if an object is not any reference point can be regarded as waste. The disadvantage of this method is that the presence of loop can not be detected.
First need to declare at least the mainstream Java virtual machine inside did not choose to manage the memory reference counting algorithm. 
What counts is the reference algorithm: adding a reference to the object counter, whenever a reference to its place, the counter value by 1; when referring to the failure, the counter is decreased by one at any time the counter value is no longer the object 0 It is used. Why mainstream Java virtual machine inside did not choose this algorithm do? The most important reason is that it is difficult to solve the problem of mutual circular references between objects.

2, root search algorithm

Concept
The basic idea is that the root of the search algorithm by a series of objects called "GC Roots" as a starting point, to start the search downward from these nodes, called search path traversed reference chain (Reference Chain), when an object to no reference GC Roots chain joined, then prove that this subject is not available.

The basic idea of ​​the algorithm is a series of objects called "GC Roots" as a starting point, a search downward from these nodes, called search path traversed chain of references, when an object has no references to GC Roots chain ( that GC Roots when the object is unreachable), then prove that this target is not available.

So the question again, how to select GCRoots object? In the Java language, as GCRoots objects include the following categories:

  • (1) The virtual machine stack (local variables in a stack frame, also called the local variable table) referenced object.
  • (2) The process region static property class object reference.
  • (3) The object referenced method constant region.
  • (4) in native method stacks JNI (Native Method) referenced object.

GCRoots below a given examples below, reference is GCRoots chain.

file

file

user3 and user5 no reference chain

The basic idea of ​​the root of the search algorithm is a series of objects called "GC Roots" as a starting point, to start the search downward from these nodes, called search path traversed reference chain (Reference Chain), when an object to GC Roots without any reference chain joined, then prove that this object is not available.

Fourth, the garbage collection policy

1, clear labeling algorithm

concept

The algorithm has two stages.

    1. Mark phase: to find all the objects accessible, making a mark
    1. Clear stages: heap traversal, the unmarked objects recovered

Scenarios

The algorithm is generally used in the old era, because the object life cycle of old age longer.

Advantages and disadvantages

Clear labeling algorithm of the advantages and disadvantages

  • 1. Advantages
    • You can solve the problem of circular references
    • When necessary recovery (out of memory)
  • 2. Disadvantages:
    • When recovered, applications need to suspend, that is, stop the world.
    • Mark and sweep efficiency is not high, especially the more when the object to be scanned
    • Cause memory fragmentation (there obviously will lead to memory space, but due to the discontinuous, apply a slightly larger objects can not be done),

2, replication algorithm

concept

If jvm algorithm using coping, initially it will be available memory is divided into two, and from domain to domain, except from a time domain to the domain are idle. When the domain from memory is not enough, and began to perform GC operation, this time from the object domain will survive to be copied to the domain, and then directly to the domain from memory cleanup.

Scenarios

coping algorithm is generally used in the new generation, because the new generation of objects in general are Chaosheng evening death, the number of live objects is not much, so when copying algorithm using coping more efficient. The Heap jvm memory into new generation years old, in turn divided into a new generation of Eden (Eden) and 2 Survivor Space (survivors region), then Eden - between the> Survivor Space From Survivor Space and the To Survivor Space Copying the implementation of the algorithm. But jvm in the application of coping algorithm, the memory is not a 1: 1 divided, so too a waste of memory space. General jvm is 8: 1. That is, Eden Area: From Area: To area ratio is

There are always 90% of the space can be used to create objects, while the remaining 10% is used to store the objects recovered survival.

file

  • 1, when the Eden area is full, will trigger the first time young gc, the copy of the object to be alive Survivor From zone; when the young gc Eden District trigger again, scans the Eden area and From the region, for two regions garbage collection, recycling still alive after this object is copied directly into the to area, and Eden and From area cleared.
  • 2, when the follow-up of young gc Eden took place, and Eden will To area for garbage collection, live objects copied to the area From, To, and Eden and emptying regions.
  • 3, the visible part of the object will be copied in the From and To regions to be copied, so the exchange 15 (MaxTenuringThreshold determined by the JVM argument, the default is 15), and finally if you still alive, it's credited to the old

Note: In case the number of live objects are more, then the memory may not be stored in the To field, this time will be space-old's.

Advantages and disadvantages

  • Pros: survival in the case of small objects, the high performance, can solve memory fragmentation and garbage collection algorithm of java - question mark clearance in reference update due.
  • Cons: may cause part of the wasted memory. However, according to the actual situation, the memory block size appropriately adjusted ratio; if the number of live objects is relatively large, the performance becomes poor coping.

3, marking the compression algorithm

Clear labeling and marking algorithm very same compression algorithms, but the mark on the compression algorithm algorithm to solve the memory fragmentation in clearing marks

concept

file

Compression algorithm brief

  • In any order: i.e., without considering the order in which the original object, does not consider references between objects freely moving object;
  • Linear order: the considered object reference relationship, for example, a reference to the object of the object b, a and b are as far as possible to a mobile;
  • Sliding the order: the original in order to slide the stack in accordance with one end of the stack of objects.

Advantages and disadvantages

  • Pros: to solve the problem of memory fragmentation,
  • Disadvantages: the compression phase, since the movement of the objects available, the need to update references.

4, Minor GC and Full GC difference

concept:

 New Generation GC (Minor GC): refers to the place in the new generation garbage collection action, because most of Java objects with
equipment characteristics Chaosheng evening off, so Minor GC very frequently, usually recover relatively fast speed.

 Old's GC (Major GC / Full GC) : GC refers to occur in old age, there was Major GC, often
accompanied by at least one of the Minor GC (but not absolute, in the collection strategy ParallelScavenge Collector's
there directly Major GC strategy selection process). MajorGC generally slower than the speed of Minor GC 10
or more times.

Minor GC trigger mechanism:

When the young generation will trigger a full Minor GC, where the young generation full refers to the generation of full Eden, Survivor will not lead to full GCFull GC trigger mechanism:

Year old throws Full GC era full, Full GC will recover at the same time the young generation, old generation, when the permanent generation of full will lead to Full GC, will lead to uninstall Class, Method meta-information

Minor GC as shown in FIG.

Virtual machine for each object defines a target age (Age) counter. If the object was born in Eden, and after the first Minor GC still alive, and can accommodate Survivor, it will be moved to Survivor space, and the age is set to 1. Survivor objects in the area to get through every time Minor GC, increased age, 1 year old, the age when it is to a certain extent (the default is 15 years old), will be promoted to the old era. Object promoted's old age threshold, parameters can be -XX: MaxTenuringThreshold (threshold) is set.
file

Permanent behalf of the JVM garbage collection will occur Why?

Garbage collection does not occur in the permanent generation is full or if permanent behalf of more than the critical value, it will trigger full garbage collection (Full GC). If you look carefully output garbage collector, you will find a permanent behalf also be recycled. This is why the correct size to avoid permanent generation of Full GC is very important. Please refer to the Java8: generation of the metadata from the permanent area
(Note: the generation of permanent Java8 has been removed, added a new memory area called native metadata area)

5, generational algorithm

Outline

This algorithm, depending on the survival period of the object memory is divided into pieces, and the new generation of years old, so you can use the most appropriate collection method based on the characteristics of each era. You can grasp key ideas to understand this algorithm.
The new generation of students toward the object evening death, the number of objects, as long as the focus of the scanning area, then it can greatly improve the efficiency of garbage collection. Also old's object storage for a long time, without having to constantly scan the old era, to avoid the overhead of scanning lead.

Cenozoic

In the new generation, every time the garbage collector are found to have a large number of dead objects, only a few survived, the use of replication algorithm, only need to pay the cost of reproduction of a small amount of live objects to complete the collection;

Years old

The old era because of the high survival rate of the object, there is no extra space is allocated to its guarantee, it is necessary to "mark - Clear - compression" algorithm for recycling. See garbage collection algorithm of java - mark _ Clean compressed

The newly created object is allocated in the new generation, if the object were still alive after several recycling, then put the objects into the old era.

Young's old district storage area is full after Survivor triggering object after minor GC still alive, the Eden area is full when the object will survive into the area of ​​Survivor, Survivor if the district save any of these objects, GC collector will direct these objects stored in the old zone, if the area objects Survivor old enough to be placed directly into the old zone. If the Old area is full, it will trigger a Full GC reclaim the entire heap memory.

Guess you like

Origin www.cnblogs.com/Kevin6317/p/12484106.html