问题描述
只需调用 System.gc()
,就可以在Java中进行垃圾收集,但有时会拖延应用程序。
You can garbage collect in Java simply by calling System.gc()
but sometimes this "stalls" the application. Is it a bad idea to garbage collect like this and avoid stalls:
new Thread(new Runnable() {
public void run() {
System.gc();
}
}).start();
或者这可能导致更多问题?
Or might this lead to even more problems?
推荐答案
是的,大多数情况下调用System.gc()是一个非常糟糕的主意。有一些例外,但他们很少,最好花时间确保你没有在GC环境中做出损害性能的事情,并研究并确保你了解gc的工作方式,而不是尝试通过自己来处理显式调用System.gc()。
Yes, most times it is a very bad idea to call System.gc(). There are exceptions but they are few and it is mostly better to spend the time making sure you are not doing things that hurt performance in a GC environment and to study and make sure you understand how a gc works than to try to handle it yourself by explicitly calling System.gc().
这篇关于智能垃圾收集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!