How does a virtual machine determine that an object is dead? Most people answer the reference count.Algorithm.
1. Reference Counting Algorithm
Add a reference counter to the object. When a reference counter is referenced in one place, the counter is added with 1. When the reference fails, the counter value is reduced by 1, objects whose counter value is 0 at any time cannot be used again. The implementation of the reference counting method is simple and the determination efficiency is relatively high. However, the reference counting method is not used in Java to manage the memory. The main reason is that it is difficult to solve the issue of cross-cycle reference between objects. Obja. instance = objb; objb. instance = obja; in addition, the two objects have no reference. In fact, these two objects are no longer accessible, but they reference each other. The reference counter cannot notify the GC collector to recycle them.
2. Root Search Algorithm
Java C # And lisp both use the root search algorithm to determine whether the object is alive. The basic idea of this algorithm is to use a series of objects named GC roots as the starting point to start searching down from these nodes. The paths searched through are referred to as reference chains. When an object is connected to a reference chain without an object, it is proved that this object is unavailable.
Death history of Objects
When an object cannot be retrieved from GC roots, it can be recycled. If the class overwrites the Finalize method, finalize may be executed. Normally, after the execution is completed, real garbage collection is facing. If you assign the object itself to another variable in the Finalize method, you can escape the fate of garbage collection, the Finalize method can only be called once. The Finalize method will not be called when the object faces garbage collection next time.
However, the Finalize method is too uncertain and costly to use.
Garbage collection Algorithm
1. Mark-clear Algorithm
The algorithm is divided into two phases: marking and clearing all objects to be recycled. After marking is complete, all marked objects are recycled in a unified manner, which is the most basic collection algorithm.
2. Copy Algorithms
It divides the memory into two equal parts by capacity. Each time it uses only one of them, when the memory is used up, it copies the still living objects to the other, then, the used memory space is cleared once. In this way, the memory is recycled every time, and the memory fragments and other complex situations do not need to be considered during memory allocation. As long as you move the heap top pointer and allocate the memory in sequence, the implementation is simple, efficient. The cost of this algorithm is to reduce the memory to half of the original size.
3. Tag-Sorting Algorithm
The marking process is still the same as the "marking-sorting algorithm", but the subsequent steps do not directly clean the recyclable objects, but move all the surviving objects to one end, then, the memory outside the end boundary is cleared.
4. Generational collection Algorithm
Currently, commercial virtual machines use this algorithm. Memory is divided into several parts based on the Survival cycle of objects, and Java stacks are divided into the new generation and old generation, in this way, you can use the most appropriate collection algorithm based on the characteristics of each age. In the new generation, a large number of objects die in each garbage collection. You can use the replication algorithm to complete the collection by copying a small number of surviving objects. In the old age, objects have a high survival rate and no extra space. You must use the tag-clean or tag-Sort Algorithm to recycle objects.
Garbage Collector
In the context of garbage collection, it is necessary to explain two terms: concurrency and parallelism.
Parallel: Multiple garbage collection threads work in parallel, but the user thread is still waiting.
Concurrency: the user thread and the garbage collection thread work simultaneously.ProgramContinue running, while the garbage collection program runs on another CPU
1. Use the serial collectorCopy Algorithm
The serial collector is the most basic and historical collector. It is a single-threaded collector and must suspend all other working threads during garbage collection until the collection is complete. Garbage collection is automatically initiated and completed by virtual machines in the background. When invisible to users, all normal working threads of users are stopped, this is unacceptable for many applications.
But it also has its own advantages: simple and efficient for a single CPU environment, the collector can achieve the highest single-thread collection efficiency because there is no thread interaction overhead.
2. The parnew collector usesCopy Algorithm
The parnew collector is actually a multi-threaded version of the serial collector, including the available control parameters, collection algorithms, suspension of all user threads, object allocation policies, and collection policies of the collector, which are exactly the same as those of the serial collector, it is the preferred new generation collector for virtual machines running in server mode. One of the reasons is that, apart from the serial collector, it can only be used with the CMS collector. The CMS collector is the first real concurrency collector on the hotspot virtual machine.
The parnew collector does not have better performance than the serial collector in a single CPU environment.
3. Use the parallel scavenge collectorCopy Algorithm
The paraller scavenge collector is also a new generation collector, which uses the replication algorithm and is a parallel multi-thread collector.
The characteristics of the parellel scavenge collector are that it is different from other collectors. collectors such as CMS try to shorten the pause time of user threads during garbage collection, the goal of the parallel scavenge collector is to achieve a controllable throughput, which is used by the CPU to run users.CodeThe ratio of the time to the total CPU consumption time. Throughput = run user code time/(run user code time + garbage collection time), the virtual machine runs for 100 minutes, the garbage collection takes 1 minute, And the throughput is 99%
The shorter the pause time, the more suitable the program to interact with the user, and the better response speed can improve the user experience; the higher throughput, the more efficient use of CPU time
The parallel scavenge collector has a GC adaptive call policy. The virtual machine collects performance monitoring information based on the current system running conditions and dynamically adjusts parameters to provide the most appropriate pause time or maximum throughput.
The Adaptive Policy is also an important difference between the parallel scavenge collector and the parnew collector.
4. The serial old collector usesMark-organizeAlgorithm
It is an older version of the serial collector. It is a single-thread collector and uses the tag-sorting algorithm.
5. The parallel old collector adopts the tag-sorting algorithm.
Parallel old is an older version of the parallel scavenge collector. It uses multithreading and markup-sorting algorithms.
6. CMS collectorMark-clearAlgorithm
The CMS collector is a collector designed to obtain the minimum recovery pause time. It consists of four steps: initial marking, concurrent marking, re-marking, and concurrent clearing. The initial marking and re-marking steps either need to pause other threads.
The initial tag only indicates the objects that can be directly associated with GC roots. The speed is very fast. The concurrent tag stage is the process of GC roots tracing, and the re-mark stage is to modify the concurrent tag period, the tag record of the part of objects whose tags are changed due to the continued operation of the user program. This phase is generally a little longer than the initial tag phase, but far shorter than the concurrency tag.
Because the collector can work with the user thread in the Process of the longest time-consuming concurrent mark and concurrent cleaning, in general, the memory reclaim process of the CMS collector is executed concurrently with the thread.
Disadvantages of CMS collectors:
The CMS collector is very sensitive to CPU resources. programs designed for concurrency are sensitive to CPU resources. Although the concurrent phase does not cause application threads to pause, however, the application code may be generated because some threads are used, and the total throughput will decrease.
CMS is based on the tag-cleaning algorithm. A large amount of space fragments are generated when the collection ends. If there are too many space fragments, it will cause a great deal of trouble to allocate them, there will be a lot of space available in the old age, but you cannot find enough continuous space to allocate the current object. You have to trigger full GC in advance.
7. G1 collectorMark-organizeAlgorithm
The G1 collector is the product of further development of the collector theory. It has two important improvements compared with the cms collector: the G1 collector is based on the tag-sorting algorithm and does not produce space fragments, it is very important for long-running applications. Second, it can precisely control the pause, allowing users to specify a time segment with a length of M milliseconds, and the time consumed on the garbage collection should not exceed n milliseconds
The G1 collector can collect low-Pause memory without sacrificing throughput. It can avoid garbage collection across the entire region.
Memory Allocation and recovery policies
The automatic memory management advocated in the Java technical system can be attributed to two problems: allocating memory to objects and recycling the memory allocated to objects.
The memory allocation of objects is distributed on the heap. objects are mainly distributed in the Eden area of the new generation. In rare cases, objects may be directly allocated in the old generation.
1. objects are preferentially allocated in Eden.
Objects are allocated in the new generation Eden area. When the Eden area does not have enough space for allocation, the virtual machine will initiate a minor GC
2. large objects directly enter the Old Age
A large object is a Java object that requires a large amount of continuous memory space. The most typical is a long string and array.
3. Long-lived objects will enter the Old Age
The Virtual Machine defines an object age counter for each object. When the age increases to a certain program, it will be promoted to the old age.
New generation uses the replication collection Algorithm
Minor GC: The garbage collection action occurs in the new generation. Most Java objects have the feature of extinction, so minor GC is very frequent, and the collection speed is also relatively fast.
Major GC/full GC: GC occurs in the old age. The speed of major GC is generally 10 times slower than that of minor GC.