Runtime.getRuntime().gc() 在 Java 8 中继续使用或有任何区别吗?

Is Runtime.getRuntime().gc() of any use or makes any difference as its continued in Java 8?

系统和运行时classes中都有gc()方法,系统class的gc()方法调用运行时的方法。

public static void gc() {
        Runtime.getRuntime().gc();
    }

根据 Java 运行时 gc() 的代码文档 class

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.

调用Runtime.getRuntime().gc()时有什么特别的地方吗? 如果我调用它而不调用它会发生什么?

我已经知道了什么?

调用 gc() 不要立即调用垃圾收集器,当垃圾收集应该发生时它会调用 jvm。

编辑

我想了解为什么 gc() 仍然存在于 Java8 中。其他线程上的讨论没有被接受的答案,而且很旧。

也许您正在编写 a benchmarking library 并且在 运行 执行基准测试时尽量减少 GC 的干扰对您来说很重要。

所以你可能想要 to call gc() between runs(因为你正在编写低级的东西并且你知道在实践中你的目标 JRE 实际上 运行 GC 调用 gc())...

有几个与此主题相关的答案以及互联网上的大量讨论。这里有一些链接可以帮助未来的读者更好地掌握它。

  1. 阅读 Oracle 在常见问题解答中对 System.gc() 的看法。
  2. 已记录 bug,但关于 System.gc() 的误导性文档仍未解决。
  3. https://community.oracle.com/thread/1542855