System.gc()与Runtime.getRuntime().gc()区别

8 篇文章 0 订阅

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

比较

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

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

源代码

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()是实例方法,没有实质差别。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值