为什么java要等待这么长时间才能运行垃圾收集器? [英] Why does java wait so long to run the garbage collector?

查看:24
本文介绍了为什么java要等待这么长时间才能运行垃圾收集器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个 Java 网络应用程序,使用 ,但不幸的是您需要一个 playapps 帐户才能查看它.

我收到反馈后会在这里报告;非常感谢大家的回答,我已经从他们那里学到了很多东西!

分辨率
Playapps 支持仍然很棒,但对我没有太多建议,他们唯一的想法是,如果我广泛使用缓存,这可能会使对象的存活时间比需要的时间长,但事实并非如此.我仍然学到了很多东西(呜呜!),我给了@Ryan Amos 绿色支票,因为我接受了他每半天调用一次 System.gc() 的建议,现在工作正常.

解决方案

Java 不会运行垃圾清理器,除非它必须运行,因为垃圾清理器会减慢速度,不应该经常运行.我认为您可以更频繁地安排清洁,例如每 3 小时一次.如果应用程序从不消耗全部内存,就没有理由运行垃圾清理器,这就是为什么 Java 只在内存非常高时才运行它.

所以基本上,不要担心别人怎么说:做最有效的.如果您发现在 66% 内存下运行垃圾清理器后性能有所提高,请执行此操作.

I am building a Java web app, using the Play! Framework. I'm hosting it on playapps.net. I have been puzzling for a while over the provided graphs of memory consumption. Here is a sample:

The graph comes from a period of consistent but nominal activity. I did nothing to trigger the falloff in memory, so I presume this occurred because the garbage collector ran as it has almost reached its allowable memory consumption.

My questions:

  • Is it fair for me to assume that my application does not have a memory leak, as it appears that all the memory is correctly reclaimed by the garbage collector when it does run?
  • (from the title) Why is java waiting until the last possible second to run the garbage collector? I am seeing significant performance degradation as the memory consumption grows to the top fourth of the graph.
  • If my assertions above are correct, then how can I go about fixing this issue? The other posts I have read on SO seem opposed to calls to System.gc(), ranging from neutral ("it's only a request to run GC, so the JVM may just ignore you") to outright opposed ("code that relies on System.gc() is fundamentally broken"). Or am I off base here, and I should be looking for defects in my own code that is causing this behavior and intermittent performance loss?

UPDATE
I have opened a discussion on PlayApps.net pointing to this question and mentioning some of the points here; specifically @Affe's comment regarding the settings for a full GC being set very conservatively, and @G_H's comment about settings for the initial and max heap size.

Here's a link to the discussion, though you unfortunately need a playapps account to view it.

I will report the feedback here when I get it; thanks so much everyone for your answers, I've already learned a great deal from them!

Resolution
Playapps support, which is still great, didn't have many suggestions for me, their only thought being that if I was using the cache extensively this may be keeping objects alive longer than need be, but that isn't the case. I still learned a ton (woo hoo!), and I gave @Ryan Amos the green check as I took his suggestion of calling System.gc() every half day, which for now is working fine.

解决方案

Java won't run the garbage cleaner until it has to, because the garbage cleaner slows things down quite a bit and shouldn't be run that frequently. I think you would be OK to schedule a cleaning more frequently, such as every 3 hours. If an application never consumes full memory, there should be no reason to ever run the garbage cleaner, which is why Java only runs it when the memory is very high.

So basically, don't worry about what others say: do what works best. If you find performance improvements from running the garbage cleaner at 66% memory, do it.

这篇关于为什么java要等待这么长时间才能运行垃圾收集器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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