Brief Introduction to Java garbage collection mechanismA Who is doing garbage Collection.
A popular saying: in C + +, the system is doing garbage collection, and in Java, Java itself is doing.
In C + +, freeing the memory is handled manually, using the delete operator to free the allocated memory. This is the popular saying. Specifically, when the application thinks that an entity is not needed, it needs a delete to tell the system that it can reclaim the space. This request, to the coder, is a very troublesome, difficult to do. Any BBS, there are always a lot of topics about memory leaks in C + +.
Java uses a different, very convenient method: garbage Collection. The garbage collection mechanism is placed in the JVM. The JVM is fully responsible for garbage collection, applying space only when needed, and not having to worry about space recycling when discarding objects.
Two When the object is discarded.
In C + +, when an object leaves its scope, the object is discarded.
Is that the lifetime of an object is no longer associated with its scope, but only with reference.
Java garbage collection mechanism generally contains nearly 10 kinds of algorithms. We don't need to be concerned about the majority of these algorithms. There is only one of the simplest: the reference counting method, which is related to coding.
An object that can have one or more reference variables pointing to it. When an object no longer has any reference variable pointing to it, the object is discarded by the application. Or, this object can be recycled by the garbage collection mechanism.
That is, when there is no reference to an object, it means that the application tells the JVM: I don't want this object, you can recycle it.
The JVM's garbage collection mechanism detects the heap space in real time. When an object is found to have a reference count of 0 o'clock, the object is included in the list to be reclaimed. However, it is not immediately destroyed.
Three Discard is recycled.
The object is deemed to be unnecessary, and the memory it occupies can be freed. The reclaimed memory can be used for subsequent redistribution.
However, it is not the object that is discarded and then recycled immediately. The JVM process does a lot of overhead with space recycling. If an application process discards one object at a time, it will reclaim its space immediately, making the system inefficient.
As mentioned earlier, there are multiple algorithms for the JVM's garbage collection mechanism. Other algorithms are used to determine when and how to recycle, except that the reference notation is used to determine whether the object has been discarded. The JVM's garbage collection mechanism has to be balanced between time and space.
Therefore, to improve system efficiency, the garbage collector usually runs only when two conditions are met: The object is recycled and the system needs to be recycled. Remember that garbage collection takes time, so the Java runtime system uses it only when it is needed. So you don't know exactly when the garbage collection happens.
Four Is it useful to have an object pointing to a reference variable?
Previously, the object that did not hang the reference variable was discarded, meaning that it was garbage in the heap space and could be reclaimed by the JVM at any time.
There is, however, an exception that is not an exception. For disposable objects (some books are called temporary objects), you can point to it without referencing a variable. One of the simplest and most common examples:
System.out.println ("I am java!");
is to create a string object and pass it directly to the println () method.
Five Can the application interfere with garbage collection?
Many people are not comfortable with Java garbage collection and want to control the garbage collection operation of the JVM in the application code. It's impossible. For the garbage collection mechanism, there are only two ways to send messages to the JVM. The first one already says that all the reference variables that point to an object are removed. This is equivalent to sending a message to the JVM: This object is not. The second is the Call library method System.GC (), which most books say calls it to make Java garbage collection.
The first one is a notification, and the call to System.GC () is only a request. When the JVM accepts this message, it does not do garbage collection immediately, but simply weights several garbage collection algorithms that make garbage collection operations easy to occur, occur earlier, or recycle more.
It is a requirement that the JVM recycle garbage in time. In fact, there is a need to reverse: in a certain period of time it is best not to recycle garbage. It is often hoped that the fastest real-time systems, especially embedded systems, are required to run fast.
The Java garbage collection mechanism is serviced for all Java application processes, not for a particular process. As a result, no process can dictate what the garbage collection mechanism does, how it is done, or how much it does.
Six What to do when an object is recycled
When an object is running, there may be something associated with it. As a result, when objects are about to be destroyed, some remedial work is sometimes needed. These operations can be written in the Finalize () method (often called The Terminator).
protected void Finalize ()
{
Finalization code here
}
The utility of this terminator is similar to the destructor in C + + and is automatically invoked. However, the timing of the two calls is different, which makes a significant difference in their performance behavior. C + + destructors are always invoked when an object leaves the scope. This means that the timing of the invocation of a C + + destructor is certain and can be applied to the knowledge. However, the Java terminator is when the object is destroyed. It is known from the above that when discarded objects are destroyed, the application is not informed. Also, for most occasions, the discarded object has not been destroyed after the application terminates.
When coding, consider this. For example, an object opens a file when it is in operation, does not close it when the object is discarded, and instead writes the file closing statement in the Terminator. Doing so can cause problems with file operations. If the file is open exclusively, other objects will not be able to access the file. If the file is open for sharing, another object that accesses the file will not be able to read the new content that the discarded object writes to the file until the end of the application.
At least for file manipulation, the coder should recognize the difference between the Java terminator and the C + + destructor.
Then, when the application terminates, will it execute all finalize () in the application. According to Bruce Eckel in the Thinking in Java: "At the end of the program, not all closure modules will be called." This refers only to the application of normal termination of the occasion, abnormal termination.
Therefore, which finishing operations can be put in finalize (), it needs to be * restricted.
A popular saying: in C + +, the system is doing garbage collection, and in Java, Java itself is doing.
In C + +, freeing the memory is handled manually, using the delete operator to free the allocated memory. This is the popular saying. Specifically, when the application thinks that an entity is not needed, it needs a delete to tell the system that it can reclaim the space. This request, to the coder, is a very troublesome, difficult to do. Any BBS, there are always a lot of topics about memory leaks in C + +.
Java uses a different, very convenient method: garbage Collection. The garbage collection mechanism is placed in the JVM. The JVM is fully responsible for garbage collection, applying space only when needed, and not having to worry about space recycling when discarding objects.
Two When the object is discarded.
In C + +, when an object leaves its scope, the object is discarded.
Is that the lifetime of an object is no longer associated with its scope, but only with reference.
Java garbage collection mechanism generally contains nearly 10 kinds of algorithms. We don't need to be concerned about the majority of these algorithms. There is only one of the simplest: the reference counting method, which is related to coding.
An object that can have one or more reference variables pointing to it. When an object no longer has any reference variable pointing to it, the object is discarded by the application. Or, this object can be recycled by the garbage collection mechanism.
That is, when there is no reference to an object, it means that the application tells the JVM: I don't want this object, you can recycle it.
The JVM's garbage collection mechanism detects the heap space in real time. When an object is found to have a reference count of 0 o'clock, the object is included in the list to be reclaimed. However, it is not immediately destroyed.
Three Discard is recycled.
The object is deemed to be unnecessary, and the memory it occupies can be freed. The reclaimed memory can be used for subsequent redistribution.
However, it is not the object that is discarded and then recycled immediately. The JVM process does a lot of overhead with space recycling. If an application process discards one object at a time, it will reclaim its space immediately, making the system inefficient.
As mentioned earlier, there are multiple algorithms for the JVM's garbage collection mechanism. Other algorithms are used to determine when and how to recycle, except that the reference notation is used to determine whether the object has been discarded. The JVM's garbage collection mechanism has to be balanced between time and space.
Therefore, to improve system efficiency, the garbage collector usually runs only when two conditions are met: The object is recycled and the system needs to be recycled. Remember that garbage collection takes time, so the Java runtime system uses it only when it is needed. So you don't know exactly when the garbage collection happens.
Four Is it useful to have an object pointing to a reference variable?
Previously, the object that did not hang the reference variable was discarded, meaning that it was garbage in the heap space and could be reclaimed by the JVM at any time.
There is, however, an exception that is not an exception. For disposable objects (some books are called temporary objects), you can point to it without referencing a variable. One of the simplest and most common examples:
System.out.println ("I am java!");
is to create a string object and pass it directly to the println () method.
Five Can the application interfere with garbage collection?
Many people are not comfortable with Java garbage collection and want to control the garbage collection operation of the JVM in the application code. It's impossible. For the garbage collection mechanism, there are only two ways to send messages to the JVM. The first one already says that all the reference variables that point to an object are removed. This is equivalent to sending a message to the JVM: This object is not. The second is the Call library method System.GC (), which most books say calls it to make Java garbage collection.
The first one is a notification, and the call to System.GC () is only a request. When the JVM accepts this message, it does not do garbage collection immediately, but simply weights several garbage collection algorithms that make garbage collection operations easy to occur, occur earlier, or recycle more.
It is a requirement that the JVM recycle garbage in time. In fact, there is a need to reverse: in a certain period of time it is best not to recycle garbage. It is often hoped that the fastest real-time systems, especially embedded systems, are required to run fast.
The Java garbage collection mechanism is serviced for all Java application processes, not for a particular process. As a result, no process can dictate what the garbage collection mechanism does, how it is done, or how much it does.
Six What to do when an object is recycled
When an object is running, there may be something associated with it. As a result, when objects are about to be destroyed, some remedial work is sometimes needed. These operations can be written in the Finalize () method (often called The Terminator).
protected void Finalize ()
{
Finalization code here
}
The utility of this terminator is similar to the destructor in C + + and is automatically invoked. However, the timing of the two calls is different, which makes a significant difference in their performance behavior. C + + destructors are always invoked when an object leaves the scope. This means that the timing of the invocation of a C + + destructor is certain and can be applied to the knowledge. However, the Java terminator is when the object is destroyed. It is known from the above that when discarded objects are destroyed, the application is not informed. Also, for most occasions, the discarded object has not been destroyed after the application terminates.
When coding, consider this. For example, an object opens a file when it is in operation, does not close it when the object is discarded, and instead writes the file closing statement in the Terminator. Doing so can cause problems with file operations. If the file is open exclusively, other objects will not be able to access the file. If the file is open for sharing, another object that accesses the file will not be able to read the new content that the discarded object writes to the file until the end of the application.
At least for file manipulation, the coder should recognize the difference between the Java terminator and the C + + destructor.
Then, when the application terminates, will it execute all finalize () in the application. According to Bruce Eckel in the Thinking in Java: "At the end of the program, not all closure modules will be called." This refers only to the application of normal termination of the occasion, abnormal termination.
Therefore, which finishing operations can be put in finalize (), it needs to be * restricted.
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.