Java 垃圾收集会停止所有 Java 进程
我们在一台 Linux 机器上运行服务器进程的多个实例。该盒子有 8 个核心和 16GB RAM。我使用 Java 1.6 使用 -Xincgc 选项启动每个进程。
我们在整个应用程序中安装了各种计时器,用于跟踪完成各种任务的时间。当垃圾收集发生时,我注意到盒子上的每个 java 进程都会打印出当时运行的任何任务都很慢。
它不会停滞很长时间,可能是 100-300 毫秒左右,但延迟是一个重要因素。它也不会持续停止,只是周期性地停止。
当垃圾收集发生时,它会阻止任何 java 进程获得时间吗?如果是这样,有什么办法解决这个问题吗?我应该使用不同的 GC 选项吗?
更新:
需要澄清的是,我并不担心 GC 发生时某个进程会停止。我可以针对这种情况调整设置或优化。我只是想知道为什么每个正在运行的 Java 进程似乎同时停止,而我认为它们或多或少是独立的。
We're running multiple instances of a server process on one linux box. The box has 8 cores and 16gb of RAM. I'm starting up each process with the -Xincgc option, using Java 1.6.
We have various timers instrumented throughout the application that track the time to complete various tasks. When garbage collection happens, I notice that every java process on the box prints out that whatever task it was running at the time was slow.
It's not stalling for a long time, maybe 100-300ms or so, but latency is a huge factor for this. It's also not stalling constantly, just periodically.
When garbage collection is happening does it stop any java process from getting any time? If so, is there any way around this? Should I be using different GC options?
UPDATE:
Just to be clear, I'm not worried about one process stalling while GC is happening. I can tweak settings or optimize for that case. I'm just wondering why EVERY running Java process seems to stall at the same time when I thought they were more or less independent.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Java v 1.6 应该很智能,可以确定哪种 GC 是最好的。
我建议阅读以下内容:Java 6 中的垃圾收集
摘要:尝试其中一个(但不能同时使用):
Java v 1.6 is supposed to be smart and work out which kind of GC is the best.
I'd recommend reading this: Garbage collection in Java 6
Summary: try either of these (but not both):
当您使用
-Xincgc
时,按照此 sun 文档并发低暂停收集器:如果
-Xincgc
™ 或-XX:+UseConcMarkSweepGC
在命令行上传递。并发收集器用于收集终身代,并在应用程序执行的同时执行大部分收集工作。 应用程序在收集期间会短暂暂停。您可能需要考虑其他替代方案,例如吞吐量收集器。上面的文档很好地描述了何时、哪种收集器有用。
when you use
-Xincgc
, as per this sun documentationThe concurrent low pause collector: this collector is used if the
-Xincgc
™ or-XX:+UseConcMarkSweepGC
is passed on the command line. The concurrent collector is used to collect the tenured generation and does most of the collection concurrently with the execution of the application. The application is paused for short periods during the collection.You may need to consider other alternatives like Throughput Collector. Above documentation has good description on when, which collector will be useful.
您遇到的问题是“完整垃圾收集”,也称为“Stop the world GC”。当这种情况发生时,你的java应用程序基本上停止工作。检查此线程以获取一些提示:
调整垃圾收集以实现低延迟
The problem you are encountering is a 'Full Garbage Collection', also known as a 'Stop the world GC'. When this occurs you java app basicly stops working. Check this thread for some pointers:
Tuning garbage collections for low latency
如果您使用 JConsole 这样的工具来查看内存活动,您会发现垃圾收集通常是一项重要的活动。您可能需要进行一些分析以查看是否还涉及其他瓶颈。此外,大多数垃圾收集算法都有相当多的可用配置选项。
最后,我记得我们不久前处理过的一个情况使我们发现将最小和最大内存设置更接近是有帮助的(至少在我们的情况下)。它导致更频繁的垃圾收集,但它们并不那么具有侵入性。
希望这有帮助。
If you use a tool like JConsole to view your memory activity you will see that garbage collection is generally a weighty activity. You may want to do some profiling to see if there are other bottlenecks involved. Also, most of the garbage collection algorithms have quite a few configuration options available to them.
Lastly, I remember a situation we dealt with quite a while ago led us to find that having your min and max memory settings closer together was helpful (at least in our case). It caused more frequent garbage collection but they were not as intrusive.
Hope this helps.