Java garbage Collection algorithm

Source: Internet
Author: User

Java garbage Collection algorithm
Introduction
The Java heap is a run-time data area in which instances (objects) of the class allocate space. The Java Virtual machine (JVM) heap stores all the objects that are created by the running application.
These objects are established through directives such as new, NewArray, Anewarray, and Multianewarray, but they do not require program code to be explicitly released. In general, the heap is made up of
Garbage collection is responsible, although the JVM specification does not require special garbage collection techniques and even does not require garbage collection at all, but due to the limited memory, the JVM is implemented
There is a heap managed by garbage collection. Garbage collection is a dynamic storage management technology that automatically frees objects that are no longer referenced by the program and is calculated according to the specific garbage collection
To realize the function of automatic resource recovery.

The meaning of garbage collection
In C + +, the memory occupied by an object is occupied until the end of the program, and cannot be assigned to other objects until explicitly released, whereas in Java, when there is no object reference pointing to the
When the memory is originally allocated to an object, the memory becomes garbage. A system-level thread of the JVM automatically frees the block of memory. Garbage collection means objects that the program no longer needs
Is "useless information" and this information will be discarded. When an object is no longer referenced, the memory reclaims the space it occupies so that the space is later used by the new object. In fact
In addition to releasing useless objects, garbage collection can also erase memory-logged fragments. The memory is fragmented because the object being created and the garbage collector frees up the memory space that the discarded objects occupy.
Fragmentation is a free memory hole between the blocks of memory allocated to an object. Defragmentation moves the occupied heap memory to one end of the heap, and the JVM allocates the compiled memory to the new object.

Garbage collection can automatically free up memory space and reduce the burden of programming. This gives Java virtual machines some advantages. First, it can make programming more efficient. In the absence of a garbage collection mechanism
, it may take a lot of time to solve a difficult memory problem. When programming in the Java language, the garbage collection mechanism can greatly shorten the time. The second is that it protects
program integrity, garbage collection is an important part of the Java language Security strategy.

One potential drawback of garbage collection is that its overhead affects program performance. The Java Virtual machine must trace the objects that are useful in the running program and eventually release the useless objects. This one
The process takes the processor time. Secondly, the incompleteness of garbage collection algorithm, some garbage collection algorithms used earlier can not guarantee that 100% collected all the discarded memory. When
However, with the continuous improvement of garbage collection algorithm and the increasing efficiency of hardware and software, these problems can be solved.

Algorithm analysis of garbage collection
The Java language Specification does not explicitly describe which garbage collection algorithm the JVM uses, but there are generally 2 basic things to do in any one of the garbage collecting algorithms:
(1) Found useless information object.
(2) Reclaim the memory space occupied by the useless object, so that the space can be reused by the program.

Most garbage collection algorithms use the concept of root set (root set), which is the collection of reference variables that the root set is able to access in the execution of a Java program
(including local variables, parameters, class variables), the program can use reference variables to access the properties of the object and methods of invoking the object. Garbage collection preferences need to be determined
From the root which is accessible and which is unreachable, objects from the root set are active objects and they cannot be recycled as garbage, which also includes the
An object that the root set can be indirectly reached. The root set, which is unreachable by any path, is eligible for garbage collection and should be recycled. Here are a few common algorithms.


