Java内存碎片和大数组的分配

发布于 2024-12-10 11:05:46 字数 332 浏览 0 评论 0原文

我正在考虑将 Timer 类用于大量 TimerTasks(比如说一两百万个)。 Timer 的堆实现是基于数组的,由于无法为数组保留空间,所以问题来了,当内存碎片时,Java 如何处理大的连续内存块(本例中为数组)的分配?

如果没有足够大的可用片段,是否总是会抛出 OutOfMemoryError?或者当可用内存总量足够时,我是否可以期望 JVM 具有某种内存碎片整理策略?

编辑: 看来我的问题没有独立于 JVM 实现的答案。在我的例子中,JVM 实现是 OpenJDK 版本 14.0-b16(根据 java.vm.name 和 java.vm.version 系统属性)。

I was considering using the Timer class for a large number of TimerTasks (say one or two million). The heap implementation for Timer is array based, and since there is no way to reserve space for the array, the question arises, how does Java handle allocation of large contiguous memory blocks (for an array in this case) when memory is fragmented?

Will an OutOfMemoryError always be thrown if there is no fragment available big enough? Or can I expect the JVM to have some kind of memory de-fragmentation strategy when the total amount of free memory is enough?

Edit: It appears there is no JVM implementation independent answer to my question. In my case the JVM implementation is OpenJDK version 14.0-b16 (according to the java.vm.name and java.vm.version system properties).

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

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

发布评论

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

评论(1

べ繥欢鉨o。 2024-12-17 11:05:46

首先,这不是由 Java 语言规范定义的,因此您得到的任何具体答案都将取决于实现。我什至不相信它是在虚拟机规范中定义的。

事实上,我建议您将问题范围缩小到特定的 JVM 实现

如果没有足够大的可用片段,是否总会抛出 OutOfMemoryError?或者当可用内存总量足够时,我是否可以期望 JVM 具有某种内存碎片整理策略?

由于 Java 隐藏了它可以执行的实际内存地址 内存压缩随意。所以是的,您可以期望它具有内存碎片整理策略。 (请记住,您的操作系统可能为 JVM 提供虚拟内存,因此您的应用程序和物理地址之间还有另一层。)

First of all, this is not defined by the Java Language Specification, so any concrete answers you'll get will be implementation dependent. I don't even believe it is defined in the Virtual Machine Specification.

In fact, I suggest you narrow down your question to a specific JVM implementation.

Will an OutOfMemoryError always be thrown if there is no fragment available big enough? Or can I expect the JVM to have some kind of memory de-fragmentation strategy when the total amount of free memory is enough?

Since Java hides the actual memory addresses it can perform memory compaction at will. So yes, you can expect it to have memory defragmentation strategies. (Keep in mind though that your operating system probably provides the JVM with virtual memory, so you have yet another layer between your application and the physical addresses.)

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