简体   繁体   English

垃圾收集器可以调用多少次?

[英]How many times can the Garbage Collector be called?

我们可以在一个 Java 程序中多次调用垃圾收集器吗?

If by "call the Garbage Collector" you mean "invoke System.gc() -- you can do it as often as you like. It may have some effect, or it may have none. Depending on the circumstances, it may make your application perform better, or it may make things worse.如果“调用垃圾收集器”的意思是“调用System.gc() ——你可以System.gc()执行它。它可能有一些效果,也可能没有效果。根据情况,它可能会让你的应用程序性能更好,否则可能会使事情变得更糟。

Modern Java implementations are designed to run with completely transparent GC.现代 Java 实现旨在与完全透明的 GC 一起运行。 My experience is that it rarely helps to poke the GC into action although, nearly always, calling gc() does actually elicit some sort of response from the JVM -- it's not usually ignored completely.我的经验是,虽然几乎总是调用gc()确实会引起 JVM 的某种响应——它通常不会被完全忽略,但它很少有助于将 GC 付诸行动。

I've seen applications that use repeated calls to gc() to overcome bugs in the application (usually related to resource management);我见过使用重复调用gc()来克服应用程序中的错误(通常与资源管理有关)的应用程序; or even, very occasionally, bugs in the JVM.甚至,偶尔会出现 JVM 中的错误。

On balance, though, I expect that if you find your application needs to poke the GC often, something is broken and, these days, it's most likely the application.不过,总的来说,我希望如果您发现您的应用程序需要频繁地调用 GC,那么某些东西就会出问题,而现在,最有可能是应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM