System.gc() is a request that the JVM run garbage collection. It is a hint, not a command: the specification only says the JVM makes a best effort to reclaim unused objects. In HotSpot, the default behaviour is to run a full collection, which pauses the application, so calling it from application code is almost always a mistake. The garbage collector knows far better than your code when memory needs reclaiming.
System.gc() has a few legitimate uses in tests, diagnostics and some tooling, and it is sometimes invoked indirectly by libraries without you noticing.
System.gc() is equivalent to Runtime.getRuntime().gc(). Both ask the JVM to reclaim memory used by unreachable objects. What happens next is implementation specific.
-XX:+DisableExplicitGC, the call is ignored.-XX:+ExplicitGCInvokesConcurrent, collectors that support it (such as G1) run a concurrent cycle instead of a full pause.Because behaviour depends on flags and the JVM, never write code that relies on System.gc() having freed anything.
Modern collectors such as G1 (the default since Java 9), ZGC and Shenandoah are tuned to run when they have to and to meet pause-time goals. A forced full collection ignores that tuning.
| Problem | Effect |
|---|---|
| Stop-the-world pause | Latency spikes in servers and UIs |
| Wasted work | Collecting when memory is not under pressure |
| Hides real leaks | Memory stays high because objects are still reachable |
| Poor portability | Behaviour changes between JVMs and flags |
If you believe you need it to avoid OutOfMemoryError, the cause is usually retained references (static maps, caches, listeners) or a heap that is too small. Fix those instead.
You can watch what an explicit call does by enabling unified logging (Java 9 and later).
Run it with java -Xlog:gc GcDemo. The log shows a pause whose cause is System.gc(), and the used memory usually drops. The exact numbers vary by JVM, collector and flags, and the program prints nothing guaranteed.
WeakReference and then call System.gc() to try to clear the referent. Treat these tests as best effort and poll with a timeout rather than asserting after one call.-XX:+DisableExplicitGC or raise the interval with sun.rmi.dgc.client.gcInterval.System.gc() when the direct memory limit is reached, so disabling explicit GC can cause OutOfMemoryError: Direct buffer memory for heavy users. Release direct buffers deterministically where you can, or size -XX:MaxDirectMemorySize appropriately.jcmd <pid> GC.heap_info, GC logs and a heap dump analysed in tools such as Eclipse MAT.finalize() for cleanup. It is deprecated for removal; use try-with-resources or java.lang.ref.Cleaner.No. It is a suggestion. The JVM may run a collection, defer it, or ignore the call, depending on the implementation and flags such as -XX:+DisableExplicitGC.
Yes. System.gc() simply calls the Runtime method.
Yes. In an operations context, jcmd <pid> GC.run requests one, which is useful for diagnosing memory without changing code.
It is common when a library calls it too often, but check RMI and direct buffer usage first, since disabling it can cause side effects. Measure before and after.
System.gc() only suggests that the JVM collect garbage, and on HotSpot it usually causes a full pause. Avoid it in application code, fix the references that keep memory alive, and tune the collector instead. Reserve it for tests, benchmarks and diagnostics, and know that RMI and direct buffers may call it for you.