Java garbage collector (GC) Analysis

Source: Internet
Author: User

People who know java must be familiar with GC. GC is the garbage collector. We all know that java has the function of automatic memory management, and programmers do not have to manually release the memory. This article briefly introduces the garbage collector.GC task:Which memory needs to be recycled? When will it be recycled? How to recycle it?What memory does GC recycle?Previously, we introduced various parts of the Data zone during java runtime, including program counters, java Virtual Machine stacks, local method stacks, stacks, and method zones. Among them, the program counters, virtual machine stacks, and local method stacks are generated with threads, and are destroyed with threads. Stack frames in the stack are methodically executed as methods enter and exit. The amount of memory allocated in each stack frame is known after the class structure is determined. Therefore, the memory allocation and recovery in these regions are deterministic, in these regions, you do not need to think too much about recycling, because the memory will naturally be recycled when the method ends or the thread ends. The java heap and method areas are different. We can only know what objects will be created when the program is running. The allocation and recycling of this part of memory are dynamic, GC focuses on this part of memory.How can I determine whether an object is alive (how to mark garbage )?
To recycle garbage, GC must first know which objects can be recycled. So how does GC mark spam objects? In java, the root search algorithm is used to determine whether an object is alive. The basic idea of an algorithm is to use a series of objects named "GC Roots" as the starting point, and start to search down from these nodes. The paths searched are called "reference chains ", when an object to GC roots is not connected with any reference chain (GC roots cannot reach this object), it proves that this object is unavailable. It can be used as the GC roots object:1. Objects referenced in the VM stack (the local variable table in the stack frame.2. Objects referenced by static attributes of classes in the method area.3. Objects referenced by constants in the method area.4. Objects referenced by JNI in the local method.
Note: Reference TypeReferences can be divided into strong references, soft references, weak references, and virtual references. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Release/0v3Tw6Os1rvT0Me/release + release/tsi4/release/z1rvE3Mn6tOa1vc/release + release/zyse38dPQ0OnS/ authorization + rTmyrG85Lm5s8nTsM/examples + wKy7 + MrVvK/examples + 8Dty + examples/examples + o7rK18/Iseq8x7Smy/examples/templates + Wz/examples/ examples/1 bzky + nGrLn9tuC/examples + 087bUz/PKsc7et6jV0rW91 + O5u7XEwazQ + MTatOa/examples/jK1byvtq/examples = "http://www.2cto.com/uploadfile/Collfiles/20140315/20140315093400209.jpg" alt = "\">
Copy Algorithm: Divide the available space by capacity into two equal parts, each using only one of them. When the memory is used up, the surviving objects are copied to the other one, and the used memory is cleaned up once. In this way, memory is recycled every time, which will not cause a lot of memory fragments. Of course, the actual situation may not be. But it remains unchanged. When the object survival rate is high, the replication algorithm performs many replication operations, which reduces the efficiency.

Tag-Sorting Algorithm: Like the first step of mark-clear, it is to mark the junk object first, but subsequent operations do not directly clean the recyclable object, instead, all the surviving objects are moved to one end, and the memory outside the end boundary is cleared directly.

Generational collection Algorithm: The memory is divided into several parts based on the lifecycle of the object. Java heap is generally divided into the new generation and the old generation, so that the most suitable collection algorithm can be used according to the characteristics of each age. In the new generation, when a large number of objects are found dead every time spam mobile phones exist, and only a small number of objects survive, the replication algorithm is used. In the old age, because the object has a high survival rate and there is no additional control to allocate a guarantee for it, you must use the "mark-clear" or "mark-sort" algorithm for recovery.




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.