树形图内存使用情况

发布于 2024-08-13 16:14:31 字数 266 浏览 2 评论 0原文

如何计算 Java TreeMap 处理每个映射需要多少内存?

我正在用 128 个线程进行实验,每个线程将 2^17 个长整型转储到自己的数组中。 然后,所有这些 2^24 long 都被映射到 int(TreeMap),每个数组引用在移动到下一个之前都被清空。

键+值的大小应为 128+64 MB。我很惊讶在分配给该虚拟机的 512MB 映射过程中出现 OutOfMemoryError 错误。

How can one calculate how much memory a Java TreeMap needs to handle each mapping?

I am doing an experiment with 128 threads, each dumping 2^17 longs in its own array.
All these 2^24 longs are then mapped to ints (TreeMap<Long,Integer>), each array reference is nulled before moving to the next.

That should amount to 128+64 MB for the keys+values. I am surprised to get OutOfMemoryError during the mapping with 512MB assigned to this VM.

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

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

发布评论

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

评论(3

秋意浓 2024-08-20 16:14:31

您似乎假设映射中的长整型键/值对仅占用 12 字节的内存。那是错误的。

即使您从原始长数组进行复制,当您将它们用作映射键和值时,自动装箱也会自动创建 Long 和 Integer 对象实例作为原始值的包装器。对象实例的内存要求是特定于 VM 实现的,但我认为 Sun 的 VM 对于这些对象来说位于 32-48 字节的范围内,64 位 VM 中的实例稍大一些。此外,映射需要每个键/值对的附加对象实例来管理内部数据结构。

You seem to assume that a Long, Integer key/value pair in a map occupies only 12 bytes of memory. That is wrong.

Even if you copy from a primitive long array, autoboxing will automatically create Long and Integer object instances as wrappers for the primitive values when you use them as map keys and values. The memory requirements for the object instances is VM implementation specific, but I think that Sun's VM lies in the range 32-48 bytes for these objects, with instances in a 64 bit VM being slightly larger. In addition, the map need additional object instances for each key/value pair to manage the internal data structures.

撩心不撩汉 2024-08-20 16:14:31

由于 8 字节对象开销和 8 字节对齐,每个 Long 至少为 16 个字节,每个 Integer 至少为 12 16 个字节。在 32 位机器上,每个节点至少为 24 32 字节(对象头、键、值、两个子节点和用于平衡的标志)。这意味着至少 2^24 * (12+24+16) = 832MB

编辑: 对象似乎是 8 字节对齐的,因此将 Integer 调整为 16 字节。另外,树节点可能还有另一个用于平衡树的字段,因此计算 32 个字节。这使我们达到最小 1024MB

Each Long is at least 16 bytes, and each Integer is at least 12 16 bytes, due to the 8-byte object overhead and 8-byte alignment. On a 32-bit machine, each node is at least 24 32 bytes (object header, key, value, two children, and flags for balancing). That means at least 2^24 * (12+24+16) = 832MB.

Edit: It appears objects are 8-byte aligned, so bump the Integer to 16 bytes. Also, a tree node probably has another field for balancing the tree, so count 32 bytes for it. That brings us to a minimum of 1024MB.

热血少△年 2024-08-20 16:14:31

显然从这个 Tree Map 4 Docs 默认大小树的大小是64M。如果超过这个值,它将报告 OutOfMemoryError。如果您没有指定最大尺寸,它将默认为该尺寸。

希望有帮助
鲍勃

编辑:忽略这一点。都是错的。

Apperently from this Tree Map 4 Docs the default size of the tree is 64M. It will report an OutOfMemoryError if you exceeed that. If you don't specify a maximum size it will default to that.

Hope that helps
Bob

EDIT: Ignore this. Its all wrong.

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