Java garbage Collection mechanism GC (detailed)

Source: Internet
Author: User
Tags compact garbage collection throwable stringbuffer
Common Definitions Java garbage Collection

garbage collection in idle time in an irregular manner, reclaiming the memory space occupied by objects without any references, rather than the object itself conditions that trigger the primary GC (garbage Collector)

(1) When the application is idle, the GC is invoked when no application thread is running. Because the GC is in the lowest-priority thread, the GC thread is not invoked when the application is busy, except for the following conditions.
(2) When the Java heap is low on memory, the GC is invoked. When the application thread is running and a new object is created during the run, if there is not enough memory space, the JVM is forced to call the GC thread to reclaim the memory for the new assignment. If the GC is not able to meet the requirements of memory allocation after the first time, the JVM will do two more GC for further attempt, if still unable to meet the requirements, the JVM will report "Out of Memory" error, the Java application will stop. Memory Leaks

The program dynamically allocates memory to some temporary objects, but the object is not reclaimed by GC, and it always occupies memory. That is, the assigned object can be reached but is useless. Memory Overflow

An error caused by the inability to request enough memory during the execution of a program. Why should there be a garbage collection mechanism

The Java language establishes a garbage collection mechanism for tracking objects that are in use and for discovering and reclaiming objects that are no longer in use (references). This mechanism can effectively guard against the two risks that may occur in dynamic memory allocation: the memory exhaustion caused by excessive memory garbage, and an illegal memory reference caused by improper memory release. Finally achieve the automatic release of memory space, reduce the burden of programming purposes. garbage Collection Algorithm reference count Method (Reference counting Collector)

Reference counting is an early strategy for garbage collection. In this approach, each object in the heap has a reference count. When an object is created, and a reference to the object is assigned to a variable, the reference count of the object is set to 1. For example, create a new object a a=new a (), and then A is assigned to another variable B, that is, b=a; then the reference count of object A is +1. When any other variable is assigned to a reference to this object, the count is plus 1. When an object's reference exceeds its lifetime or is set to a new value, the object's reference count is reduced by 1, such as B=c, then A's reference count-1. Any object with a reference count of 0 can be garbage collected. When an object is garbage collected, it references any object count minus 1. In this approach, an object that is garbage collected can cause garbage collection actions for subsequent objects. such as a a=new a (); b=a; when b is garbage collected, the reference count of a becomes 0, which causes a to be garbage collected.
Advantages
The reference count collector can be executed quickly and interleaved in the running of the program. This reminder is good for a real-time environment where programs cannot be interrupted for long periods of time.
Disadvantage
The reference count cannot detect loops (that is, two or more objects are referenced to each other). Example of a loop, where a parent object has a reference to a child object, which in turn refers to the parent object. It is impossible for an object user to count to 0, even if they cannot be touched by the root object of the executing program. Another disadvantage is that each reference count increases or decreases with additional overhead. mark-Purge Algorithm (mark-sweep)

This algorithm is to solve the problem that the citation counting method brings. The garbage collector begins scanning from the root set, identifying which objects are available, which objects are unreachable, and, in some way, marking the accessible objects, such as setting one or more bits for each accessible object. When the scan is finished, objects that are not marked are unreachable and can be collected. replication algorithm (copying)

This algorithm is proposed to overcome the handle overhead and solve heap fragmentation garbage collection. The 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 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. Mark-Finishing Algorithm (mark-compact)

This algorithm combines the advantages of "tag-clear" and "replicate" two algorithms. But in order to solve the defects of the assignment algorithm and make full use of the memory space, a "mark-finishing" algorithm is proposed. The algorithm marks the same phase as mark-clear, but after the tag is finished, it does not clean the recyclable object directly, but instead moves the surviving objects to one end, and then cleans out the memory outside the end of the boundary. Generational collection algorithm (generational Collection)

The Generational collection algorithm is an algorithm used by most of the JVM's garbage collectors at present. Its core idea is to divide memory into a number of different regions based on the life cycle of the object's survival. In general, the heap zoning is divided into the old age (tenured Generation) and Cenozoic (young Generation), the old age is characterized by a garbage collection only a small number of objects need to be recycled, and the new generation is characterized by each garbage collection has a large number of objects need to be recycled, Then the most suitable collection algorithm can be adopted according to the characteristics of different generations.
At present, most of the garbage collectors in the new generation are taking copying algorithm, because each garbage collection in the Cenozoic to reclaim most objects, that is, the number of operations to replicate less, but in practice is not in accordance with the proportion of 1:1 to divide the new generation of space, In general, the Cenozoic is divided into a larger Eden space and two small survivor space, each time using the Eden Space and one of the survivor space, when recycling, Copy the surviving objects in Eden and survivor to another survivor space, and then clear out Eden and the survivor space that you just used.
And because of the characteristics of the old age is a collection of only a small number of objects, the general use of the mark-compact algorithm.
Note that there is another generation outside the heap area that is the permanent generation (permanet Generation), which is used to store class classes, constants, method descriptions, and so on. Recycling for a permanent generation mainly reclaims two parts: discarded constants and unwanted classes.

Three. Typical garbage collector commonly used garbage collectors

