In Java, System.gc()suggests that the JVM run the garbage collector now, to reclaim memory used by objects that nothing refers to any more. When the call returns, the JVM has made a "best effort", which may mean a full collection, a small one, or nothing at all.
Normal programs never need it: the JVM collects garbage automatically when it needs memory, and it is usually better at choosing the moment. You will mostly see gc() in demos, memory experiments, and some tests. An explicit call can even make a program slower, because a full collection may pause all threads.
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.gc(...). No import is needed, because classes in java.lang are always visible.
Syntax
No arguments and no result. These two lines do exactly the same thing:
Java
System.gc(); // suggestion to collect garbage
Runtime.getRuntime().gc(); // the method System.gc() calls
Call
What it does
System.gc()
Asks the JVM to collect unreachable objects.
Runtime.getRuntime().gc()
The same request through the Runtime object.
Runtime.getRuntime().freeMemory()
Free heap bytes; changes after collections (do not rely on exact numbers).
java -XX:+DisableExplicitGC App
HotSpot option that turns System.gc() into a no-op.
The JVM may ignore the request completely, for example when started with -XX:+DisableExplicitGC. Objects that are still reachable from your code are never collected, no matter how often you call gc().
Library declarations
Java
// java.lang.System
public static void gc()
Parameters
Parameter
Description
None
gc() takes no parameters.
Return Value
Return Value
Description
void
Nothing. You cannot find out from the call whether a collection happened or how much memory was freed.
Compare with Similar Methods
Related memory tools:
Method
How it differs
Runtime.getRuntime().gc()
Identical behaviour; System.gc() is the short form.
Asks the JVM to run pending finalizers; deprecated for removal.
WeakReference
Lets an object be collected while you still hold a "weak" pointer to it.
-Xmx option
Sets the maximum heap size; the real fix when a program needs more memory.
Java Examples
Example 1 — Create Garbage and Ask for a Collection
Temporary arrays become garbage after each loop pass. Calling gc() returns normally whether or not the JVM collects them.
Java
public class SystemGcRequest {
public static void main(String[] args) {
int created = 0;
for (int i = 0; i < 1000; i++) {
byte[] temp = new byte[10000];
temp[0] = 1;
created++;
}
System.out.println("Temporary arrays created: " + created);
System.gc();
System.out.println("System.gc() returned");
Runtime.getRuntime().gc();
System.out.println("Runtime.getRuntime().gc() returned");
}
}
Output
Temporary arrays created: 1000
System.gc() returned
Runtime.getRuntime().gc() returned
How It Works
Short-lived objects. Each pass allocates a 10000-byte array that is unreachable as soon as the pass ends, about 10 MB of garbage in total.
A request, not a command.System.gc() may start a collection, a partial one, or nothing; the program just continues once the call returns.
Same thing, longer name.System.gc() simply calls Runtime.getRuntime().gc(), so the second request behaves the same. What happened inside the heap is not printed, because it varies.
Example 2 — Reachable Objects Survive, Unreachable Ones May Go
A WeakReference does not keep its object alive. While a normal (strong) reference exists, gc() cannot clear it.
Java
import java.lang.ref.WeakReference;
public class SystemGcWeakReference {
public static void main(String[] args) {
StringBuilder strong = new StringBuilder("still in use");
WeakReference<StringBuilder> weak = new WeakReference<>(strong);
System.gc();
System.out.println("Weak reference still set: " + (weak.get() != null));
System.out.println("Strong reference: " + strong);
strong = null;
System.gc();
System.out.println("Now the object MAY be collected");
}
}
Output
Weak reference still set: true
Strong reference: still in use
Now the object MAY be collected
How It Works
Two kinds of reference.strong is an ordinary variable; weak points to the same StringBuilder but does not stop the collector from removing it.
Strongly reachable means safe.strong is still used after the first gc(), so the object cannot be collected and weak.get() is guaranteed to return it.
Cleared or not is up to the JVM. After strong = null only the weak reference is left. The next collection may clear it, but gc() does not promise that, so the program does not print the result.
Example 3 — Data You Still Reference Is Never Collected
Objects stored in a variable or array are "live". A collection cannot touch them, which is why gc() never fixes a memory leak.
Java
public class SystemGcLiveData {
public static void main(String[] args) {
int[][] blocks = new int[3][];
for (int i = 0; i < blocks.length; i++) {
blocks[i] = new int[100000];
blocks[i][0] = i + 1;
}
System.gc();
Runtime rt = Runtime.getRuntime();
long used = rt.totalMemory() - rt.freeMemory();
System.out.println("Blocks kept: " + blocks.length);
System.out.println("First values added up: " + (blocks[0][0] + blocks[1][0] + blocks[2][0]));
System.out.println("Heap in use is positive: " + (used > 0));
}
}
Output
Blocks kept: 3
First values added up: 6
Heap in use is positive: true
How It Works
Three live blocks. Each int[100000] takes about 400 KB and is stored in blocks, so all three stay reachable during the collection.
Values intact. After gc() the first elements are still 1, 2, and 3, which add up to 6: collecting garbage never changes live objects.
Memory numbers vary.totalMemory() - freeMemory() is the heap currently in use. Its exact size depends on the JVM and collector, so the program only checks that it is above zero.
Frequently Asked Questions
No. It only suggests a collection. The JVM may run a full collection, a partial one, or ignore the call completely, for example with -XX:+DisableExplicitGC.
None in behaviour. System.gc() is a convenient static shortcut that calls Runtime.getRuntime().gc().
Almost never. The JVM collects garbage automatically when memory is needed, and an explicit full collection can pause your program. Fix leaks by removing references instead.
No. The JVM already collects as much as it can before it throws OutOfMemoryError. If live objects fill the heap, you need to keep fewer objects or raise the heap size with -Xmx.
Either something still references it (a variable, a list, a static field, a listener), or the JVM decided not to collect it yet. Collection of any particular object is never guaranteed.