Java garbage collection

Source: Internet
Author: User

Working principle: maintain a reference count for each memory object.

When a new reference points to an object, the reference count of the object is added. When the reference pointing to the object is destroyed, the count is reduced by one. When the count is zero, reclaim the memory resources occupied by the object.

Defect: The reference count must be modified every time the memory object is referenced or the reference is destroyed. Such operations are called footprint. The reference count footprint is very high. This greatly affects the overall performance of the program. Therefore, most modern programming languages do not apply the reference count as the garbage collection implementation algorithm.

In addition, the reference counting has a fatal defect. When a sequential loop reference occurs in a process, the reference counting algorithm cannot detect it, and the memory objects referenced by the loop become memory objects that cannot be recycled. This causes memory leakage.


Example:

class A{  public B b;   }class B{  public A a;}public class Main{    public static void main(String[] args){    A a = new A();    B b = new B();    a.b=b;    b.a=a;    }}

At the end of the function, the counts of A and B are both 2.

Undo a first, and then the Count of A is 1, waiting for B. A to undo the reference of a, that is, waiting for B to undo

The same is true for B.

Both objects are waiting for the other party to undo, and all these two resources cannot be released.


  • Mark-sweep)
    This algorithm is executed in two phases. In the first stage, all referenced objects are marked from the reference root node. In the second stage, the whole heap is traversed to clear unmarked objects. This algorithm suspends the entire application and generates memory fragments.
  • Copy)
    This algorithm divides the memory space into two equal regions and uses only one of them at a time. During garbage collection, traverse the current region and copy the objects in use to another region. The secondary algorithm only processes objects in use at a time, so the replication cost is relatively small. At the same time, the corresponding memory can be organized after the replication, but the "Fragmentation" problem occurs. Of course, the disadvantage of this algorithm is also obvious, that is, it requires two times of memory space.
  • Mark-compact)
    This algorithm combines the advantages of "tag-clear" and "copy" algorithms. It is also divided into two phases. In the first phase, all referenced objects are marked from the root node. In the second stage, the whole heap is traversed to clear unlabeled objects and compress the surviving objects to one of the heap, discharge in sequence. This algorithm avoids the "tag-clear" fragmentation problem and avoids the space issue of the "copy" algorithm.
  • Incremental collecting)
    The garbage collection algorithm is implemented, that is, garbage collection is performed simultaneously by the application. I don't know why the Collector in jdk5.0 does not use this algorithm.
  • Generational collecting)
    Based on the garbage collection algorithm obtained after object lifecycle analysis. Objects are divided into young, old, and persistent generations. Different algorithms (one of the above methods) are used to reclaim objects in different lifecycles. The current garbage collector (beginning with j2se1.2) uses this algorithm.

  • 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.