我正好遇到下面的代码(略简体):定期垃圾收集是否有助于JVM的性能?
/* periodically requests garbagecollect to improve memory usage and
garbage collect performance under most JVMs */
static class GCThread implements Runnable {
public void run() {
while(true) {
try {
Thread.sleep(300000);
} catch (InterruptedException e) {}
System.gc();
}
}
}
Thread gcThread = new Thread(new GCThread());
gcThread.setDaemon(true);
gcThread.start();
我尊重代码作者,但不再具有方便地访问,请他捍卫他的主张在顶部的评论。
这是真的吗?与我的直觉相反,这个小小的黑客应该改进任何东西。我希望JVM有更好的装备来决定何时执行一个集合。
代码运行在Z/OS上的IBM WebSphere内部运行的Web应用程序中。
我非常怀疑这一点。把代码拿出来,让作者在基准测试中显示出来,然后再把它放回去。 – delnan 2011-02-26 20:54:22
无论如何RMI会定期调用GC(如果未收到次要的集合) – 2011-02-27 00:17:20