Analysis on the garbage collection mechanism in java and java

Source: Internet
Author: User

Analysis on the garbage collection mechanism in java and java

The garbage collection mechanism is an essential issue for java programmers to interview. Some people may think that since JVM automatically manages the memory and allocates it, garbage collection should also be performed automatically by JVM, so do programmers still need to consider this issue? The answer is: yes. The reason is simple:When you need to troubleshoot various memory overflow and Memory leakage problems, when garbage collection becomes the bottleneck of high concurrency performance of the system, we need to implement necessary monitoring and adjustment for the automatic mechanism of these JVMs.In general, garbage collection (GC) mainly completes three tasks:

  • Which memory needs to be recycled?
  • When will it be recycled?
  • How to recycle it?
1. How to Determine useless objects

As mentioned before, when JVM allocates memory, the heap memory has the largest allocated space, because the objects generated by the program running are basically stored in the heap memory.GC mainly targets garbage collection in heap memory.So how can we determine the "garbage" in the heap memory, that is, useless objects? Here is a brief introduction:

  • Reference COUNTING METHOD

    Reference count is an early policy in the garbage collector. In this method, each object instance in the heap has a reference count. When an object is created and assigned to a variable, the variable count is set to 1. When any other variable is assigned a reference value to this object, the count is incremented by 1 (a = B, then the counter of the object instance referenced by B + 1 ), however, when a reference of an object instance exceeds the lifecycle or is set as a new value, the Reference Counter of the object instance is reduced by 1. Any object instance with a reference counter of 0 can be collected as garbage. When an object instance is garbage collected, the Reference Counter of any object instance it references is reduced by 1.

    Advantage: The reference counting collector can be executed quickly and intertwined in the program running. It is advantageous for the real-time environment where the program needs not to be interrupted for a long time.

    Disadvantage: Circular references cannot be detected.

  • Accessibility Analysis

    Accessibility analysis is also called the ROOT search algorithm. This method is introduced from Graph Theory in discrete mathematics. The program regards all referenced relationships as a graph starting from a node gc root, find the corresponding reference node. After finding this node, continue searching for the reference node of this node. After all the reference nodes are found, the remaining nodes are considered to be unused nodes.

    Java can serve as the GC Root object:

    1. Objects referenced in the VM stack (local variable table)

    2. Objects referenced by static attributes in the Method Area

    3. Objects referenced by constants in the method Area

    4. Native object referenced in the local method Stack)

2. Garbage collection Algorithm

Since the implementation of the garbage collection algorithm involves a lot of program details, and different virtual machines have different memory operations methods, the implementation of the algorithm is not discussed here, it just briefly introduces the basic idea of several garbage collection algorithms.

  • Mark clearing method

    The mark-clearing algorithm scans the root set and marks the surviving object. After marking, It scans the unlabeled objects in the whole space for recycling, as shown in. The tag-clearing algorithm does not need to move objects and only processes non-surviving objects. It is extremely efficient when there are many surviving objects, however, because the tag-clearing algorithm directly recycles non-surviving objectsMemory fragmentation.

  • Mark sorting method

    The tag-sort algorithm uses the tag-clear algorithm to mark objects. However, when the object is cleared, all the surviving objects are moved to the left free space and the corresponding pointer is updated. The tag-sorting algorithm moves Objects Based on the tag-clearing algorithm. Therefore, the cost is higher, but the memory fragmentation problem is solved. In the implementation of collectors Based on the Compacting algorithm, the handle and handle tables are generally added.

  • Replication method

    The replication algorithm is proposed to overcome the handle overhead and solve the garbage collection of heap fragments. At the beginning, it divides the heap into one object plane and multiple idle planes. The program allocates space for the object from the object plane. When the object is full, garbage Collection Based on the copying algorithm scans activity objects from the root set and copies each activity object to the idle surface (so that there is no idle hole between the memory occupied by the activity object ), in this way, the idle surface becomes the object surface, and the original object surface becomes the idle surface. The program will allocate memory in the new object surface. A typical garbage collection Algorithm Based on the coping algorithm is the stop-and-copy algorithm, which divides the heap into the object plane and the free area plane. During the switching process between the object plane and the free area, the program is suspended.

  • Generation collection method

    The generational garbage collection policy is based on the fact that:Different objects have different lifecycles.. Therefore, objects of different lifecycles can adopt different recycling algorithms to improve the efficiency of recycling.

Young Generation)

1. All newly generated objects are first put in the young generation. The goal of the young generation is to quickly collect objects with short lifecycles as much as possible.

2. The new generation memory is divided into one eden zone and two VOR (same vor0, same vor1) zones according to the ratio. One Eden zone and two vor zones (generally ). Most objects are generated in the Eden area. When recycling, copy the surviving objects in the eden area to a region vor0, and then clear the eden area. When the region vor0 is full, then, copy the surviving objects in the eden and region vor0 areas to another region vor1, and then clear the eden and region vor0 areas. At this time, Region vor0 is empty, and then switch between region vor0 and region vor1, that is, keep region vor1 empty.

3. When region vor1 is insufficient to store the surviving objects of eden and region vor0, the surviving objects are directly stored in the old age. If the old age is Full, a Full GC will be triggered, that is, the new generation and old generation will be recycled.

4. the GC of the new generation is also called Minor GC. the occurrence frequency of MinorGC is relatively high (not necessarily triggered when the Eden zone is full)

Old Generation)

1. objects that are still alive after N garbage collection in the young generation will be put into the old generation. Therefore, it can be considered that objects with long lifecycles are stored in the old generation.

2. the memory size is much larger than that of the New Generation (about). When the old generation has Full memory, the Major GC is triggered, that is, Full GC. The frequency of Full GC is relatively low, objects in the old age have a long survival time and a high survival rate.

Permanent Generation)

It is used to store static files, such as Java classes and methods. Persistent generation has no significant impact on garbage collection, but some applications may dynamically generate or call some classes, such as Hibernate, in this case, you need to set up a large persistent storage space to store the classes added during the running process.

3. inevitable Memory leakage

Although we have a well-developed garbage collection mechanism, memory leakage still occurs during java program running:

  • Static collection classes such as HashMap and Vector are most prone to memory leakage. The lifecycle of these static variables is the same as that of the application, and all Object objects cannot be released, because they will also be applied by Vector and so on.
  • Various connections, database connections, network connections, IO connections, and so on are not shown to be closed by calling close. GC is not collected, leading to memory leakage.
  • The use of listeners may also cause memory leakage when the listener is released without deleting the listener.

(Reference: http://www.cnblogs.com/andy-zcx/p/5522836.html)

 

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.