Java garbage collection

Source: Internet
Author: User

Garbage collection is a major difference between Java and C ++.ProgramIt is an important guarantee that no pointer is needed. In C ++, when an object is no longer valuable, We need to manually clear the object to release space. In Java, this job is automatically completed by the garbage collector, and programmers do not have to consider difficult object recycling issues.

I. Benefits of garbage collection:
Garbage collection frees programmers from the burden of releasing memory, allowing them to focus more on programming and logic, improving efficiency.
Garbage collection ensures the normal operation of the program and does not cause many problems such as memory leakage caused by the forgotten release of objects in C ++.

Ii. Basic Methods for garbage collection:
All garbage collectionAlgorithmThere are two things to do: Check the spam object and delete the spam object, and return the heap space to the program.
1. Reference Counter:
Each object in the heap has a corresponding counter. when an object is created and Its Reference is specified to a variable, the reference timer is 1. When this object is referenced by another object, the counter is + 1. the reference of other objects to this object is canceled, and the counter is-1. When the counter is 0, this object is no longer referenced, and the garbage collector can collect it.
The reference counter is used in the early garbage collection policy. The counter cannot process circular references (the parent object references sub-objects, and the self-object also references the parent object. These two objects cannot be collected using the reference counter ).
2. Tracking COLLECTOR:
The tracking collector maps objects and references in the heap into a graph. The object is the node of the graph, and the reference between objects is the edge of the graph.
Garbage collection starts from the heel node and finds the isolated nodes in the figure by repeating the graph, that is, the spam object.

Iii. Garbage collection policy for Handling Heap fragments:
When an object is recycled, its heap space return program will encounter a problem when the object is recycled, and the space in the heap is not continuous. In this way, when creating a new object, you may have to allocate continuous space due to insufficient space in the front of the heap. This will not only waste the heap space, but may also cause insufficient space, memory overflow.
Generally, the following recyclers are used to solve this problem:
1. Compression COLLECTOR:
When the compression collector moves the active object to the heap segment, a large number of blank spaces appear on the other end of the heap.

2. Copy the collector:
The copy collector copies the active objects to a new region. During the copy process, these active objects are placed in a new region, which eliminates the gaps in the old region. When the new area is full, copy the active object to the old area.
Because object copying requires the program to stop running, copying objects in the region takes a long time and affects the normal operation of the program.


3. By-generation COLLECTOR:
In a program, large objects have the following characteristics: Most objects have short lifecycles and a small part of objects have long lifecycles. For this reason, the heap can be divided into several sub-heaps, each of which is called a generation (from low to high generation, the second generation of three generations ......). Objects with short lifecycles are placed in the base generation, and objects with long lifecycles are placed in the high generation. In this way, objects in the base generation will be collected more frequently, while objects in the higher generation will be collected less frequently, ensuring the collection efficiency. During each collection, if an object in this generation is found to be active, put it in a higher generation; otherwise, the object will be collected.
4. Adaptive COLLECTOR:
Adaptive collection selects an appropriate collector or adjusts collector parameters based on the conditions of objects in the heap to achieve higher efficiency.

The above is the basic method for Java garbage collection. If you have any shortcomings or errors, please let us know.

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.