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

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

问题描述

回答有关如何在java中强制释放对象的大小缓存对象(这个人正在清除一个1.5GB的HashMap),其中 System.gc( ),我被告知手动调用 System.gc()是不好的做法,但这些注释并不完全令人信服。另外,似乎没有人敢鼓掌,也没有降低我的答案。



我被告知那是坏习惯,但后来我被告知垃圾收集器运行不要系统地停止这个世界,并且它也可以被JVM有效地用作提示,所以我有点茫然。



我理解JVM在需要回收内存时通常比你更清楚。我也明白,担心几千字节的数据是愚蠢的。我也明白,即使是兆字节的数据也不是几年前的情况。但是,仍然是1.5千兆字节?而且你知道存在1.5 GB的数据,它不像是在黑暗中拍摄的照片。是 System.gc()系统性很差,或者有没有一点可以让它变好?

问题实际上是双重的:


  • 为什么调用 System.gc()?它在某些实现中仅仅是JVM的一个暗示,还是总是一个完整的收集周期?真的有垃圾收集器的实现可以在不停止世界的情况下完成他们的工作吗?请阐明人们在评论中对我的答案所作的各种声明。

  • 阈值在哪里?调用 System.gc()是否永远不是一个好主意,还是有时它可以被接受?如果是这样,那是什么时候?


解决方案

c $ c> System.gc()是它是一个非常好的基本破解代码指示器。依赖于正确性的任何代码肯定会被破坏;任何依靠它来获得性能的东西都可能被破坏。

你不知道你正在运行什么类型的垃圾回收器。肯定有些人不会像你声称的那样阻止世界,但是有些JVM并不那么聪明或者出于各种原因(可能他们在用手机?)不这样做。你不知道它会做什么。



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



你不知道它会做什么,你不知道它是否会帮助你,而且你不需要调用它,这就是为什么人们如此强烈地说,你一般不应该这么称呼它。我认为这是一个如果你需要问你是否应该使用这个,你不应该的情况。




编辑来解决另一个线程的一些问题:



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

如同,JVM将保存内存(数十兆字节)并根据需要增加堆。即使释放Java对象,它也不一定会将该内存返回给系统;它可以完全自由地保留分配的内存以用于将来的Java分配。



为了表明可能是 System.gc() 什么都不做,查看:

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



特别是有一个 - XX:DisableExplicitGC 虚拟机选项。


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.

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.

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:

  • 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?

解决方案

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.

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.

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:

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.

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.

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

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

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

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

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