Details of the new generation and the old garbage collector's

The new generation and the old year

After the object is instantiated, it belongs to the new generation.

Most of the new generation of its life cycle is short, objects that are created in a process will be finished with the process stack space stack after stack frame a lost reference.

And indeed there are some objects in the heap of long-term survival, such as the object referenced Static. This object is not easily be garbage collected.

So JVM heap memory will be divided into two regions, a young generation of an old era.

Among the young generation, as the name suggests, it is to create and use objects will be recovered immediately after completing the inside. Then with the elderly, that is, some will be long-term survival of the object on the inside.

Why the old and into the new generation's

Many people will have a question, why we should divide the two areas it?

Because the garbage collection related to the young generation of objects, their features are soon to be recycled, so it is necessary the use of a garbage collection algorithm.

And for old time, which most likely will target long-term survival, then the use of the new generation collection algorithm on here may not be so appropriate. We need to have their own set of collection algorithm.

What is a perpetual generations

Very simple, JVM in the permanent generation of fact, we said before, the method area

On behalf of the so-called permanent, you can think of some kind of information is stored, in a chapter we knew we generate .class is stored in this area. Under normal circumstances, we are the new generation and the old tune for years jvm tuning. Generally permanent Generation maintain the default configuration on it.

Does that mean we do not need permanent attention generations?

Certainly it is not. Because the relevant information to be stored in the class, so that in the case of class dynamically generated relatively easily substituted permanent memory overflow occurs. The most typical scenario is that the more jsp page, the easy generation of permanent memory overflow occurs.

How to determine whether an object is garbage

We know that when an object is created, for example to create an object in a method, when the process is finished, there will be no references to the object, and the object becomes garbage objects.

This is just one case.

Low to which objects are spicy chicken, which objects not?

The JVM uses a reachability analysis algorithm to determine which objects can be recycled. The core of the algorithm is to see who is the object references in it, then layer by layer up to judge, to see whether the referenced GC roots.

In java, as the object GC Roots are:

1.虚拟机栈(栈帧中的本地变量表)中引用的对象(也就是我们前面提到在方法中创建的对象);
2.方法区中的类静态属性引用的对象;
3.方法区中常量引用的对象;
4.本地方法栈中JNI(即一般说的Native方法)中引用的对象

When an object is not referenced above, the object can be considered as a garbage objects.

In summary remember a word, as long as your object method with variables, static variables like references to, he would not recover.

Reference relationship java objects different.

1, strong references

就是被GC roots所直接引用的对象。只要是强引用关系,那么垃圾回收器是绝对不会回收这个对象的。

2, soft references

public class test{
    public static SoftReference<ReplicaManager> manager =
        new SoftReference<ReplicaManager>(new Replicamanager)
}

/**如上诉代码,实例对象被“SoftReference” 软引用类型的对象包装起来了,那么这个对象的引用就是软引用。

正常情况下是不会回收软引用对象的,但是如果你进行垃圾回收后,返现内存还是不够存放新的对象的时候,这个时候就会吧软引用的对象给回收掉
**/

3, weak references

这个相对前两中使用的很少,与软引用类时,当一个实例对象被“WeakReference”弱引用的对象包装起来的时候,那么这个对象就是弱引用。
弱引用的生命周期就存在下一次垃圾回收之前,也就是说下一次垃圾回收会回收掉弱引用的对象。

4, phantom reference

最弱鸡的一种引用,我感觉没什么作用。一个对象是不是虚引用对他本生的生命周期没有影响。该什么时候回收就什么时候回收。
设置他唯一的目的就是被回收的时候会得到一个系统通知,一般来说没什么卵用

Action finalize () method

Here, I believe you should be clear which objects will be recovered which objects can not be recovered.

If an object is referenced by GC Roots, but! ! If he is soft or weak references cited, it is likely to be spicy chicken collector to reclaim away.

When the object is garbage collected, it will be called the Object object finalize () method.

Our self-redemption codes to simulate a spicy chicken.

public class A{
    public static A instance;
    @Override
    protected void finalize() throws Throwable{
        A.instance = this;
    }
}

When objects are recovered, call finalize method re-GC Roots variable reference point to themselves, then it will not be recovered.

This is basically a nobody, a feeling that sb would write code. Write to you to say that this detail.

How old's the new generation objects are turned into objects

Long-term survival of objects repeatedly dodged garbage collection

How many times Minor GC escaped without being recycled out, we believe that the age of the object are how old, by default, when an object is more than 10 years of age, he was considered an old man. We need to be transferred to the old year go.

Think also normal, the new generation of competitive pressure is so big, elderly subjects still early to really compare the old year with young people compete for resources.

Particularly large objects directly without going through the new generation enters years old

Competition in the young generation of the already great, you have to take up so many resources, no no, you still go to it's old.

Age estimation of dynamic mechanism

This part requires a combination of garbage collector algorithm is concerned, it is to copy algorithm. We will cover in a later chapter, here probably tell us.

We generally will Cenozoic is divided into three areas, one Eden, two Survivor. Ratio 8: 1: 1.

Eden generated default object region. In the event of a rear Minor GC, we will live objects copied to a Survivor region. GC will survive after the next time an object is copied to another piece of Survivor. The benefit of this is to reduce memory fragmentation.

When we GC occurs once after the surviving objects into the area where a survivor. Found 1 year, 2 years old, 3-year-old target age add up to more than half of the survivor memory area, it will put more than 4 years old and 4-year-old wanted to move on to the old era.

GC region after the survivor can not fit

This is no way, after GC still can not fit in general but for the surge in traffic, or is optimized not in place, so I had to write these objects transferred to the old era.

Space guarantee mechanism

Before minor gc occurs, the virtual testing: old's maximum available contiguous space> new generation of all objects of the total space?

1, to meet, minor gc is safe and can be minor gc.

2, is not satisfied, the virtual machine View HandlePromotionFailure parameters:

(1) is true, allow guarantee failure, will continue to detect the largest contiguous space available years old> promoted to the average size of the previous decade old objects. If it exceeds, will attempt to conduct a minor gc, if fails, re-conduct a full gc.

(2) is false, is not allowed to take risks, to be full gc (years old were gc).

Guess you like

Origin www.cnblogs.com/zhxiansheng/p/11294529.html