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