Garbage collection is used to achieve automatic memory management, which is different from manual management ). Problems with manual memory management: 1) suspension pointer, dangling pointer2) repeated recovery, double free3) Memory leakage, Memory Leak
HistoryJohn McCarthy invented the concept and technology of garbage collection in 1959 and applied it to the List Language. In 1959, computer science was still in its infancy, but the great god has invented garbage collection, and the great god is the great god.
PolicyCurrently, there are two types of garbage collection policies: 1) Reference technology reference counging: Add a reference counter for each object. Each time you add a reference to a team object, the counter increases. If the counter is less referenced, the counter decreases. If the counter is 0, the object can be recycled.
This method is simple and fast, and code collection and execution are synchronized. The biggest problem is that it cannot solve the cyclic reference problem ).
2) tracking objects: stores all allocated objects in the system. Some objects are called root objects ). All objects connected to and reachable from the following objects are called active objects. The remaining objects are spam objects that can be recycled. The implementation is complicated, and the recycling time and running time are uncontrollable (the recycling code runs separately from the program code, and the recycling code will block the running of the program code), but it can solve the problem of loop reference.
Every policy has been used systematically: IOS, Python, COM, C ++ Library (auto_ptr, etc.) using objects such as Java,. net, and lisp.
Algorithm
This section describes object tracing algorithms.
Mark-clearing method, Mark-sweepThis was invented by John McCarthy. Garbage collection is divided into two phases: the mark phase: To find all active objects, and traverse from the following object. All active objects are represented by a flag. Clear stage: traverses all objects. if the object is not active, reclaim and release space. The cleared Memory has fragmentation ).
Copy algorithm, copyingAlso called C
Heney's AlgorithmInvented by C. J. Cheney, around 1970. The idea is to divide the memory into two zones of the same size: When a zone is full, garbage collection will be performed on this zone. Execute the mark phase to find all active objects, and then copy all active objects to another memory zone. The current memory zone can be recycled. This method solves the memory fragmentation problem, but introduces a new problem, with only half of the memory utilization.
Generational algorithm, generationalNo specific inventor was found. The memory is divided into different areas, each of which stores objects with different characteristics, such as divided by the length of the object's lifecycle. Different methods can be used for recovery in each zone. In the current JVM implementation, the garbage collection algorithm is used.
Generation in Oracle JVM: http://www.oracle.com/technetwork/java/javase/gc-tuning-6-140523.html
The above three algorithms constitute the core of garbage collection. All the current algorithms are generated based on the above three methods. These three methods are not very complex, but the first person to think of these methods is still very great.
Derivative algorithmsMark-sweep-compact (also called Mark-compact) is a combination of mark-sweep and copy algorithms. Execute the tag phase to find all active objects, and then move all active objects to the memory zone's start point (rellocate). All objects are continuously prevented. All the memory space behind the last active object can be released at one time. This method solves the memory fragmentation problem and the memory utilization problem. Currently, JVM is widely used.
GC in Oracle JVMIt mainly uses three algorithms: generational division, copy, and tag sorting. The copy algorithm is used for the collection of young (Youth. Note that the memory is not divided into two zones of the same size. Instead, the memory is not divided into two zones Eden and memory vor. The specific proportion can be set through the JVM parameter. The basis of this design is that the vast majority of objects will be recycled after GC, and only a small number of objects will survive. All the VOR objects do not have to be as large as Eden. The collection of Young is called minor GC (or young GC) in JVM, because the space is small, the speed will be faster and more frequent.
The mark-sweep-compact algorithm is used to collect tenured in the old age. The object moving process is required during the recycling process, and the tenured space will be relatively large and the recycling time will be relatively long. The collection of the entire heap (including young, tenured, Perm permanent zone and not heap) in JVM is called full GC. Minor GC may trigger full GC.
Based on the concurrency of the GC algorithm:
Serial collection serial collectorSingle-threaded execution, high efficiency, but not multi-core.
Parallel collection of parallel collectorMulti-threaded execution, supporting multiple cores, high performance on multi-core processors.
Follow the Running Relationship with the program code:
Stop the worldWhen GC is executed, the program code cannot be executed.
ConcurrentGC can be executed simultaneously with the program code, but it is not completely parallel. You also need to stop the program code, but the stop time is relatively short. Currently, only the concurrent mark-sweep (CMS) collector can support concurrency.
GC in JVM can be classified by the above method, and each GC can be divided from the preceding dimensions. Generation algorithms, parallel execution, and concurrent execution.
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