Garbage collection in Java

Source: Internet
Author: User

Garbage collection from the JVM is a cliché, and here I'm going to talk to you about garbage collection.
1 where to collect the rubbish?
2 What can be considered rubbish?
3 How to recycle rubbish?


Where to collect rubbish

Here, I suggest you read my book: Java Memory management


5 Parts of:


The virtual machine stack, the local method stack, the program counter three regions with the thread, and out of the thread, stack frames in the stack with the method entered and exited and methodically carried out the stack and into the stack operation. How much memory is allocated in each stack frame is basically known when the class structure is determined (although some optimizations are made by the JIT compiler during the run-time, but in the discussion of the conceptual model in this chapter, which can generally be seen as the compilation period), so that the memory allocation and recycling in these areas are deterministic, There is no need to think too much about recycling in these areas, because when the method ends or the thread ends, the memory is naturally recycled.

Method area, which is shared by each thread. It stores the class information that has been loaded by the virtual machine, normally lit, static variables, instant compiler compiled code, and so on. It also contains a run-time constant pool, which holds various literal and symbolic references during compilation. The Java Virtual Machine specification said that virtual machines could not be required to implement garbage collection in the method area, and that the "price/performance" of garbage collection in the method area was generally low. In the heap, especially in the Cenozoic, a garbage collection of regular applications can generally reclaim 70%~95% space, and the garbage collection efficiency of the permanent generation is much lower.

The garbage collection of the method zone's permanent generation mainly recycles two parts: obsolete constants and useless classes.

The method area is also a permanent generation (permanent Generation)


The last thing left is a heap.

The Java heap is the largest piece of memory managed by the Java Virtual machine. and is shared by all threads. This area is created when the virtual machine is started. The only purpose of its existence is to store objects.

We're mainly recycling rubbish in the heap.

Let's first look at the composition of the heap in Java



As shown, the entire heap is divided into two parts, the Cenozoic and the Laosheng generation ( tenured Gen)。
One of the Laosheng is divided into three parts, a Eden area (Eden District, hehe) and two Survivor areas, respectively from Survivor and to Survivor.
The default proportions for each zone are given in.


What can be thought of as rubbish?

Objects that cannot be accessed are garbage.

So how do we tell if an object can't be accessed? There are at least two algorithms here.

Reference counting method

Person A=new person ("name1"); Person B=a;pserson c=a;c=new Person ("name2");

In the above code, the new person produces an object in the heap, which is referenced three times. After c=new person ("name2"), the original object is less frequently cited, and becomes 2.
We can give a record of the number of times each object is referenced. If, when, an object is referred to 0, then we think it is rubbish and can be erased.
Is this method OK? Let's look at the following example

public class REFERENCECOUNTINGGC {public    static void Main (string[] args) {        testgc ();    }    Public Object instance = null;    private static final int _1mb=1024*1024;    /**     * The only meaning of this member property is to occupy a bit of memory so that it can be seen in the GC log whether it has been recycled/    private byte[] bigsize = new byte[2 * _1MB];    public static void Testgc () {        REFERENCECOUNTINGGC obja=new referencecountinggc ();        REFERENCECOUNTINGGC objb=new REFERENCECOUNTINGGC ();        OBJA.INSTANCE=OBJB;        Objb.instance=obja;        Obja=null;        Objb=null;        Assuming GC is occurring on this line, can obja and objb be recycled?        System.GC ();    }}
Logically, Obja and OBJB have been unable to be accessed, it is garbage # should be cleared, but from the reference counting method, Obj1 and ojb2 the quoted number is not 0, they should not be cleared #

After we read the GC report, we know that the two objects have been cleared, which means that Java does not use reference counting to determine if an object is unreachable #

(How to report to the GC.) In addition, Java does not refer to technical law does not mean that this method is not good, Python uses the citation to manage

and search algorithms

This method is logically simple, too.

, the object1234 can be reached from the root, but OBJECT5,OBJECT6,OBJECT7 is beyond our reach. All of us think that object5,6,7 is rubbish and should be recycled.
So, what's the problem?
In the Java language, the objects that can be used as GC roots include the following:
The referenced object in the virtual machine stack (the local variable table in the stack frame).
The object referenced by the class static property in the method area.
The object referenced by a constant in the method area.
The object referenced by the JNI (that is, the generally said native method) in the local method stack.


How to recycle garbage (garbage collection algorithm)

There are at least three different algorithms here.

Tag-Recycle algorithm

The first step: according to the above search algorithm, determine those objects should be cleared, and mark the object
Step two: Clear the object based on the first step of the tag.
As follows:

The flaw in this algorithm is obvious: there is a problem with memory fragmentation.

Replication Algorithms

The premise of the algorithm is to divide the memory area into two parts and use only one part at a time
When recycling trash
The first step: Copy the surviving objects in the in-use section to the second block of memory
Step two: Completely erase the first block of memory.
As follows:


The advantage of this algorithm is that there is no fragmentation, but a disadvantage but a high cost to the use of memory space, because the memory can be used to shrink to half the original. Moreover, the efficiency of the replication algorithm is very much related to the number of surviving objects, and if there are many surviving objects, the efficiency of the copying algorithm will be greatly reduced.

Marker Grooming algorithm

This algorithm is similar to the tag-recovery algorithm
The first step: Mark out the surviving object, the garbage object.
Step two: Move the surviving object to one end of the memory.
Step three: Clear out the memory outside the boundary.
As follows:


Method of generational recovery

The generational situation of the heap has been mentioned above.
In the new generation we are using a replication algorithm.
Laosheng uses the tag-collation algorithm or the tag cleanup algorithm.
This involves a new problem, how is the object allocated in memory?
If it is a small object, it is placed directly into the new generation of Eden and a survivor area, and if it is a large object, it is placed directly into the Laosheng generation.
If it is not enough to allocate memory in the new generation, the copy algorithm is used to replicate the surviving objects of Eden and one survivor zone to another Suvivor area.
That is to say, 10% of the area to store 90% of the content.
Can you put this down?
In most cases, the answer is yes,because IBM studies have shown that 98% of new-generation objects are dying in the face of life.
So what if the Survivor area, which accounts for only 10%, does not have enough surviving objects in the 90% region?
The answer is:Put these objects into the old age.
If an object of the new generation has gone through 15 garbage collections, it is not dead, so put it in the Laosheng generation.
What if the new generation, the Laosheng are full?
Is there another anomaly called Outofmemoryerror:java heap space?

In addition, there are two types of garbage collection actions:
New Generation GC (Minor GC): Refers to the garbage collection action occurring in the Cenozoic, because most Java objects have the characteristics of being born and going out, so the Minor GC is very frequent, and the general recovery speed is relatively fast.
Old age GC (Major gc/full GC): Refers to the GC, which occurred in the old age, Major GC, often accompanied at least once Minor GC (but not absolute, in the Parallelscavenge Collector's collection strategy is directly Major GC Policy selection process). The speed of the MAJORGC is generally 10 times times slower than the Minor GC.
So big object, what is the boundary of the small object? How big, how small? The JVM has a parameter to set this value, and everyone is interested in Baidu.

In the next section, we take a look at a few GC instances and the GC report reading


Resources

Deep understanding of Java Virtual Machines Chapter III

Http://www.cnblogs.com/dolphin0520/p/3783345.html
Http://www.th7.cn/Program/java/201409/276272.shtml
Http://www.cnblogs.com/gw811/archive/2012/10/19/2730258.html










Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Garbage collection in Java

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.