Java garbage collection mechanism learning experience

Source: Internet
Author: User

This article is a summary of my knowledge of the Java garbage collection mechanism, in particular thanks to http://www.cnblogs.com/andy-zcx/p/5522836.html and http://blog.csdn.net/ zsuguangh/article/details/6429592 's blog.

    • memory leak : A memory leak refers to a memory space that is not recycled after it has been exhausted. In general, memory leaks in Java are due to the memory object lifetime exceeding the length of time it exists in the program
      • Garbage collection Meaning: Solve the memory management problem that need to be considered in programming, solve the memory leak problem effectively, make full use of free memory space. Java objects no longer have scope concepts, only references to objects have scopes.
    • Basic Operations : (1) Find useless objects, (2) Reclaim space occupied by useless objects, and release the space that the program can use again.
    • payback time : Garbage collection occurs at a time that is unpredictable and generally has two kinds of timing:
      1. The programmer calls the JVM's GC interface, but only informs the system that it wants garbage collection, and the time is determined by the system;
      2. If large chunks of memory are needed while the program is running, and the block is not large enough at this time, the system calls the GC for garbage collection.
    • Recovery Algorithm :
  1. Reference counting method
    • Content: When each object is created, the object instance is assigned to a variable (referred to as a reference counter), the value of the variable count is set to 1, and when the object is referenced, the value is incremented by 1, and the reference counter is reduced by 1 when a reference to an object instance exceeds the life cycle or is set to another value. When the reference counter value becomes 0 o'clock, it can be used as garbage collection. (Note: The object instance may also refer to other objects, so when the object is recycled, the counters for all objects it references are reduced by 1.)
    • Evaluation: can be executed more quickly, in real-time system is more convenient, but can not solve the circular reference problem. For example, object A and object B reference each other, but the values of a and B are assigned null, and the objects referenced by A and B are no longer accessible, but they reference each other, causing their reference counter values to be not 0, at which time the GC will never reclaim them.
  2. Root Search algorithm
    • Content: Use all the reference relationships in the program as graphs, starting with the root node GC root object, looking for the node that corresponds to the reference, and so on, all the reference nodes make up a graph, and the rest of the nodes are considered to be unreferenced nodes, that is, useless nodes. The GC root objects in Java are: objects referenced in the Java Virtual machine stack, objects referenced by static properties in the method area, objects referenced by constants in the method area, objects referenced in the local method stack
  3. Tag-Purge algorithm
    • Content: Scans from the root collection, tags the surviving objects, scans all the unmarked objects, and recycles them.
    • Evaluation: You do not need to move objects, only the non-surviving objects, when the survival of the object is more efficient, but will cause memory fragmentation.
  4. Tagging-sorting algorithms
    • Content: On the basis of algorithm 3, after reclaiming the useless objects, all the surviving objects are moved to the free space, and the free memory is organized.
    • Evaluation: Overhead costs increased, but the fragmentation problem was resolved.
    • Note: Algorithms 3, 4, and 5 are root search algorithms, which are described separately for ease of understanding.
  5. Copying algorithm
    • Content: Divides the memory into the object area polygon and the free area polygon, scans in the object area, then copy all valid nodes of the object area to the idle area, then releases the original object area, and the original object area becomes the idle area. Garbage collection is done in the switch between the object area and the idle area.
    • Evaluation: Reduces the collection of handles and does not fragment, but the program needs to be paused during the switchover.
  6. split-generation algorithm
      • content: Based on different object lifecycles, the objects are divided into three generations: New generation, old generation, and durable generation.
      • Cenozoic:
        • All newly generated objects are first placed in the younger generation. The goal of the young generation is to collect as quickly as possible those objects with short life cycles.

        • The Cenozoic memory is divided into one Eden area and two Survivor (Survivor0,survivor1) regions according to the 8:1:1 ratio. One Eden area, two survivor districts (in general). Most objects are generated in the Eden area. The Eden zone survivor is copied to a Survivor0 area before being reclaimed, and the Eden area is emptied, and when the Survivor0 area is fully stocked, the Eden and Survivor0 zone survivors are copied to another Survivor1 area. Then empty the Eden and the Survivor0 area, where the Survivor0 area is empty, and then swap the survivor0 and Survivor1 areas, that is, to keep the Survivor1 area empty,  so reciprocating.

        • When the Survivor1 area is not sufficient to hold the surviving objects of Eden and Survivor0, the surviving objects are stored directly in the old age. If the old age is full, it will trigger an all-time GC, that is, the new generation, the old generation to recycle

        • The new generation of GC is also called minor GC,MINORGC frequency is higher (not necessarily when the Eden area is full)  

      • old generation:
        • The objects that survived after n garbage collection in the younger generation will be placed in the old generation. Therefore, it can be considered that older generations are storing objects with longer life cycles.

        • Memory is much larger than the new generation (approximately 1:2), and when the old memory is full, the major GC that triggers the Gc,full GC is relatively low, the age of the older objects is longer, and the survival rate is high.

      • persistent:
        • is used to store static files, such as Java classes, methods, and so on. Persistent generations have no significant impact on garbage collection

Java garbage collection mechanism learning experience

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.