Garbage Collection in java
Garbage Collection in java:
Garbage collection is the process of automatically freeing objects that are no longer referenced by the program. As we have seen that in C++ dynamically allocated memory, via the keyword new, is destroyed by delete operator. The JVM's heap stores all objects created by an executing Java program. Objects are created by Java's "new" operator, and memory for new objects is allocated on the heap at run time. This frees the programmer from having to keep track of when to free allocated memory, thereby preventing many potential bugs and headaches. The name "garbage collection" implies that objects that are no longer needed by the program are "garbage" and can be thrown away. A more accurate and up-to-date metaphor might be "memory recycling." When an object is no longer referenced by the program, the heap space it occupies must be recycled so that the space is available for subsequent new objects. The garbage collector must somehow determine which objects are no longer referenced by the program and make available the heap space occupied by such unreferenced objects. In the process of freeing unreferenced objects, the garbage collector must run any finalizers of objects being freed.
Destructors:
At the moment we will not study in detail about the finalize method but would just see how it is used.
The finalize( ) Method
Garbage collection is the process of automatically freeing objects that are no longer referenced by the program. As we have seen that in C++ dynamically allocated memory, via the keyword new, is destroyed by delete operator. The JVM's heap stores all objects created by an executing Java program. Objects are created by Java's "new" operator, and memory for new objects is allocated on the heap at run time. This frees the programmer from having to keep track of when to free allocated memory, thereby preventing many potential bugs and headaches. The name "garbage collection" implies that objects that are no longer needed by the program are "garbage" and can be thrown away. A more accurate and up-to-date metaphor might be "memory recycling." When an object is no longer referenced by the program, the heap space it occupies must be recycled so that the space is available for subsequent new objects. The garbage collector must somehow determine which objects are no longer referenced by the program and make available the heap space occupied by such unreferenced objects. In the process of freeing unreferenced objects, the garbage collector must run any finalizers of objects being freed.
Destructors:
At the moment we will not study in detail about the finalize method but would just see how it is used.
The finalize( ) Method
- Every class inherits the finalize() method from java.lang.
- Object the method is called by the garbage collector when it determines no more references to the object exist.
- The Object finalize method performs no actions but it may be overridden by any class.
- Normally it should be overridden to clean-up non-Java resources ie closing a file .
Join me on Google+
protected void finalize
()
{
if (aFile != null) {
aFile.close();
aFile = null;
}
}
