Analysis on GC Principles of ART virtual machines and gc Principles of art virtual machines

Source: Internet
Author: User

Analysis on GC Principles of ART virtual machines and gc Principles of art virtual machines

1. Garbage Collection Mechanism

Garbage Collection is a set of automatic memory management mechanisms provided by virtual machines in Android systems.

Traditional C/C ++ does not have GC. to initialize an object and allocate memory space, you need to manually malloc/new it. To destroy an object, you need to manually free/delete it.

The garbage collection mechanism can determine the "garbage" in the memory space based on certain rules and automatically release the memory occupied by the "garbage. However, the cost is that GC consumes additional system resources and suspends user processes. In severe cases, the user experience is significantly affected, such as system freezing and animation frame dropping.

Once Memory leakage or overflow occurs, if developers do not understand the VM memory management mechanism, solving such problems becomes tricky.

2. GC algorithm of the ART Virtual Machine

After Android 5.0, Google replaced the Dalvik virtual machine with the ART virtual machine. In order to better utilize the features of the Automatic Memory Management of the ART virtual machine, it is necessary for application developers to thoroughly understand the implementation of the garbage collection mechanism of the ART virtual machine.

Mainstream GC algorithms include Reference counting, Mark-Sweep GC, and Copying GC) mark-Compact GC, followed by tracing algorithms.

Reference counting [excessive memory consumption when system references are complex]

Mark-clear Algorithm

1 mark stage

First, mark the root SET object, including the objects in the current thread stack, all loaded classes of the current process, and static references in the class. Then, recursively mark all reachable objects from the root SET object.

2. Suspension phase

The marking phase does not pause other threads when marking the root set, so the reference relationship of the object may change during the marking process, if you do not handle this change, the object may be incorrect or missing. When cleaning up the garbage, the surviving object may be recycled, resulting in Abnormal Running of the program. Therefore, a pause phase is required to process objects whose reference relationships change during the marking phase.

3. Clearing stage

After the previous two stages, all the surviving objects identified by the Mark clearing algorithm are marked in the Mark Bitmap. Next, we will recycle unmarked memory blocks.

4. Final Stage

There is a final task after garbage collection, which will crop the heap after garbage collection as needed, and reset the reserved idle memory of the heap.

Mark-Compression Algorithm

Copy Algorithm

3. Heap Structure of the ART Virtual Machine

Before learning about the recycle mechanism, you must first understand the internal distribution of the memory heap, for example:

4. Memory GC policy of the ART heap

The biggest drawback of the Mark clearing algorithm is that it can easily cause memory fragments. The consumption of system memory is also huge when the algorithm is run every time. To optimize the original Java memory tag clearing algorithm, ART provides three GC policies:

Generation garbage collection Sticky GC: only the memory applied between the last GC and the current GC is recycled. Partial garbage collection (Partial GC): Collects memory garbage except Image Space and Zygote Space. Global garbage collection Full GC: Memory garbage in Space other than Image Space.

Comparison of policies:

GC pause time: Sticky GC <Partial GC <Full GC efficiency: Sticky GC> Partial GC> Full GC

Three phases of GC execution:

Phase 1: A lightweight GC is performed first, and allocation is attempted after GC is completed. If the allocation fails, select the next GC policy and perform a lightweight GC again. After each GC is completed, the allocation is attempted until all three GC policies are round-robin and the allocation cannot be completed. Phase 2: allocate objects when the heap is allowed to grow. If the allocation still fails, a GC with soft references allowed to be recycled will be performed. Stage 3: if the object is a Non-movable object, ART will allocate it to the Non-Moving Space. If the object is a movable object, a homogeneous Space (Main Space) is compressed, which increases the possibility of successful allocation.

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.