first, the basic recovery algorithm
1. Reference count (Reference counting)
The older collection algorithm. The principle is that this object has a reference, that is, adding a count, and deleting a reference reduces a count. When garbage collection is collected, only objects with a collection count of 0 are used. The most lethal of this algorithm is the inability to handle circular references.
2. Mark-Clear (Mark-sweep)
This algorithm performs in two phases. The first stage marks all referenced objects starting from the reference root node, and the second phase traverses the entire heap, clearing the unmarked objects. This algorithm needs to suspend the entire application, while generating memory fragmentation.
3. Replication (copying)
This algorithm delimits the memory space to two equal regions, using only one of the regions at a time. When garbage collection, traverse the current area of use and copy the objects in use to another area. The secondary algorithm only processes the objects in use each time, so the replication cost is small, while the replication past can also be the corresponding memory collation, but there is a "fragmentation" problem. Of course, the disadvantage of this algorithm is also very obvious, that is, twice times the memory space required.
4. Mark-Finishing (mark-compact)
This algorithm combines the advantages of "tag-clear" and "replicate" two algorithms. It is also divided into two phases, the first phase marks all referenced objects from the root node, the second phase traverses the entire heap, clears the unmarked objects and "compresses" the surviving objects into one of the heaps, discharging them sequentially. This algorithm avoids the fragmentation problem of "tag-purge" and avoids the space problem of the "copy" algorithm.
5. Incremental collection (incremental collecting)
Implement the garbage collection algorithm, which is: garbage collection while the application is in progress. Don't know why the collector in JDK5.0 does not use this algorithm.
6. Generational (generational collecting)
Based on the analysis of the object life cycle, the garbage collection algorithm is derived. The object is divided into the young generation, the old generation, the permanent generation, the different life cycle objects using different algorithms (one of the above methods) for recycling. Now the garbage collector (starting from j2se1.2) uses this algorithm.
Ii. Classification of heap memory in the JVM
1. Young (younger generation)
The young generation is divided into three districts. An Eden area, two survivor districts. Most objects are generated in the Eden area. When the Eden is full, the surviving objects will be copied to the Survivor area (one of two), and when the survivor is full, the surviving objects of the area will be copied to another survivor area, and when the survivor is full, Objects copied from the first survivor area and still alive at this time will be replicated in the old age area (tenured). Note that the two areas of survivor are symmetrical and have no precedence, so there may be simultaneous objects in the same zone that are replicated from Eden, and those that were copied from the previous survivor, and those that were copied to the old age only came from the first survivor. Moreover, one of the survivor areas is always empty.
2. Tenured (older generation)
Older generations store objects that live from young generations. In general, older generations are storing objects that are longer in their lifetimes.
3. Perm (lasting generation)
Used to store static files, Java classes, methods, etc. today. Persistent generations have no significant impact on garbage collection, but some applications may dynamically generate or invoke some class, such as Hibernate, where a larger, persistent generation space needs to be set up to store the new classes in these runs. The persistent generation size is set by-xx:maxpermsize=<n>.
three, GC type
There are two types of GC: Scavenge GC and full GC.
1. Scavenge GC
In general, when a new object is generated, and the Eden application space fails, it is good to trigger the scavenge GC, heap the Eden area for GC, purge the inactive objects, and move the surviving objects to the survivor area. Then organize the two districts of survivor.
2. Full GC
Organize the whole heap, including young, tenured and perm. The full GC is slower than the scavenge GC, so the full GC should be reduced as much as possible. A full GC can occur for the following reasons:
* tenured is written full
* Perm field is full
* System.GC () is displayed to call
* Dynamic change of domain allocation policy after last GC heap
Four, Garbage collector
There are three main types of collectors: serial collectors, parallel collectors, and concurrent collectors.
1. Serial collector
Use a single-threaded process for all garbage collection, because there is no need for multithreaded interaction, so it is more efficient. However, the advantages of multiprocessor are not available, so this collector is suitable for single processor machines. Of course, this collector can also be used on multiprocessor machines with small amounts of data (around 100M). You can use-XX:+USESERIALGC to open it.
2. Parallel collector
1. Concurrent garbage collection for young generations can reduce garbage collection time. Typically used on multithreaded multiprocessor machines. Use-XX:+USEPARALLELGC. Open. The parallel collector, introduced in the j2se5.0 66th update, has been enhanced in the Java SE6.0--which can be collected in parallel in the older generation. If the older generation does not use concurrent collection, it is garbage collection using a single thread, thus restricting scalability. Open using-XX:+USEPARALLELOLDGC.
2. Use-xx:parallelgcthreads=<n> to set the number of threads for concurrent garbage collection. This value can be set equal to the number of machine processors.
3). This collector can be configured as follows:
* Max garbage Collection pause: Specifies the maximum pause time for garbage collection, specified by-xx:maxgcpausemillis=<n>. <N> milliseconds. If this value is specified, the heap size and garbage collection related parameters are adjusted to reach the specified value. Setting this value may reduce the throughput of the application.
* Throughput: Throughput is the ratio of garbage collection time to non garbage collection time, set by-xx:gctimeratio=<n>, Formula 1/(1+N). For example, when-xx:gctimeratio=19, it means that 5% of the time is used for garbage collection. The default is 99, or 1% of the time is used for garbage collection.
3. Concurrent Collectors
It is possible to ensure that most of the work is concurrent (application does not stop), garbage collection is only paused for a very small amount of time, this collector is suitable for response time requirements of a high scale application. Open using-XX:+USECONCMARKSWEEPGC.
1). The concurrent collector mainly reduces the pause time of the older generation, and he uses a separate garbage collection thread to track the accessible objects without stopping the application. In each old generation garbage collection cycle, the concurrent collector at the beginning of the collection will briefly pause the entire application, pausing again in the collection. The second pause is slightly longer than the first, and multiple threads are garbage collected at the same time during this process.
2). The concurrent collector uses the processor for a short pause time. On an N-processor system, the Concurrent Collection section uses k/n available processors for recycling, typically 1<=K<=N/4.
3. The concurrent collector is used on a host that has only one processor, and a shorter pause time is set to incremental mode mode.
4. Floating garbage: Because garbage collection occurs while the application is running, some of the garbage may be generated when the garbage collection is complete, resulting in a "floating garbage" that needs to be recycled at the next garbage collection cycle. Therefore, the concurrent collector generally requires 20% of the reserved space for these floating garbage.
5. Concurrent Mode Failure: The concurrent collector collects when the application runs, so it is necessary to ensure that there is enough space in the garbage collection time for the program to use, otherwise, the garbage collection is not complete, the heap space is full first. In this case, "concurrency mode Failure" will occur, at which time the entire application will be paused for garbage collection.
6. Start concurrent Collector: Because concurrent collections are collected at application run time, you must ensure that there is sufficient memory space before the collection is complete for the program to use, otherwise "Concurrent Mode failure" appears. Start concurrent collection by setting-xx:cmsinitiatingoccupancyfraction=<n> to specify how many remaining heaps to run
4. Summary
* Serial Processor: &NBSP
--Application: Small amount of data (around 100M) ; A single processor and an application that has no requirement for response time. &NBSP
-Disadvantage: Only for small application
* Parallel Processor:
-Application: "High throughput Requirements", multiple CPUs, application response time is not required for large and medium applications. Examples: Background processing, scientific calculation. &NBSP
-Disadvantage: Application response time may be longer
* Concurrent processors:
-Application: "High response time requirements," multiple CPUs, the application response time has a higher demand for large and medium applications. Examples: Web server/Application server, telecommunications Exchange, integrated development environment.