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

huangapple go评论68阅读模式
英文:

How many times can the Garbage Collector be called?

问题

Sure, here's the translation:

我们能在一个单独的Java程序中多次调用垃圾回收器吗?

英文:

Can we call the Garbage Collector more than once in a single Java program?

答案1

得分: 2

如果你所说的“调用垃圾回收器”是指“调用System.gc()”,你可以随意频繁地这样做。它可能会产生一些影响,也可能没有任何影响。根据情况,它可能会使你的应用程序表现更好,也可能会使情况变得更糟。

现代的Java实现被设计为具有完全透明的垃圾回收(GC)。根据我的经验,尽管很少有帮助去主动触发GC的动作,但几乎总是,调用gc()确实会引起JVM的某种响应,它通常不会被完全忽视。

我曾经看到过一些应用程序反复调用gc()来克服应用程序中的错误(通常与资源管理有关);甚至偶尔还会出现JVM中的错误。

总的来说,我预计如果你发现你的应用程序经常需要触发GC,那么可能出现了一些问题,而在当今这些问题很可能出现在应用程序本身。

英文:

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.

Modern Java implementations are designed to run with completely transparent 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.

I've seen applications that use repeated calls to gc() to overcome bugs in the application (usually related to resource management); or even, very occasionally, bugs in the 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.

huangapple
  • 本文由 发表于 2020年9月30日 15:24:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/64132762.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定