In Java, System.runFinalization() asks the JVM to run the finalize() methods of objects that the garbage collector has found unreachable but not yet cleaned up. Like gc(), it is only a best-effort request: when it returns, the JVM has tried, but nothing is promised.
Finalization turned out to be unreliable and slow. Object.finalize() has been deprecated since Java 9, and Java 18 (JEP 421) deprecated finalization for removal, including System.runFinalization(). New code should close resources explicitly with try-with-resources, or use java.lang.ref.Cleaner as a safety net.
A static method of the java.lang.System class, available since the first version of Java (JDK 1.0). Call it on the class name, as in System.runFinalization(...). No import is needed, because classes in java.lang are always visible. Deprecated for removal since Java 18.
Deprecated for removal.
The method still works in current JDKs, but the compiler warns about it on Java 18 and later, and a future release may make it do nothing or remove it. JDK 18 also added the --finalization=disabled option to test programs without finalization.
Syntax
No arguments and no result. It was usually called right after System.gc():
Java
System.gc(); // suggest a collection
System.runFinalization(); // suggest running pending finalizers
// Modern code: try (Resource r = ...) { ... } closes r for sure
Call
What it does
System.runFinalization()
Requests that pending finalizers run.
Runtime.getRuntime().runFinalization()
The same request through Runtime (also deprecated).
try (Res r = new Res()) { }
Calls r.close() at the end of the block; the reliable replacement.
cleaner.register(obj, action)
Java 9+ Cleaner: runs action once, explicitly or after obj becomes unreachable.
Finalizers run on a separate JVM thread in no particular order. Whether a given object's finalize() ever runs is not guaranteed, not even at program exit.
Library declarations
Java
// java.lang.System
@Deprecated(since="18", forRemoval=true)
public static void runFinalization()
Parameters
Parameter
Description
None
runFinalization() takes no parameters.
Return Value
Return Value
Description
void
Nothing. The call does not report which finalizers, if any, were run.
Compare with Similar Methods
Ways to clean up resources, from worst to best:
Method
How it differs
finalize() + runFinalization()
Unpredictable timing and order; deprecated for removal.
Only frees memory, never closes files or connections.
Cleaner (Java 9+)
A safety net that runs a cleanup action at most once.
try-with-resources (Java 7+)
Closes resources at a known point, even when an exception is thrown. The first choice.
Java Examples
Example 1 — A Harmless Call to runFinalization()
The method can still be called. This program defines no finalizers, so the call has nothing to do and simply returns. On Java 18 and later the compiler prints a deprecation warning.
Java
public class SystemRunFinalizationCall {
public static void main(String[] args) {
String[] words = {"alpha", "beta", "gamma"};
System.out.println("Words before cleanup: " + words.length);
words = null;
System.gc();
System.runFinalization();
System.out.println("gc() and runFinalization() both returned");
System.out.println("Nothing else is guaranteed to have happened");
}
}
Output
Words before cleanup: 3
gc() and runFinalization() both returned
Nothing else is guaranteed to have happened
How It Works
Make something unreachable. Setting words to null drops the only reference to the array, so it becomes eligible for collection.
The old two-step. Legacy code called gc() and then runFinalization() hoping that finalizers would run right away; both are just requests.
Nothing to report. Neither call returns information, and arrays have no finalizer, so the only safe output is that the calls returned.
Example 2 — The Replacement: Close Resources with try-with-resources
Instead of waiting for a finalizer, implement AutoCloseable. Java calls close() at a known moment, in reverse order of opening.
Java
public class SystemRunFinalizationTryWith {
static class Connection implements AutoCloseable {
private final String name;
Connection(String name) {
this.name = name;
System.out.println("Open " + name);
}
@Override
public void close() {
System.out.println("Close " + name);
}
}
public static void main(String[] args) {
try (Connection db = new Connection("database"); Connection cache = new Connection("cache")) {
System.out.println("Working with both connections");
}
System.out.println("Cleanup done without any finalizer");
}
}
Output
Open database
Open cache
Working with both connections
Close cache
Close database
Cleanup done without any finalizer
How It Works
Resources in the try header. Both Connection objects are created in the parentheses after try, in the order written.
Closed when the block ends. Leaving the block calls close() automatically, last opened first: cache, then database. This also happens if the block throws.
No guessing. The cleanup lines always appear at the same point, which is exactly what finalize() plus runFinalization() could never promise.
Example 3 — A Safety Net with java.lang.ref.Cleaner (Java 9+)
This example needs Java 9 or newer. A Cleaner runs a cleanup action at most once: explicitly through clean(), or later if the object becomes unreachable.
Java
import java.lang.ref.Cleaner;
public class SystemRunFinalizationCleaner {
private static final Cleaner CLEANER = Cleaner.create();
public static void main(String[] args) {
Object buffer = new Object();
Cleaner.Cleanable cleanable = CLEANER.register(buffer, () -> System.out.println("Cleanup action ran"));
System.out.println("Buffer registered");
cleanable.clean();
cleanable.clean();
System.runFinalization();
System.out.println("The second clean() did nothing");
}
}
Output
Buffer registered
Cleanup action ran
The second clean() did nothing
How It Works
Register an action.register(buffer, action) links a cleanup action to the object. The lambda must not refer to buffer itself, or the object could never become unreachable.
Run it on purpose. The first clean() runs the action right away in the current thread and prints "Cleanup action ran".
At most once. The second clean() and the runFinalization() request find nothing left to do, unlike a finalizer that might run late or never.
Frequently Asked Questions
It asks the JVM to run the finalize() methods of objects that are waiting for finalization. It is only a best-effort request and returns without reporting anything.
Yes. Java 18 deprecated finalization for removal (JEP 421), including System.runFinalization() and Runtime.runFinalization(). Object.finalize() itself has been deprecated since Java 9.
Close resources with try-with-resources and AutoCloseable. As a backup, Java 9 added java.lang.ref.Cleaner.
No. There is no guarantee when, in which order, or even whether a finalizer runs.
Not directly. A collection may queue objects with finalizers; a separate finalizer thread runs them later, if at all.