Runtime.getRuntime()是否有任何副作用.gc()

时间:2013-09-06 05:57:48

标签: java performance coding-style garbage-collection runtime

我想知道在我们的程序中明确使用Runtime.getRuntime().gc()有任何副作用吗?

或者使用相同的代码获得更好的性能是一种好习惯吗?

3 个答案:

答案 0 :(得分:4)

Runtime.getRuntime().gc()可能会导致副作用。

  1. Runtime.getRuntime().gc() - 不保证gc会运行

  2. Runtime.getRuntime().gc() - 如文档所述,这将暗示GC可以采取行动,如果GC决定准备好运行,它可能会与应用程序处理线程重叠并导致延迟和缓慢。

  3. 建议不要使用此功能,而是尝试调整GC以使系统正常运行。

答案 1 :(得分:1)

即使您使用Runtime.getRuntime()。gc()也无法保证内存是免费的

答案 2 :(得分:1)

每次使用Runtime.getRuntime().gc()时,您的程序都会放慢速度。

相关问题