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.