Android garbage collection mechanism

Source: Internet
Author: User

1. The core idea of garbage collection algorithm

The Java language provides an automated GC mechanism that frequently checks memory and uses object reference counting to reclaim objects with a reference count of 0. This prevents two dangers: (1) prevents useless objects from taking up memory resources (2) to prevent the use of useful objects from being freed, causing an illegal memory reference.

2. Conditions that trigger GC (garbage Collector)

(1) When the application process is idle, the GC reclaims the memory resources of the idle process.

(2) When the application process is busy, when the required memory resources are insufficient, the GC enforces a low-priority process resource, and if it is insufficient, it is reclaimed two times or is not sufficient to be reported to Oom.

3. Measures to reduce GC overhead

(1) to call System.GC () as little as possible;

(2) Reduce the reference to temporary objects; After the temporary object exits the function,

(3) After the object is used, it is set to point to null, which makes it easier for the system to find empty objects and recover memory faster.

(4) can use the basic data types such as int, as far as possible do not use the integer and other reference types, the basic type occupies a much smaller resource than the reference type;

(5) Use static variables as sparingly as possible. The static variable is global and the system allocates memory for it in the heap, and the GC cannot reclaim the memory;

(6) If you want to use variable-length string variables, try to use StringBuffer instead of string. Each time a string is assigned, the memory is reassigned once, string str = STR1+STR2+STR3+STR4+STR5, and each more "+" will be more

Creates an object.

(7) The time when objects are created and deleted. Creating or deleting too many objects at once can cause the memory to suddenly become tense or disposable too, not using the proper use of memory.

Android garbage collection mechanism

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.