System 类和 Runtime 类中都有 gc()
方法,System 类的 gc()
方法调用 Runtime 的方法。
public static void gc() {
Runtime.getRuntime().gc();
}
根据 Runtime 类中 gc() 的 Java 代码文档
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调用。
编辑
我想了解为什么 Java8 中仍然存在 gc()
。其他线程上的讨论没有接受的答案,而且已经很老了。
最佳答案
也许您正在写a benchmarking library在运行基准测试时尽量减少 GC 的干扰对您来说非常重要。
所以你可能想要to call gc()
between runs (因为您正在编写低级内容,并且您知道实际上您的目标 JRE 将在调用 gc()
时实际运行 GC)...
关于java - Runtime.getRuntime().gc() 在 Java 8 中继续存在,有什么用处或者有什么区别吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32439777/