为什么调用 System.gc() 是不好的做法? [英] Why is it bad practice to call System.gc()?

查看:29
本文介绍了为什么调用 System.gc() 是不好的做法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

回答之后关于如何Java 中的强制释放对象(这家伙正在清除 1.5GB HashMap)与 System.gc(),有人告诉我手动调用 System.gc() 是不好的做法,但评论并不完全令人信服.此外,似乎没有人敢于对我的回答投赞成票或反对票.

After answering a question about how to force-free objects in Java (the guy was clearing a 1.5GB HashMap) with System.gc(), I was told it's bad practice to call System.gc() manually, but the comments were not entirely convincing. In addition, no one seemed to dare to upvote, nor downvote my answer.

有人告诉我这是不好的做法,但后来我也被告知垃圾收集器运行不再系统地停止世界,并且它也可以被 JVM 有效地用作提示,所以我'我有点不知所措.

I was told there that it's bad practice, but then I was also told that garbage collector runs don't systematically stop the world anymore, and that it could also effectively be used by the JVM only as a hint, so I'm kind of at loss.

我确实理解 JVM 在需要回收内存时通常比您更了解.我也明白担心几千字节的数据是愚蠢的.我也明白,即使是兆字节的数据也不是几年前的样子.但仍然是 1.5 GB?而且你知道内存中大约有 1.5 GB 的数据;这不像是在黑暗中拍摄.System.gc() 是系统性的坏事,还是在某个点上变得可以了?

I do understand that the JVM usually knows better than you when it needs to reclaim memory. I also understand that worrying about a few kilobytes of data is silly. I also understand that even megabytes of data isn't what it was a few years back. But still, 1.5 gigabytes? And you know there's like 1.5 GB of data hanging around in memory; it's not like it's a shot in the dark. Is System.gc() systematically bad, or is there some point at which it becomes okay?

所以问题实际上是双重的:

So the question is actually double:

  • 为什么调用 System.gc() 是不好的做法?它真的只是在某些实现下对 JVM 的一个提示,还是总是一个完整的收集周期?真的有垃圾收集器实现可以在不停止世界的情况下完成它们的工作吗?请阐明人们在对我的答案的评论中做出的各种断言.
  • 门槛在哪里?调用 System.gc() 是否永远是一个好主意,或者有时它是可以接受的?如果是这样,那是什么时候?
  • Why is or isn't it bad practice to call System.gc()? Is it really merely a hint to the JVM under certain implementations, or is it always a full collection cycle? Are there really garbage collector implementations that can do their work without stopping the world? Please shed some light over the various assertions people have made in the comments to my answer.
  • Where's the threshold? Is it never a good idea to call System.gc(), or are there times when it's acceptable? If so, what are those times?

推荐答案

大家总是说要避免 System.gc() 的原因是它是一个从根本上被破坏的很好的指标代码.任何依赖于它的正确性的代码肯定是坏的;任何依赖它来提高性能的东西很可能都坏了.

The reason everyone always says to avoid System.gc() is that it is a pretty good indicator of fundamentally broken code. Any code that depends on it for correctness is certainly broken; any that rely on it for performance are most likely broken.

你不知道你在运行什么样的垃圾收集器.当然,有些 JVM 并没有像您断言的那样停止世界",但是有些 JVM 并不那么聪明,或者出于各种原因(也许它们在手机上?)没有这样做.你不知道它会做什么.

You don't know what sort of garbage collector you are running under. There are certainly some that do not "stop the world" as you assert, but some JVMs aren't that smart or for various reasons (perhaps they are on a phone?) don't do it. You don't know what it's going to do.

另外,它不能保证做任何事情.JVM 可能会完全忽略您的请求.

Also, it's not guaranteed to do anything. The JVM may just entirely ignore your request.

你不知道它会做什么"、你甚至不知道它是否有帮助"和无论如何你都不应该打电话给它"的组合是人们如此强硬的原因说一般你不应该打电话给它.我认为这是如果你需要问你是否应该使用它,你不应该"的情况

The combination of "you don't know what it will do," "you don't know if it will even help," and "you shouldn't need to call it anyway" are why people are so forceful in saying that generally you shouldn't call it. I think it's a case of "if you need to ask whether you should be using this, you shouldn't"

编辑以解决其他线程中的一些问题:

EDIT to address a few concerns from the other thread:

阅读您链接的主题后,我还想指出几点.首先,有人建议调用 gc() 可能会将内存返回给系统.这当然不一定是真的 - Java 堆本身的增长独立于 Java 分配.

After reading the thread you linked, there's a few more things I'd like to point out. First, someone suggested that calling gc() may return memory to the system. That's certainly not necessarily true - the Java heap itself grows independently of Java allocations.

与此类似,JVM 将持有内存(数十兆字节)并根据需要增加堆.即使您释放 Java 对象,它也不一定会将内存返回给系统;保留分配的内存以用于将来的 Java 分配是完全自由的.

As in, the JVM will hold memory (many tens of megabytes) and grow the heap as necessary. It doesn't necessarily return that memory to the system even when you free Java objects; it is perfectly free to hold on to the allocated memory to use for future Java allocations.

要证明 System.gc() 可能什么都不做,请查看:

To show that it's possible that System.gc() does nothing, view:

http://bugs.sun.com/view_bug.do?bug_id=6668279

特别是有一个 -XX:DisableExplicitGC VM 选项.

and in particular that there's a -XX:DisableExplicitGC VM option.

这篇关于为什么调用 System.gc() 是不好的做法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