当不再需要时,JVM 是否会将空闲内存返还给操作系统?

发布于 2024-10-10 19:47:44 字数 183 浏览 8 评论 0原文

我有一个应用程序,某些进程暂时需要一定量的内存/堆。给 JVM 一个合理的最大堆大小值作为一个选项,JVM 会从一个小堆开始,并根据需要从操作系统请求更多内存。

我的问题是,当我的应用程序不再需要太多内存时,JVM 是否会将额外的内存返还给操作系统(例如,用于其他进程)。目前,我的应用程序似乎永远保留了该内存,即使不再需要时也是如此。

I have an application that temporarily needs some certain amount of memory/heap for some processes. Given a reasonable value of maximum heap size to the JVM as an option, the JVM starts with a little heap and requests more memory from the OS on demand.

My question is, if that additional memory will be given back by the JVM to the OS (e.g. for other processes) when there is no need for much memory anymore by my application. Currently my application seems to hold that memory forever, even when not needed anymore.

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

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

发布评论

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

评论(3

渔村楼浪 2024-10-17 19:47:44

JVM 确实会将内存返回给操作系统,但只是非常不情愿,因为它可能很快会再次需要它,并且从操作系统获取内存是一个相对昂贵的操作。

如果您希望 JVM 更快地将内存返回给操作系统,可以使用 调整 Oracle JVM 的参数,特别是 -XX:MaxHeapFreeRatio-XX:MinHeapFreeRatio

The JVM does return memory to the OS, but only very reluctantly, since it may need it again soon, and getting memory from the OS is a relatively expensive operation.

If you want the JVM to return memory to the OS more eagerly, you can use the tuning parameters of the Oracle JVM, specifically -XX:MaxHeapFreeRatio and -XX:MinHeapFreeRatio

浅语花开 2024-10-17 19:47:44

未必。这取决于您使用的 JVM。我知道一个案例,是 IBM 的 JVM。我们还遇到一个问题,即程序不释放内存,即使不需要它。可以在 IBM 网站

Not necessarily. It depends on JVM you use. I know of one case, with IBM's JVM. We also had an issue with a program not freeing memory, even when it's not needed. Some info can be found on IBM's site.

去了角落 2024-10-17 19:47:44

这实际上取决于版本以及所选的垃圾收集器。我发现 java 1.8.0_73 偶尔会默认释放少量数据回操作系统。这是我记得看到的最早的版本,无需调整 JVM 参数,但它很可能适用于早期版本,我不知道确切的版本。

除非调整参数,否则默认情况下它可能不会释放任何内容,除非 60-70% 的堆未使用。

这里可能存在一个性能考虑因素 - java 因速度慢而闻名,因此默认情况下,JVM 可能会尝试最小化操作系统的分配,并通过保留大量内存并减少执行频率来使垃圾收集尽可能高效。

我想知道如果操作系统剩余的可用内存相对较少,它是否会不那么吝啬内存?

It depends on the version indeed and on the garbage collectors selected. I found that java 1.8.0_73 does occasionally release small amounts back to the OS by default. This is the earliest version I remember seeing it without having to adjust JVM parameters, but it may very well apply to earlier ones, I don't know an exact version.

Unless parameters are adjusted, by default it likely won't release anything unless 60-70% of your heap is unused.

Herein may lie a performance consideration - java had a reputation for being slow, so by default the JVM may try to minimise allocation from the OS and making garbage collection as efficient as it can by holding on to a lot of memory and doing it less often.

I wonder if it might be less stingy with memory if the OS has relatively little free memory left..?

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