System.gc()与Runtime.getRuntime().gc()区别_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > System.gc()与Runtime.getRuntime().gc()区别

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

 2019/7/23 16:10:27  高级java工程师  程序员俱乐部  我要评论(0)
  • 摘要:首先,我们需要明确一点的是,两个gc都会强制触发垃圾收集,它们只是向JVM建议,现在也许是收集垃圾的好时机。比较System.gc()调用起来更方便,但是会给应用带来不必要的性能问题。还可以通过参数-XX:+DisableExplicitGC.禁止显示调用gc。Runtime.getRuntime()用来与Java运行时进行交互,调用该方法会建议JVM花费精力回收不再使用的对象。源代码System.gc()源代码,来自System.java/***Runsthegarbagecollector
  • 标签:system 区别
首先,我们需要明确一点的是,两个gc都会强制触发垃圾收集,它们只是向JVM建议,现在也许是收集垃圾的好时机。
比较
System.gc()调用起来更方便,但是会给应用带来不必要的性能问题。还可以通过参数-XX:+DisableExplicitGC.禁止显示调用gc。
Runtime.getRuntime()用来与Java运行时进行交互,调用该方法会建议JVM花费精力回收不再使用的对象。
源代码
System.gc()源代码, 来自System.java

class="java" name="code">
  /**
     * 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()是实例方法,没有实质差别。

上一篇: 理解Java的强引用、软引用、弱引用和虚引用 下一篇: 没有下一篇了!
发表评论
用户名: 匿名