Finalize () in Java ()

Source: Internet
Author: User

This is the best article I have read about finalize in Java.

Finalize () in Java ()

When the garbage collector wants to recycle objects, it first needs to call the Finalize method of this class (you can write a program to verify this conclusion). Generally, classes written in pure Java do not need to overwrite this method again, because the object has implemented a default function, unless we want to implement special functions (this involves many things, such as the object space tree ).
However, for Classes written in code other than Java (such as the memory allocated by the new method of JNI and C ++), the garbage collector cannot correctly recycle these parts, in this case, we need to overwrite the default method to release and recycle the memory correctly (for example, C ++ requires delete ).
In short, finalize is equivalent to a destructor. It is the first method to be called when the Garbage Collector recycles an object. However, because Java's garbage collection mechanism can automatically do these tasks for us, we generally do not need to manually release them.

Some operations are required to cancel an object. For example, if an object is processing non-Java resources, such as a file handle or a window font, make sure that these resources are released before an object is revoked. To handle such a situation, Java provides a mechanism called finalization. You can use this mechanism to define some special operations that are executed when an object is to be released by the garbage collection program.
To add finalizer to a class, you only need to define the finalize () method. This method is called when Java recycles an object of this class. In the finalize () method, you must specify the operations that must be performed before an object is revoked. Garbage collection runs cyclically, and check that the object is no longer referenced by the running state or indirectly referenced by other objects. Before the object is released, the Java runtime system calls the finalize () method of the object.

The general format of the finalize () method is as follows:

Protected void finalize ()
{
// Finalization code here
}

The keyword protected is used to prevent code defined outside of this class from accessing the finalize () identifier. This identifier and other identifier will be explained in chapter 7th.

It is important to understand that finalize () is called before garbage collection. For example, if an object exceeds its scope, finalize () is not called. This means that you cannot know when -- or even whether -- finalize () is called. Therefore, your program should provide other methods to release the system resources used by the object, rather than relying on finalize () to complete normal operations of the program.

Note: If you are familiar with C ++, you know that C ++ allows you to define a destructor for a class, which is called before the object is out of scope. Java does not support this idea and does not provide undo functions. The finalize () method is only similar to the function of revoking a function. When you have rich experience with Java, you will see that Java uses the garbage collection subsystem and there is almost no need to use the Undo function.


Finalize should work like this: Once the garbage collector is ready to release the storage space occupied by the object, it first calls finalize (), and only in the next garbage collection process, will actually recycle the object memory. therefore, if finalize () is used, important cleanup or cleanup operations can be performed during garbage collection.

When will finalize () be called?
There are three cases
1. All objects are automatically called when they are garbage collection, such as when system. GC () is run.
2. The Finalize method is called once for each object when the program exits.
3. explicitly call the Finalize method

In addition, under normal circumstances, when an object is collected as useless information by the system, finalize () will be automatically called, but the JVM does not guarantee that finalize () will be called, that is, the call to finalize () is uncertain, Which is why Sun does not advocate the use of finalize ().

Some operations are required to cancel an object. For example, if an object is processing non-Java resources, such as a file handle or a window font, make sure that these resources are released before an object is revoked. To handle such a situation, Java provides a mechanism called finalization. You can use this mechanism to define some special operations that are executed when an object is to be released by the garbage collection program.

To add finalizer to a class, you only need to define the finalize () method. This method is called when Java recycles an object of this class. In the finalize () method, you must specify the operations that must be performed before an object is revoked. Garbage collection runs cyclically, and check that the object is no longer referenced by the running state or indirectly referenced by other objects. Before the object is released, the Java runtime system calls the finalize () method of the object.

The general format of the finalize () method is as follows:

Protected void finalize ()
{
// Finalization code here
}

The keyword protected is used to prevent code defined outside of this class from accessing the finalize () identifier. This identifier and other identifier will be explained in chapter 7th.

It is important to understand that finalize () is called before garbage collection. For example, if an object exceeds its scope, finalize () is not called. This means that you cannot know when -- or even whether -- finalize () is called. Therefore, your program should provide other methods to release the system resources used by the object, rather than relying on finalize () to complete normal operations of the program.

Note: If you are familiar with C ++, you know that C ++ allows you to define a destructor for a class, which is called before the object is out of scope. Java does not support this idea and does not provide undo functions. The finalize () method is only similar to the function of revoking a function. When you have rich experience with Java, you will see that Java uses the garbage collection subsystem and there is almost no need to use the Undo function.

During garbage collection, the garbage collector automatically calls the Finalize method of the object to perform custom non-memory cleanup, because the garbage collector does not process anything other than the memory. Therefore, you may need to define some cleanup methods, such as processing non-memory resources such as files and ports.

From: http://hi.baidu.com/xiantong100/item/6bad7e0f2f75a6c890571838

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.