Java termination function detailed

Source: Internet
Author: User

What is the Java termination function?

For an object, the Java termination function performs the final validation work. This is in contrast to the Java structure function, which creates and initializes a Java class instance. When an object is not needed and the resources must be used for other objects, the Java termination function can be used to purge tasks in a class instance or in a distributed system resource, such as a file descriptor or a network jack connection. You do not need proof or return any values for the termination function. Unfortunately, when a class or interface is loaded, the current description of the Java language does not include any explanation of the termination function for Java classes or interfaces. Let's take a closer look at the finalization () method of the Java.lang object's termination function, providing a method instance. (How to use the Clone function in PHP5)

protected void Finalize () throws Throwable

When a Java object is no longer needed, the space that the object originally occupies is expected to be automatically recycled by the Java Recycle tool. This is a significant difference in Java, and is uncommon in most structured programming languages, such as the C language. If a class instance implements the finalization () method of the termination function, the space it occupies cannot be recycled by the Recycle tool in a timely manner. The worst case scenario is that it may not be recycled at all. Any class instance that implements the Finalize () method of the terminating function often calls the terminating object. When they are no longer referenced, they cannot be reclaimed by the Java Recycle tool immediately, attaching objects to the specified queue for the final Program Java Recycle tool. is usually performed by a specified linear program, and is referred to as a "reference handle" on some Java virtual machines. In the final program phase, the terminating function linear program executes the Finalize () method for each object's termination function. When Finalize () is successfully executed, the Java Recycle tool will deliver the object, regenerating the space it occupies by the "future" fragmentation collection function. I did not say "existing", which means that at least two pieces of the collection cycle must be required to reclaim the terminated object. That sounds like some kind of consumption? Right. We need some way to make space available again.

The linear termination function is not given the maximum priority in the system. Higher-priority threads cause terminated objects to be aligned, and if a linear "Java termination function" cannot be consistent with this efficiency, the termination function queue continues to grow, causing the Java heap to accumulate continuously. The final Java heap will be depleted, and the java.lang.OutOfMemoryError will be thrown away.

For any object, a Java virtual machine will not have more than one reference termination function Finalize () method. If any exception is thrown by the Finalize () method, the object's terminating program stops.

For the Finalize () method of the class you are almost free to do anything. When you do this, when the object is no longer referenced or is no longer needed, do not expect the storage space to be occupied by any object reclaimed by the Java Recycle Program. Why? The Finalize () method is not controllable for the complete completion of the schedule. The worst case scenario is that when there are no more objects involved, it may not even be resolved. This means that any object with a Finalize () method is not recoverable. This is a potential danger to memory management development, and needless to say, there is a significant overhead of spending on queues, running the Finalize () method, and reflecting objects to the next defragmentation link.

If you want to run a function on an object, consider terminating the function as the last method of performing your own garbage removal method, which will be even more smooth. Full trust termination functions are very dangerous to do after the garbage cleanup, especially if your termination object involves local resources.

The actual operating experience of the Java termination function

Within Objectwyieldfinalizer, we can perform a finalize () method with a linear yield () so that finalize () cannot be fully executed, as shown in code table I. The linear yield () method blocks the execution of existing linear programs from running programs and allows other linear programs to execute. If the terminating function linear program calls this finalize () method, it will suspend execution.

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.