In Java, What is System.gc() used for?
Answered
In Java, What is System.gc() used for?
Best answer
The method System.gc() is used for running the garbage collector. Calling the gc() method suggests that the Java Virtual Machine expend effort towards recycling unused objects in order to make the memory they currently occupy available for quick reuse. When control returns from the method call, the Java Virtual Machine has made a best effort to reclaim space from all discarded objects.
The call System.gc() is effectively equivalent to the call:
Runtime.getRuntime().gc()
However, you need to be very careful to call System.gc(). Calling it can add unnecessary performance issues to your application, and it is not guaranteed to actually perform a collection.