简体   繁体   English

关于垃圾收集。为什么我们需要调用System.gc();?

[英]regarding garbage collection.Why do we need to call System.gc();?

Garbage collection is called automatically when an object is refered to is no longer available to any variable. 当引用对象时,自动调用垃圾收集对任何变量都不再可用。 But I like know why do we call explicitly using System.gc() when garbage collection is called automatically.When do we call System.gc(); 但我想知道为什么我们在自动调用垃圾收集时使用System.gc()显式调用。我们什么时候调用System.gc();

You don't. 你没有。 As you say, garbage collection is automatic. 如你所说,垃圾收集是自动的。 System.gc() doesn't even force a garbage collection; System.gc()甚至不强制垃圾收集; it's simply a hint to the JVM that "now may be a good time to clean up a bit" 它只是对JVM的暗示“现在可能是清理一下的好时机”

In general, trying to force the garbage collector to do what you want with System.gc() is a hack applied by people who think they know better than they actually do, or as an (attempted) workaround for broken code. 一般来说,试图强制垃圾收集器使用System.gc()执行您想要的操作是由认为他们比实际知道更好的人应用的黑客,或者是破坏代码的(尝试)解决方法。

I've been writing Java for years and I've yet to see a situation where calling System.gc was really the right thing to do (in anything I've written) 我已经写Java多年了,我还没有看到调用System.gc真的是正确的事情(在我编写的任何内容中)

We don't. 我们没有。

We just don't. 我们只是没有。

Perhaps my experience is limited, but I have not once found it necessary to call System.gc(). 也许我的经验有限,但我没有发现有必要调用System.gc()。

I will quote Brian Goetz on the performance aspect (if you haven't heard of him, look him up -- and read the rest of this article, too): 我将引用Brian Goetz关于性能方面的内容(如果你还没有听说过他,那就看看他了 - 并阅读本文的其余内容):

A third category where developers often mistakenly think they are helping the garbage collector is the use of System.gc() , which triggers a garbage collection (actually, it merely suggests that this might be a good time for a garbage collection). 开发人员经常错误地认为他们正在帮助垃圾收集器的第三类是使用System.gc() ,它触发垃圾收集(实际上,它只是表明这可能是垃圾收集的好时机)。 Unfortunately, System.gc() triggers a full collection, which includes tracing all live objects in the heap and sweeping and compacting the old generation. 不幸的是, System.gc()触发了一个完整的集合,其中包括跟踪堆中的所有活动对象以及扫描和压缩旧代。 This can be a lot of work. 这可能是很多工作。

In general, it is better to let the system decide when it needs to collect the heap, and whether or not to do a full collection. 通常,最好让系统决定何时需要收集堆,以及是否进行完整收集。

You don't need it. 你不需要它。 Think of it as a diagnostic tool, like being able to write to a debug console. 可以将其视为诊断工具,例如能够写入调试控制台。

For example, imagine that if you were doing benchmarking, you would want to tell the GC to collect garbage after each benchmark run. 例如,假设您在进行基准测试时,您希望告诉GC在每次基准测试运行后收集垃圾。

You do need it. 确实需要它。 It is very useful no matter what these other people say. 无论这些人说什么,它都非常有用。

A usage example: Say that you have just finished a long background task that has used a lot of memory. 一个用法示例:假设您刚刚完成了一项耗费大量内存的长后台任务。 None of those objects are going to be used again. 这些对象都不会再次使用。 Since the task took a long time the user isn't going to care about another 5-10 seconds. 由于任务需要很长时间,因此用户不会再关心5-10秒。 This is a good time to garbage collect. 垃圾收集的好时机。

If you don't GC at that point, it is going to happen later. 如果你那时没有 GC,它会在以后发生。 Probably during interactive use of the program at which point the user experience gets choppy. 可能在交互式使用程序期间,用户体验会变得不稳定。

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

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