1. Reference counting method (Reference counting Collector)
Reference counting is the only algorithm that does not use the root set for garbage collection, which uses reference counters to differentiate between surviving objects and objects that are no longer in use. In general, each of the heap
Object corresponds to a reference counter. When an object is created and assigned to a variable each time, the reference counter is set to 1. When an object is assigned to any variable, the reference counter is used for each
Add 1 when the object is out of scope (the object is discarded), the reference counter is reduced by 1, and once the reference counter is 0, the object satisfies the garbage collection condition.
A garbage collector based on a reference counter runs faster and does not interrupt program execution for long periods of time, and it is appropriate to run programs in real time. But the reference counter increases the cost of program execution,
Because each time the object is assigned to a new variable, the counter adds 1, and each time the existing object is scoped, the counter is reduced by 1.
2. Tracing algorithm (tracing Collector)
The tracing algorithm is proposed to solve the problem of the reference counting method, which uses the concept of the root set. The garbage collector based on the tracing algorithm starts scanning from the root set to identify which
objects can be reached, which objects are unreachable, and tagged in some way to objects, such as one or more bits for each of the objects that can be reached. In the process of scanning recognition, based on tracing calculation
The garbage collection of the method is also known as the Mark and Purge (mark-and-sweep) garbage collector.
3. Compacting algorithm (compacting Collector)
In order to solve the problem of heap fragmentation, garbage collection based on tracing absorbs the idea of the compacting algorithm, in the process of purging, the algorithm moves all objects to one end of the heap,
The other end becomes an adjacent free memory area, and the collector updates all references to all the objects it moves so that the references in the new location will recognize the original
Like. In the implementation of the collector based on the compacting algorithm, the handle and the handle table are generally added.
4. Copying algorithm (coping Collector)
The algorithm is proposed to overcome the overhead of the handle and to solve the garbage collection of heap fragments. It starts by dividing the heap into an object face and multiple free polygons, which the program assigns to objects from the object face.
Space, 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 free surface (making the memory of the active object
No free holes), so that the free surface becomes the object surface, the original object face becomes the idle surface, and the program allocates memory in the new object face.
A typical garbage collection based on coping algorithm is the stop-and-copy algorithm, which divides the heap into object and idle area polygons, and in the process of switching between the object surface and the idle area,
The program pauses execution.
5. Generation algorithm (generational Collector)
One 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
The design has 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, and each sub-heap acts as a
The generation of objects (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 algorithm (Adaptive Collector)
In 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 selects the appropriate algorithm
Garbage collector.
Perspective Java Garbage Collection
1. Command line parameter perspective garbage collector run
2. Use System.GC () to request Java garbage collection regardless of which garbage collection algorithm the JVM is using. There is a parameter in the command line-VERBOSEGC can check
Look at the heap memory used by Java, which is in the following format: JAVA-VERBOSEGC classfile


You can look at an example:
Class TESTGC
{
public static void Main (string[] args)
{
New TESTGC ();
System.GC ();
System.runfinalization ();
}
}
In this example, a new object is created, because it is not used, so the object quickly becomes accessible, after the program is compiled,
Execute command: JAVA-VERBOSEGC TESTGC After the result is:
[Full GC 168k->97k (1984K), 0.0253873 secs]
The environment for the machine is, Windows + JDK1.3.1, data 168K and 97K before and after the arrows represent the amount of memory used by all surviving objects before and after the garbage collection GC, indicating
The 168k-97k=71k object capacity is reclaimed, the data in parentheses is 1984K the total capacity of the heap memory, and the time required to collect it is 0.0253873 seconds (this time at each execution
Will vary).


2. Finalize method to view the operation of garbage collector
Before the JVM garbage collector collects an object, it is generally required that the program call the appropriate method to free the resource, but without explicitly releasing the resource, Java provides the default machine
System to terminate the object's heart release resources, this method is Finalize (). Its prototype is:
protected void Finalize () throws Throwable
After the Finalize () method returns, the object disappears and garbage collection begins execution. The throws Throwable in the prototype indicates that it can throw any type of exception.
The reason to use Finalize () is that there are times when you need to take a different approach to Java's common approach, by allocating memory to do something with C-style. This mainly
Can be done by means of an "intrinsic method", which is a way to invoke a non-Java method from Java. C and C + + are the only languages currently supported by native methods. But since they can
Calling subroutines written in other languages makes it possible to invoke anything effectively. Within non-Java code, you might be able to call the malloc () series function of C and use it to allocate memory
Storage space. and unless free () is called, storage space is not freed, causing a memory "vulnerability" to occur. Of course, free () is a C and C + + function, so
We need to call it in an intrinsic method inside the Finalize (). This means that we cannot use Finalize () too much, and it is not an ideal place to do normal cleanup work.

In normal cleanup, to clear an object, the user of that object must invoke a purge method at the point where it is expected to be cleared. This is somewhat similar to the concept of C + + "sabotage"
Conflict. In C + +, all objects are destroyed (cleared). Or, in other words, all objects "should" be destroyed. If you create a C + + object as a local object, such as on a stack
(is not possible in Java), the cleanup or destruction will be done at the end of the scope that the "closing curly brace" represents, which creates the object. If the object is in
New (similar to Java), then when the programmer calls the C + + DELETE command (Java does not have this command), the corresponding destroy is invoked. If the programmer forgets,
Then never call the wreck, and what we end up with is a memory "vulnerability" that also includes other parts of the object that never get erased.

Instead, Java does not allow us to create local (local) objects-in any case, using new. In Java, however, there is no "delete" command to free the object because the garbage collector
will help us to automatically free up storage space. So if we stand in a relatively simplified position, we can say that there is a garbage collection mechanism, so Java does not have a destruction device. However
As you learn later, you will know that the existence of a garbage collector does not completely eliminate the need for a sabotage, or that it does not eliminate the need for a mechanism that the device represents.
(and definitely not directly call Finalize (), so try to avoid using it). If you want to perform some sort of cleanup work other than freeing the storage space, you must still
Call one of the methods in Java. It is equivalent to C + + 's destruction, but not the latter convenience.


The following example shows you the process of garbage collection and summarizes the previous statements.
Class Chair {
static Boolean gcrun = false;
static Boolean f = false;
static int created = 0;
static int finalized = 0;
int i;
Chair () {
i = ++created;
if (created = = 47)
System.out.println ("Created 47");
}
protected void Finalize () {
if (!gcrun) {
Gcrun = true;
System.out.println ("Beginning to finalize after" + created + "chairs has been created");
}
if (i = = 47) {
System.out.println ("Finalizing Chair #47," + "Setting flag to stop Chair creation");
F = true;
}
finalized++;
If (finalized >= created)
System.out.println ("All" + finalized + "finalized");
}
}
public class Garbage {
public static void Main (string[] args) {
if (Args.length = = 0) {
System.err.println ("Usage: \ n" + "java garbage before\n or:\n" + "java Garbage after");
Return
}
while (! CHAIR.F) {
New Chair ();
New String ("to take up space");
}
System.out.println ("After all chairs has been created:\n" + "total created =" + chair.created + ", total finalized =" + chair.finalized);
if (Args[0].equals ("before")) {
SYSTEM.OUT.PRINTLN ("GC ():");
System.GC ();
System.out.println ("Runfinalization ():");
System.runfinalization ();
}
System.out.println ("bye!");
if (Args[0].equals ("after"))
System.runfinalizersonexit (TRUE);
}
}
The above program creates many chair objects, and at some point after the garbage collector starts running, the program stops creating the chair. Because the garbage collector may be at any time
Run, so we don't know exactly when it starts. Therefore, the program uses a tag called Gcrun to indicate whether the garbage collector has started running. Using the second Mark F,
Chair can tell main () that it should stop generating the object. Both of these tokens are set internally in Finalize () and are tuned for garbage collection. Another two static variables--created
And finalized--are used to track the number of objects that have been created and the number of objects that the garbage collector has finished finishing. Finally, each chair has its own (non-static)
int I, so I can track how much it's specific number. Chair with number 47 completes the finishing touches, the tag is set to true to end the Chair object creation process.


A few additions to garbage collection
After the above description, you can find that garbage collection has the following characteristics:
(1) The unpredictable nature of garbage collection: Due to the implementation of different garbage collection algorithms and the use of different collection mechanisms, so it may be timed to occur, it may be when the system appears
CPU resources, it can also be the same as the original garbage collection, when memory consumption occurs at the limit, which is related to the choice of the garbage collector and the specific settings.
(2) The accuracy of garbage collection: mainly includes 2 aspects:
(a) The garbage collector is able to accurately mark live objects.
(b) The garbage collector is able to pinpoint the referential relationships between objects. The former is a precondition for completely reclaiming all discarded objects, or it may cause a memory leak. and the latter is
The necessary conditions for implementing algorithms such as merging and copying. All unreachable objects can be reliably reclaimed, all objects can be reassigned, objects are copied and object memory is shrunk,
This effectively prevents the fragmentation of memory.
(3) There are many different kinds of garbage collectors, each with its algorithm and its performance, both when garbage collection starts to stop the application running, and when garbage collection starts
It also allows the application's threads to run, as well as the garbage collection multithreading at the same time.
(4) The implementation of garbage collection is closely related to the specific JVM and the JVM's memory model. Different JVMs may use different garbage collection, and the JVM's memory model determines
What types of garbage collection can be used by the JVM. The memory systems in the hotspot family of JVMs are now designed with an advanced object-oriented framework, which allows the series of JVMs to adopt
State-of-the-art garbage collection.
(5) With the development of technology, modern garbage collection technology provides a number of optional garbage collectors, and in the configuration of each collector can also set different parameters, which makes according to
It is possible to obtain the best application performance for different applications.


In view of the above characteristics, we should pay attention to when using:
(1) Do not attempt to assume that garbage collection occurs at any time, all of which are unknown. For example, a temporary object in a method becomes a useless object after the method call is complete, which
Its memory can be released.
(2) Java provides a number of classes that deal with garbage collection, and provides a way to enforce garbage collection-calling System.GC (), but this is also an indeterminate approach.
Java does not guarantee that each call to this method will be able to start garbage collection, it will only issue to the JVM such a request, whether or not to actually perform garbage collection, everything is unknown.
(3) Pick a garbage collector that suits you. In general, if your system does not have special and demanding performance requirements, you can use the JVM's default options. Otherwise, you might consider using a targeted
Garbage collectors, such as incremental collectors, are more suitable for systems with higher real-time requirements. The system has a higher configuration, there are more idle resources, you can consider using
Parallel tag/purge collector.
(4) The key is also difficult to grasp the problem is the memory leak. Good programming habits and rigorous programming attitude is always the most important, do not let your own a small error caused a large memory leak.
(5) Release the reference of the useless object as soon as possible. Most programmers use temporary variables to automatically set the reference variable to null after exiting the active domain (scope), suggesting that the garbage
Collector to collect the object, you must also be aware that the referenced object is listening, and if so, remove the listener and then assign a null value.
Conclusion
In general, Java developers can not focus on the allocation of heap memory and garbage collection in the JVM, but fully understanding the Java feature allows us to use resources more efficiently.
Also note that the Finalize () method is the default mechanism for Java, and sometimes you can write your own Finalize method to ensure explicit release of object resources.

Java garbage Collection algorithm

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.