Summary of Java garbage collection mechanism

Source: Internet
Author: User


First, there are a few things to be clear:
        first: Before the garbage collector reclaims garbage, let's take a look at the way Java allocates objects, the Java heap is more like a conveyor belt, and each time a new object is assigned, it moves one grid forward. This means that the                  allocation of object storage space is fairly fast. Java's heap pointers simply move to areas that are not yet allocated. In other words, when allocating space, the "heap pointer" moves forward in turn regardless of whether the object behind is still being released                put it away. If the program quits before the available memory runs out, then the garbage collector will not be activated at all. But as the "heap pointer" moves forward in turn, you will think that there is always a               The day memory is depleted and the garbage collector begins to release the memory. Someone here is sure to ask: how can you tell if an object should be recycled? The answer is that when the stack or static store does not have a reference to this object, the table                  shows that the program (member) is not interested in this object, it should be recycled. There are two ways to know if this object is referenced: The first is to iterate over the objects on the heap to find the reference; the second is to traverse the stack or static save                the reference of the storage area to find the object. The former is called "reference counting", meaning that when a reference is connected to an object, the reference count is plus 1, and the reference count is reduced by 1 when the reference leaves the scope or is set to NULL, a method that                There is a flaw, if there is a follow up between objectsRing reference, you may receive a situation where the object should be recycled, but the reference count is not zero.

Second: Java uses the latter, in which case the Java Virtual Machine employs an "adaptive" garbage collection technology, how to handle the surviving objects found (i.e. not garbage),
There are two ways to Java:
One is "Stop-copy": Theoretically pausing the program (so it is not part of the back-end recycle mode), and then copying all the surviving objects from the current heap to another heap, all of which are not replicated. When objects are copied to the new heap, they are one next to the other, so the new heap stays in a tight arrangement (which is why the "heap pointer" moves forward as the object is allocated). Then you can simply and directly allocate the memory as described in the preceding method. This results in a large amount of memory replication behavior, and memory allocations are in large "block" units. With the block, the garbage collector can copy objects out of the heap without copying them directly to the discarded blocks.

The other is "mark-sweep": The idea is also to start from the stack and the static storage area, traversing all the references, and then find all the surviving objects. Whenever it finds a surviving object, it gives the object a token. No objects will be reclaimed in this procedure. When all tags are complete, objects that are not marked are freed and no replication work occurs, so the remaining heap space is discontinuous, and the garbage collector rearranges the remaining objects so that they are contiguous.

When the garbage collector first starts, it performs "stop-copy" because there is too much garbage in memory at this time. Then the Java Virtual opportunity is monitored, if all objects are stable and the garbage collector is inefficient, switch to tag-sweep; Similarly, the Java Virtual Opportunity tracks the "tag-sweep" effect and switches to stop-copy mode if there is a lot of fragmentation in the heap space. This is the so-called "adaptive" technology. In fact, think about it, "stop-copy" and "Mark-sweep" is simply: "In a lot of rubbish to find clean things and in a lot of clean things to find rubbish." Different environments in different ways, this is done entirely to improve efficiency, you know, either way, Java will first suspend the operation of the program, so the garbage collector's efficiency is actually very low.




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.