简体   繁体   English

如果我增加 memory 堆是否会增加 GC 时间

[英]If I increase memory heap is it increase GC time

My current application running on 2GB memory Normal GC cycle.我当前的应用程序在 2GB memory 正常 GC 周期上运行。

I want to increase JVM memory to 4GB to incrase application performance.我想将 JVM memory 增加到 4GB 以提高应用程序性能。

will it incraese GC time?它会增加GC时间吗? If yes how much performance will get affected.如果是的话,会有多少性能受到影响。 Is their any good artical to tune GC?他们有什么好的艺术来调整 GC 吗?

Short Answer简短的回答

Yes, the time will increase.是的,时间会增加。 Bigger Heap = Longer Pause Times更大的堆 = 更长的暂停时间

Long Answer长答案

There are a few factors to consider.有几个因素需要考虑。 How have you configured your GC to run?你是如何配置你的 GC 运行的? how big have you sized your young gen?你的年轻一代有多大? How often do you see full GC's?您多久看到一次完整的 GC?

If you rarely see full GC's then the difference will be negligible.如果您很少看到完整的 GC,那么差异将可以忽略不计。 If you log your GC activity to log you can see the pause times for a partial GC is very quick.如果您将 GC 活动记录到日志中,您可以看到部分 GC 的暂停时间非常快。 The difference between 2GB and 4GB on a partial GC will be around 0.1s.部分 GC 上 2GB 和 4GB 之间的差异将在 0.1s 左右。 To log the GC activity you can use the following parameters -XX:+PrintGCDetails -verbose:gc -Xloggc:/log/path/gc.log .要记录 GC 活动,您可以使用以下参数-XX:+PrintGCDetails -verbose:gc -Xloggc:/log/path/gc.log There are many tools out there which will read this GC log for you, provide graphs and stats such as throughput and total pause times.有许多工具可以为您读取此 GC 日志,提供图表和统计信息,例如吞吐量和总暂停时间。

If you are seeing frequent Full GC's and that is why you want to add more memory then you may want to consider profiling the application instead to see what is eating up all the memory.如果您看到频繁的 Full GC,这就是为什么要添加更多 memory,那么您可能需要考虑分析应用程序,而不是查看是什么占用了所有 memory。 With a bigger heap, these problems will only cause longer GC's until you fix the underlying problems.使用更大的堆,这些问题只会导致更长的 GC,直到您解决了潜在的问题。

In the end you need to try the various configurations, test properly and put into your production environment what will work best for you.最后,您需要尝试各种配置,正确测试并将最适合您的配置放入您的生产环境。

Large heaps == long GC pauses.大堆 == 长时间的 GC 暂停。

I have seen GC stopping for several minutes on ~10G size heaps.我已经看到 GC 在大约 10G 大小的堆上停止了几分钟

On the other hand, smaller heaps mean more frequent (yet shorter taking) GC cycles.另一方面,较小的堆意味着更频繁(但占用时间更短)的 GC 周期。 There is no good generic answer to this problem - it all depends on the needs of your app (high-freq trading app? kitten webserver?), hardware, object churn, etc.这个问题没有好的通用答案——这完全取决于您的应用程序(高频交易应用程序?小猫网络服务器?)、硬件、object 流失等的需求。

Couple of good resources to read on this subject:关于这个主题的几个很好的资源:

There is not straightforward answer because the answer depends on what your application does memory wise.没有直接的答案,因为答案取决于您的应用程序做什么 memory 明智。
However, you should take in consideration that if you increase the jvm memory then your application would have more memory... and if all that memory is used then your gc will be called more frequently (increase in GC time) However, you should take in consideration that if you increase the jvm memory then your application would have more memory... and if all that memory is used then your gc will be called more frequently (increase in GC time)
Increasing the memory is not always the best solution, optimize objects usage and recycling unused object might a better solution.增加 memory 并不总是最好的解决方案,优化对象使用并回收未使用的 object 可能是更好的解决方案。

More available memory will tend to decrease overall time spent in GC but increase the time that a full GC takes.更多可用的 memory 将倾向于减少花费在 GC 上的总时间,但会增加完整 GC 所需的时间。 However, modern JVMs don't necessarily use "stop the world" garbage collectors anymore.但是,现代 JVM 不一定再使用“停止世界”垃圾收集器了。

Tuning GC of course depends on what JVM you use.当然,调整 GC 取决于您使用的 JVM。 The Oracle JVM has a huge amount of GC tuning options and a pretty extensive tuning guide . Oracle JVM 有大量的 GC 调整选项和相当广泛的调整指南

If you want to learn how to tune your JVM you can start by reading http://java.sun.com/performance/reference/whitepapers/tuning.html and http://www.oracle.com/technetwork/java/gc-tuning-5-138395.html If you want to learn how to tune your JVM you can start by reading http://java.sun.com/performance/reference/whitepapers/tuning.html and http://www.oracle.com/technetwork/java/gc -tuning-5-138395.html

Regarding your question there is no simple answer but increasing the heap will both delay the full garbage collection but will also make it last longer.关于您的问题,没有简单的答案,但增加堆会延迟完整的垃圾收集,但也会使其持续更长时间。

Performance tuning is a bit of a black art and once you start changing your GC settings it becomes something you need to constantly monitor because in my opinion you are telling the VM that you know better.性能调优有点像魔法,一旦你开始改变你的 GC 设置,它就变成了你需要不断监控的东西,因为在我看来,你是在告诉 VM 你知道得更清楚。

There are some VM flags you can add to log the GC pauses and heap sizes which are very useful, you can grep your logs and get some good stats.您可以添加一些 VM 标志来记录 GC 暂停和堆大小,这些非常有用,您可以 grep 您的日志并获得一些好的统计信息。

verbose:gc -XX:+PrintGCDetails XX:+PrintGCTimeStamps –Xloggc:PATH_FROM_ROOT/gclog.log详细:gc -XX:+PrintGCDetails XX:+PrintGCTimeStamps –Xloggc:PATH_FROM_ROOT/gclog.log

Use JVisualVM, Jconsole or a good profiler like JProfiler to monitor.使用 JVisualVM、Jconsole 或像 JProfiler 这样的好的分析器来监控。 A good place to start to read about GC is开始阅读 GC 的一个好地方是

Tuning Garbage Collection 调整垃圾收集

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

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