How Java garbage collector works

Source: Internet
Author: User

How does the Garbage Collector work? Let me give a brief introduction.

First, we need to clarify the following points:

Java allocates space for objects on the heap.

The garbage collector is only related to memory. What is Io, network connection, and P?

When the number of available memory is low, the sun version of the garbage collector will be activated

Before the Garbage Collector recycles garbage, let's take a look at how Java allocates objects. The Java heap is more like a conveyor belt. Every time a new object is allocated, it moves one cell forward. This means that the object storage space is allocated quite quickly. Java's "heap pointer" is simply moved to an unallocated field. That is to say, when allocating space, the "heap pointer" only moves forward in sequence, regardless of whether or not the subsequent objects are to be released. If the program exits before the available memory is exhausted, the garbage collector will not be activated.

However, since the "heap Pointer" only moves forward sequentially, you will surely think that one day the memory will be exhausted and the garbage collector will start to release the memory. Someone may ask: how can I determine whether an object should be recycled? The answer is that when the stack or static storage area does not reference this object, it indicates that the Program (member) is not interested in this object, and it should be recycled. There are two ways to know whether the object has been referenced: first, to retrieve the reference of the object on the traversal stack, and second, to retrieve the object by traversing the reference of the stack or static storage area. The implementation of the former is called the reference counting method, which means that when a reference is connected to an object, the reference count is incremented by 1. When the reference leaves the scope or is set to null, the reference count is reduced by 1, this method has a defect. If there is a circular reference between objects, there may be a situation where "the object should be recycled, but the reference count is not zero.

Java uses the latter. In this way, the Java virtual machine uses an adaptive garbage collection technology to process the found surviving objects (that is, not garbage ), java has two methods:

One is "stop-Copy": theoretically, the program is paused first (so it does not belong to the background recycle mode), and then all the surviving objects are copied from the current heap to another heap, all the data that has not been copied is junk. When objects are copied to the new stack, they are one by one, so the new stack keeps in a compact arrangement (that is why the "heap Pointer" only moves forward when objects are allocated ). Then you can simply and directly allocate the memory as described above. This will lead to a large number of memory replication behaviors. memory allocation is based on a large "Block. With blocks, the garbage collector can copy objects directly to discarded blocks instead of copying objects to the heap.

The other is "mark-sweep": the idea is to traverse all references from the stack and static storage areas, and then find all the surviving objects. Every time it finds a surviving object, it will mark the object. No objects will be recycled in this process. When all the objects are marked, the unmarked objects will be released without any replication. Therefore, the remaining heap space is not continuous, and the garbage collector reassembles the remaining objects, make them arranged consecutively.

When the garbage collector is started for the first time, it executes "stop-Copy" because there is too much garbage in memory at this time. Then, the Java Virtual Machine monitors all objects. If all objects are stable and the efficiency of the garbage collector is reduced, switch to the "mark-sweep" method. Similarly, the Java Virtual Machine tracks the "mark-clean" effect. If there are many fragments in the heap space, it switches to the "stop-Copy" mode. This is the so-called "Adaptive" technology.

As a matter of fact, "stop-Copy" and "mark-clean" are nothing more than "find clean items in a large amount of garbage and find garbage in a large number of clean items ". Different Environments use different methods. This is done to improve efficiency. You must know that Java will suspend the program first in either way. Therefore, the efficiency of the garbage collector is actually very low. Java efficiency is converted back to the flexibility of the garbage collector and runtime that C ++ does not have. I think this is a wise choice (although it is only related to memory). With the rapid development of hardware, I believe that, development time is much more important than running efficiency!

Reference: http://fhz1980.blog.163.com/blog/

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.