比较
System.gc()调用起来更方便,但是会给应用带来不必要的性能问题。还可以通过参数-XX:+DisableExplicitGC.禁止显示调用gc。
Runtime.getRuntime()用来与Java运行时进行交互,调用该方法会建议JVM花费精力回收不再使用的对象。
源代码
System.gc()源代码, 来自System.java
/** * Runs the garbage collector. ** Calling the
gc
method suggests that the Java Virtual * Machine expend effort toward 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: ** * @see java.lang.Runtime#gc() */ public static void gc() { Runtime.getRuntime().gc(); }* Runtime.getRuntime().gc() *
Runtime.getRuntime().gc()源代码,来自Runtime.java
/** * Runs the garbage collector. * Calling this method suggests that the Java virtual machine expend * effort toward recycling unused objects in order to make the memory * they currently occupy available for quick reuse. When control * returns from the method call, the virtual machine has made * its best effort to recycle all discarded objects. ** The name
gc
stands for "garbage * collector". The virtual machine performs this recycling * process automatically as needed, in a separate thread, even if the *gc
method is not invoked explicitly. ** The method {@link System#gc()} is the conventional and convenient * means of invoking this method. */ public native void gc();
示例:
import lombok.extern.slf4j.Slf4j; import java.util.ArrayList; import java.util.List; import java.util.concurrent.TimeUnit; /** * Created by EricYang on 2019/7/13. */ @Slf4j public class GcMainDemo { public static void main(String[] args) { ListstringList = new ArrayList<>(); for(int i=0;i < 100; i++) { stringList.add(new String("The " + i + "th string")); } //unit bytes long maxMemBefore = Runtime.getRuntime().freeMemory(); //System.gc(); Runtime.getRuntime().gc(); long maxMemAfter = Runtime.getRuntime().freeMemory(); log.info("System.gc(), maxMemBefore={}, maxMemAfter={}, diff={}kb", maxMemBefore, maxMemAfter, (maxMemAfter - maxMemBefore)/1024); for(int i=0;i < 100; i++) { stringList.add(new String("The " + i + "th string")); } maxMemBefore = Runtime.getRuntime().freeMemory(); Runtime.getRuntime().gc(); maxMemAfter = Runtime.getRuntime().freeMemory(); log.info("Runtime.getRuntime().gc(), maxMemBefore={}, maxMemAfter={}, diff={}kb", maxMemBefore, maxMemAfter, (maxMemAfter - maxMemBefore)/1024); try { TimeUnit.SECONDS.sleep(2000); } catch (Exception ex) { log.error("sleep exception", ex); } } }
程序运行输出(不同机器运行结果不一样)
16:39:22.616 [main] INFO com.yq.GcMainDemo - System.gc(), maxMemBefore=244672784, maxMemAfter=253615304, diff=8732kb
16:39:22.635 [main] INFO com.yq.GcMainDemo - Runtime.getRuntime().gc(), maxMemBefore=250951864, maxMemAfter=253949240, diff=2927kb
结论
两者一样。除了方法名称不同, System.gc()是静态方法, 而Runtime.getRuntime().gc()是实例方法,没有实质差别。