Runtime

Suggest Object Finalization to the JVM

In this example we shall show you how to suggest an Object Finalization to the JVM. We are using the Runtime class. Every Java application has a single instance of class Runtime that allows the application to interface with the environment in which the application is running. The current runtime can be obtained from the getRuntime method. An application cannot create its own instance of this class. To suggest an Object Finalization to the JVM one should perform the following steps:

  • Use getRuntime() API method of Runtime. This method returns the runtime object associated with the current Java application.
  • Use runFinalization() API method that runs the finalization methods of any objects pending finalization,

as described in the code snippet below.

package com.javacodegeeks.snippets.core;

public class RunFinalizationExample {

	public static void main(String args[]) {
		// get current Java Runtime using getRuntime()
		Runtime runtime = Runtime.getRuntime();

		// Run discarded object's finalization method
		runtime.runFinalization();
	}

}

 
This was an example of how to suggest an Object Finalization to the JVM in Java.

Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button