调整 Java 中的垃圾收集参数

发布于 2024-12-01 14:54:16 字数 283 浏览 0 评论 0原文

我有一个服务器java组件,它在启动时有巨大的内存需求,但会逐渐减弱。例如,在启动时,内存需求可能会达到 4g;最初的激增结束后将降至 2g。我已将组件配置为以 5g 内存启动,组件启动良好;使用的内存飙升至 4g,然后下降至接近 2g。此时堆消耗的内存仍然徘徊在 4g 左右,我想将其降低(本质上将可用内存保持在几百 mb,而不是 2g。我尝试使用 MinFreeHeapRatio 和 MaxFreeHeapRatio,将它们从默认值降低但这会导致在初始峰值期间初始运行后不会触发垃圾收集,并且使用的内存保持在高于平常的水平任何指针都会有很大帮助。

I have a server java component which has a huge memory demand at startup which mellows down gradually. So as an example at startup the memory requirement might shoot unto 4g; which after the initial surge is over will go down to 2g. I have configured the component to start with memory of 5g and the component starts well; the used memory surges upto 4g and then comes down close to 2g. The memory consumed by the heap at this point still hovers around 4g and I want to bring this down (essentially keep the free memory down to few hundred mb's rather than 2g. I tried using the MinFreeHeapRatio and MaxFreeHeapRatio by lowering them down from the default values but this resulted in garbage collection not being triggered after the initial run during the initial spike and the used memory stayed at a higher than usual level. Any pointers would greatly help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

舟遥客 2024-12-08 14:54:16

首先,我问为什么您担心释放服务器上的 2 GB 内存? 2GB 内存大约 100 美元或更少。如果这是在桌面上,我想我可以理解这种担忧。

如果您确实有充分的理由考虑它,这可能与您正在使用的垃圾收集算法有关。有些算法会将未使用的内存释放回操作系统,有些则不会。有一些与此相关的图表等,位于 http://www.stefankrause.net/wp/ ?p=14 。您可能想尝试 G1 收集器,因为它似乎可以轻松地将内存释放回操作系统。

根据评论进行编辑
如果他们都选择同时最大化负载怎么办?您是否同意其中一些将内存分页到磁盘并减慢服务器速度?我会在具有足够内存的服务器上运行它们,以在最大堆上运行所有应用程序,另外还有 4-6GB 用于操作系统/缓存。 32 或 64 GB 的服务器非常常见,而且您还可以获得更多。

First, I ask why you are worried about freeing up 2 GB of ram on a server? 2GB of ram is about $100 or less. If this is on a desktop I guess I can understand the worry.

If you really do have a good reason to think about it, this may be tied to the garbage collection algorithm you are using. Some algorithms will release unused memory back to the OS, some will not. There are some graphs and such related to this at http://www.stefankrause.net/wp/?p=14 . You might want to try the G1 collector, as it seems to release memory back to the OS easily.

Edit from comments
What if they all choose to max their load at once? Are you ok with some of them paging memory to disk and slowing the server to a crawl? I would run them on a server with enough memory to run ALL applications at max heap, + another 4-6GB for the OS / caching. Servers with 32 or 64 GB are pretty common, and you can get more.

孤者何惧 2024-12-08 14:54:16

你必须记住,JVM 在启动时保留虚拟内存,并且永远不会将其返还给操作系统(直到程序退出)。你最希望的是未使用的内存被换出(这不是一个好计划)如果你不这样做如果不希望应用程序使用超过一定数量的内存,则必须将其限制为该数量。

如果您有许多使用内存峰值的组件,则应考虑重写它们以在启动时使用更少的内存,或者在同一 JVM 中使用多个组件,这样额外的内存就不那么重要了。

You have to remember that the JVM reserves virtual memory on startup and never gives it back to the OS (until the program exits) The most you can hope for is that unused memory is swapped out (this is not a good plan) If you don't want the application to use more than certain amount of memory, you have to restrict it to that amount.

If you have a number of components which use a spike of memory, you should consider re-writing them to use less memory on startup or use multiple components in the same JVM so the extra memory is less significant.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文