About java garbage collection

1. Background

Traditional C or C ++ programmer is responsible for the recovery of allocated memory, which is displayed for garbage collection. Because the programmer does not know when to release memory should be, it will lead to some recovery can not be allocated out of memory, resulting in "memory leaks." Cause the system to slow down, paralyzed program.

2. What is a memory leak?

Some can not be allocated out of the memory recovery, cause the system to slow down, and even program the phenomenon of paralysis.

3. Display the shortcomings of garbage:

1) Forget the timely recovery program useless memory, causing memory leaks and reduce system performance

2) core libraries program error recovery procedures, resulting in a system crash

The 4.java garbage collection mechanism, which is GC (Garbage Collection)

Java memory allocation and recovery procedures are carried out by the JRE automatically in the background, this mechanism is called "garbage collection, Garbage Collection, namely GC.

JRE provides a hyperthreading for testing and control, it is generally automatic garbage collection when the CPU is idle or insufficient memory, because programmers can not control the garbage collection time and sequence.

java heap memory is a runtime data area for storing "object", i.e., instance of the class. All objects in the program are on the stack memory, heap memory is usually by the garbage collection mechanism responsible. Garbage collection is a dynamic storage management technology, it will automatically release the "object is no longer referenced by the program," garbage in a particular algorithm.

5.java garbage collection mechanism is mainly recovered two situations:

(1) When no object references to the original memory allocated to an object, that memory has become garbage. When an object is no longer referenced, the memory space will be reclaimed

(2) Garbage collection can also clear the memory record fragmentation. Since creating objects and garbage collector release the memory space occupied by discarding objects, memory will be fragmented. "Fragment" refers to a region between the free memory block of memory allocated to the object, the processing will take up the debris heap memory into one end of the stack, the JVM will be ordered in memory reassigned to the new object.

6. The advantages of garbage collection

(1) improve programming efficiency. Programmers do not need to spend time on memory garbage.

(2) protect the integrity of the program is an important part of the java security policy.

7. drawback of garbage collection

Garbage collection affect program performance. Because java virtual machine must keep track of the program useful objects to determine which objects need to recover, and this way it will take some time processor

8. The garbage collection features;

(1) garbage collection goal is to recover unused memory space objects, this memory is memory space in the JVM garbage collection to reclaim memory only for physical resources, such as database connections, disk IO and other resources powerless.

(2) by the value to null object reference to the object is recoverable objects imply

9. programmer manual garbage collection and free up memory space:

Garbage collection is performed via the object .finalize () or System.gc ()

Guess you like

Origin www.cnblogs.com/yuxiangyuan-cloud/p/12167922.html