Mainly according to the following 3 blog to do the finishing http://blog.csdn.net/zsuguangh/article/details/6429592http://www.cnblogs.com/ywl925/p/3925637. htmlhttp://blog.csdn.net/zouxinfox/article/details/1594216 generally fall into two broad categories,Reference Counting MethodAndTracking Method1.Reference Counting MethodUse reference counters to differentiate between live objects and objects that are no longer in use. Each object in the heap corresponds to a reference counter. Whenever an object is created and assigned to a variable, the reference counter is set to 1. When an object is assigned to any variable, the reference counter is incremented by 1, the reference counter is reduced by 1 when the object is out of scope (the object is discarded), and once the reference counter is 0, the object satisfies the garbage collection condition. Advantage: Fast garbage collection, try for real-time environment. Disadvantage: cannot detect a circular reference. 2.Tracking MethodThe tracing algorithm is proposed to solve the problem that the reference counting method cannot detect the circular reference, and it uses the concept of the root set. This method considers each object as a node in the graph, and the reference relationship between the objects is the adjacency relationship of each node in the graph. Start scanning from the root set to identify which objects are up to, which objects are unreachable, and to tag the objects in some way. Advantages: can detect the circular reference, more commonly used.here are some common garbage collection algorithms1.mark-Clear collectorThe collector first loops through the object graph and marks the reachable objects, then scans the stack for unmarked objects and frees up their memory. This collector typically uses a single thread to work and stops other operations. Also, because it clears only those unmarked objects and does not compress the tagged objects, it can result in a lot of memory fragmentation, which wastes memory. 2.Tag-Compression collectorSometimes also called Mark-purge-compress collector, with the mark-purge collector has the same marking stage. In the second stage, the tag object is copied to the new domain of the stack to compress the stack. This collector also stops other operations. 3.Copy CollectorThe algorithm is proposed to overcome the overhead of the handle and to solve the garbage collection of heap fragments. It begins by dividing the heap into an object area and multiple idle areas, where the program allocates space for objects from the object area, and when the object is full, garbage collection based on the coping algorithm scans the active object from the root set and copies each active object to the idle area (so that there is no idle interval between the memory occupied by the active object). The idle area becomes the object area, the original object area becomes the idle area, and the program allocates memory in the new object area. A typical garbage collection based on coping algorithm is the stop-and-copy algorithm, which divides the heap into object and idle regions, and the program suspends execution during the switching between the object area and the idle area. 4.Incremental CollectorThe incremental collector divides the stack into multiple domains, collecting garbage from only one domain at a time. This can result in a smaller application outage. 5.Generational CollectorsOne drawback of the stop-and-copy garbage collector is that the collector must replicate all active objects, which increases the program wait time, which is why the coping algorithm is inefficient. In the program design there is such a law: most objects exist for a short time, a few of the existence of a long time. Therefore, the generation algorithm divides the heap into two or more, each sub-heap as the object's generation (generation). Because most objects exist for a shorter time, the garbage collector collects these objects from the youngest child heap as the program discards objects that are not used. After the generational garbage collector runs, the last surviving object is moved to the next highest generation sub-heap, saving time because the old generation of sub-heaps is not often recycled. 6.Adaptive CollectorsIn certain cases, some garbage collection algorithms are better than other algorithms. The garbage collector based on the adaptive algorithm monitors the usage of the current heap and will select the appropriate algorithm for the garbage collector. 7.Concurrent CollectorsThe concurrent collector runs concurrently with the application. These collectors generally have to stop other operations to complete a particular task at some point (such as compression), but because other applications can perform other background operations, the actual time to interrupt other processing is greatly reduced. 8.Parallel CollectorThe parallel collector uses some traditional algorithms and uses multithreading to perform their work in parallel. The use of multithreading on multi-CPU machines can significantly improve the scalability of Java applications. Example:Train AlgorithmA big drawback of garbage collection algorithms is the difficulty of controlling the amount of time that garbage collection takes, and when it is necessary to do a garbage collection. Train algorithms are algorithms used by generational collectors to provide progressive collection of time limits in mature object space. Currently applied to the hotspot virtual machine.
In the train algorithm, the memory is divided into blocks, and multiple blocks form a collection. In order to visualize, a compartment represents a block, a train represents a collection, see Figure a
Note that each compartment is of equal size, but the number of carriages contained in each train is not necessarily equal. The garbage collection takes the compartment as the unit, and the collection order is followed. This order is also the order in which the blocks are created.
The garbage collector starts scanning from the block until, if all the objects in the train's four blocks are not referenced by the train's objects, and only the objects inside the train refer to each other, the whole train is garbage and can be recycled.
As shown in Figure II , the carriages are visible, and the train does not refer to the object of the train, then the whole train is rubbish.
There are objects referenced by other trains, see figure III , when the car is scanned to find the object reference, the object is transferred to the compartment, and then the scanned object is also transferred to the car, and then scanned whether to refer to other objects, if the reference to other objects will also be transferred, and so on. After scanning all the objects of the train, the rest of the objects that are not transferred are rubbish, and the whole train can be recycled as garbage. Note that if the compartment is full at the time of the transfer, a new compartment will be opened at the end of the train and the newly transferred object should be placed on the rear of the train.
Supplementary note: The garbage collector scans only one compartment at a time. The objects B and C in figure III are not immediately recycled, but are first transferred to the rear compartment of train 1. That is, after scanning 1.1, B was transferred to the train 1 tail, after scanning 1.3, C was transferred to the rear. When the garbage collector scans the tail of the train 1, B and C are collected if there are still no external objects referencing them.
The great advantage of the train algorithm is that it ensures that the large loop structure can be fully collected, as objects in the cyclic structure of the garbage, no matter how large, are moved into the same train and eventually collected together. Another benefit is that this algorithm can in most cases ensure that garbage collection takes a limited amount of time, because a garbage collection collects only one compartment, and the size of the compartment is limited. measures to reduce GC overhead in programs According to the above GC mechanism, the operation of the program will directly affect the system environment changes, thus affecting the GC trigger. Without design and coding for GC features, there is a series of negative effects such as memory presence. To avoid these effects, the basic principle is to minimize the garbage and reduce the cost of the GC process as much as possible. Specific measures include the following aspects: (1) do not explicitly call System.GC () This function recommends that the JVM carry out the main GC, although it is only recommended but not necessarily, but in many cases it triggers the main GC, increasing the frequency of the main GC, which increases the number of intermittent pauses. (2) minimizing the use of temporary objects temporary objects will become garbage after jumping out of function calls, and less temporary variables will be equivalent to reducing the generation of garbage, thus prolonging the occurrence of the second triggering condition mentioned above and reducing the chance of the main GC. (3) It is best to explicitly place the object when not in use null generally, null objects are treated as garbage, so the unused objects are explicitly set to NULL, enabling GC collectors to determine garbage, which increases the efficiency of the GC. (4) try to use StringBuffer instead of string to accumulate the string because string is a fixed-length string object, when you accumulate the string object, it is not amplified in a string object. Instead, recreate the new string object, such as STR5=STR1+STR2+STR3+STR4, which produces multiple garbage objects during execution, because a new string object must be created for the "+" operation, but these transition objects are meaningless to the system. Will only add more rubbish. Avoid this situation can use StringBuffer to accumulate the string, because the StringBuffer is variable long, it expands on the original basis, does not produce intermediate objects. (5) with basic types such as Int,long, it is not necessary to use the Integer,long object Basic type variables to occupy much less memory resources than the corresponding objects, if not, it is best to work with basic variables. (6) Minimize the use of static object variables Static variables are global variables, are not recycled by GC, and they always consume memory. (7) time to create or delete a Scatter object set up a large number of new objects in a short period of time, especially large objects, can result in a sudden need for large amounts of memory, and in this case the JVM can only perform a primary GC to reclaim memory or consolidate memory fragments, thereby increasing the frequency of the main GC. The same is true for deleting objects centrally. It makes the sudden emergence of a large number of garbage objects, free space must be reduced, which greatly increases the next time to create a new pairThe opportunity to force the main GC when the image is
Java garbage collection algorithm