There are three main types of collectors: serial collectors, parallel collectors, and concurrent collectors. A. 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. two. Parallel collectors can reduce garbage collection time by parallel garbage collection for young generations. Typically used on multithreaded multiprocessor machines. Use-XX:+USEPARALLELGC. Open. The parallel collector, introduced in the j2se5.0 66th update, was enhanced in the Java SE6.0 – can be gathered in parallel by 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. Use-xx:parallelgcthreads= to set the number of threads for concurrent garbage collection. This value can be set equal to the number of machine processors. This collector can be configured as follows:
Max garbage Collection Pause: Specifies the maximum pause time for garbage collection, specified by-xx:maxgcpausemillis=. is 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=, 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. three. 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: Concurrent collectors in the application runtime to collect, so you need to ensure that the heap in the garbage collection period of time there is enough space for the program to use, otherwise, garbage collection is not completed, heap space first full. 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 collection is 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" will appear. Start concurrent collection by setting-xx:cmsinitiatingoccupancyfraction= to specify how many remaining heaps to runFour. SummarySerial Processor:
– Application: A small amount of data (around 100M), a single processor, and no request for response time.
– Disadvantage: Only for small application parallel handlers:
– Application: "High throughput Requirements", multiple CPUs, medium and large applications with no requirements for application response time. Examples: Background processing, scientific calculation.
– 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.how to affect Java garbage collection

In general, we cannot control the garbage collection mechanism of the JVM in development, but it is possible to programmatically influence garbage collection in order to make the object conform to the garbage collection criteria.
1. Assign a worthless object to null
2. Re-assign a value to a reference variable

For example:

Person p = new person ("AAA");
 p = new Person ("BBB");
In this way, the object of the new person ("AAA") is garbage-eligible for garbage collection.
3. Make interconnected objects known as "Island" objects
person P1 = new Person ("AAA");
 person P2 = new Person ("BBB");
 Person P3 = new Person ("CCC");
 P1=P2; P2=P3; P3=P1;
 P1=null; P2=null; P3=null;
Before P1, p2, p3 are null, there is a love triangle between them. To null, a triangular relationship still exists, but three variables do not use them. The three person objects form an isolated island that eventually dies on the heap-and is recycled by garbage.
4. Compulsory garbage collection System.GC ()
System.GC ()
1 2

The above method is used to explicitly notify the JVM that a garbage collection can take place, but it is unpredictable at what point the real garbage collection mechanism starts to garbage collection; The only guarantee is that when there is very little in you, The garbage collector runs once before the program throws Outofmemaryexception. Finalize () method

Before the JVM garbage collector collects an object, it is generally required that the program call the appropriate method to free the resource, but in the absence of a clear release of resources, Java provides a default mechanism to terminate the object's heart-releasing resources, which is finalize (). Its prototype is:

protected void Finalize () throws Throwable
1

the understanding of the Finalize () method: The
1.finalize () method is the method in object. The
2.finalize () method is called once by the garbage collector before the object is garbage collected, which is a mechanism of the Java language. The
3.finalize () method is invoked at most one time by the garbage collector on any object.
     after the Finalize () method returns, the object disappears and the garbage collection starts executing. The throws Throwable in the prototype indicates that it can throw any type of exception. The reason for using Finalize () is that there is a special case that the garbage collector cannot handle. Assume that your object (not using the new method) obtains a "special" area of memory, because the garbage collector knows only the memory space that is allocated by the new one, so it doesn't know how to release the "Special" memory area. So this time Java allows the definition of a Finalize () method in a class. The
Finalize () method uses traps:
1. The garbage collector cannot guarantee that garbage objects can be recycled, so the Finalize () method is not guaranteed to run. It is recommended that you do not rewrite the Finalize () method, even if overridden, and do not write critical code in the Finalize () method. The
2.finalize () method can transfer itself to individual objects, so that it is not garbage and avoids being recycled. But the next time the object is garbage collected, the Finalize () method is not invoked for the second time, but is cleaned up directly. measures commonly used to reduce GC overhead in development (1) do not explicitly call System.GC ()

This function recommends that the JVM be the primary GC, although it is recommended rather than certain, but in many cases it triggers the primary GC, which increases the frequency of the primary GC, and increases the number of intermittent pauses. It is particularly important to note that the call System.GC () shown in the code does not necessarily make the GC, which we can verify through the Finalize () method, that is, to invoke System.GC () voluntarily, and not necessarily to call the Finalize () method each time. The Finalize () method is characterized by calling the Finalize () method first before the object is reclaimed. (2) minimizing the use of temporary objects

The temporary object becomes garbage after the function call is dropped, and the less temporary variable is the equivalent of reducing garbage generation, thus prolonging the occurrence of the second triggering condition mentioned above and reducing the chance of the primary GC. (3) It is best to explicitly null an object when it is not in use

In general, NULL objects are treated as garbage, so it is more efficient for GC collectors to determine garbage by explicitly setting unused objects to null. (4) try to use StringBuffer instead of string to accumulate strings

Because string is a fixed-length string object, accumulating a string object is not amplified in a string object, but rather creates a new string object, such as STR5=STR1+STR2+STR3+STR4, that produces multiple garbage objects during execution. Because a new string object must be created for the secondary "+" operation, these transition objects are meaningless to the system and will only add more garbage. To avoid this situation, you can use StringBuffer to accumulate strings, because the StringBuffer is variable length, it expands on the original basis, does not produce intermediate objects. (5) can use basic type such as Int,long, do not use Integer,long object

The base type variable consumes much less memory resources than the corresponding object, and if it is not necessary, it is best to use the base variable. Under what circumstances you need to use an integer. (6) Use of static object variables as little as possible

Static variables are global variables and are not reclaimed by GC, and they can occupy memory all the time. (7) The time concentrated on the creation or deletion of dispersed objects in a short time to create a large number of new objects, especially large objects, can cause a sudden need for a large amount of memory, the JVM in this case, only the primary GC to reclaim memory or consolidate memory fragmentation, thereby increasing the frequency of the primary GC The same is true for deleting objects centrally. It causes a sudden emergence of a large number of garbage objects, the inevitable reduction of free space, thereby greatly increasing the next time to create a new object to force the main GC opportunity.

Related Article

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.