System.gc()与Runtime.getRuntime()

2019-07-13  本文已影响0人  不1见2不3散4

首先,我们需要明确一点的是,两个gc都会强制触发垃圾收集,它们只是向JVM建议,现在也许是收集垃圾的好时机。

比较

System.gc()调用起来更方便,但是会给应用带来不必要的性能问题。还可以通过参数-XX:+DisableExplicitGC.禁止显示调用gc。

Runtime.getRuntime()用来与Java运行时进行交互,调用该方法会建议JVM花费精力回收不再使用的对象。

源代码

JDK8
System.gc()源代码, 来自System.java

    /**
     * Runs the garbage collector.
     * <p>
     * Calling the <code>gc</code> 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.
     * <p>
     * The call <code>System.gc()</code> is effectively equivalent to the
     * call:
     * <blockquote><pre>
     * Runtime.getRuntime().gc()
     * </pre></blockquote>
     *
     * @see     java.lang.Runtime#gc()
     */
    public static void 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.
     * <p>
     * The name <code>gc</code> stands for "garbage
     * collector". The virtual machine performs this recycling
     * process automatically as needed, in a separate thread, even if the
     * <code>gc</code> method is not invoked explicitly.
     * <p>
     * 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) {
        List<String> stringList = 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()是实例方法,没有实质差别。

上一篇下一篇

猜你喜欢

热点阅读