Hashtable使用了多少内存? [英] How much memory does a Hashtable use?

查看:678
本文介绍了Hashtable使用了多少内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中,如果我创建一个 Hashtable< K,V> 并在其中放入N个元素,它会占用多少内存?如果它依赖于实现,那么什么是一个好的猜测?

In Java, if I create a Hashtable<K, V> and put N elements in it, how much memory will it occupy? If it's implementation dependent, what would be a good "guess"?

推荐答案

编辑; 哦,天哪,我是个白痴,我为HashMap提供了信息,而不是HashTable。但是,在检查之后,实现对于内存来说是相同的。

这取决于VM的内部存储器设置(项目的打包,32位或64位)位指针和字对齐/大小)并且未由java指定。

This is dependent on your VM's internal memory setup (packing of items, 32 bit or 64 bit pointers, and word alignment/size) and is not specified by java.

可以找到估算内存使用的基本信息这里

Basic info on estimating memory use can be found here.

您可以这样估算:


  • 在32位虚拟机上,一个指针是4个字节,在64位虚拟机上,它是8个字节。

  • 对象开销是8个字节的内存(对于一个空对象,什么都不包含)

  • 将对象填充为8字节(ugh)的倍数。

  • 有一个小的,不变的开销每个hashmap:一个float,3个int,加上对象开销。

  • 有一个插槽数组,其中一些将有条目,其中一些将保留给新的。填充槽与总槽的比率不超过构造函数中指定的载荷因子。

  • 插槽数组需要一个对象开销,加上一个用于大小的int,以及每个插槽的一个指针,以指示存储的对象。

  • 插槽数通常是存储映射数的1.3到2倍,默认负载系数为0.75,但可能小于此值,具体取决于散列冲突。

  • 每个存储的映射需要一个入口对象。这需要一个对象开销,3个指针,加上存储的键和值对象,加上一个整数。

  • On 32-bit VMs, a pointer is 4 bytes, on 64-bit VMs, it is 8 bytes.
  • Object overhead is 8 bytes of memory (for an empty object, containing nothing)
  • Objects are padded to a size that is a multiple of 8 bytes (ugh).
  • There is a small, constant overhead for each hashmap: one float, 3 ints, plus object overhead.
  • There is an array of slots, some of which will have entries, some of which will be reserved for new ones. The ratio of filled slots to total slots is NO MORE THAN the specified load factor in the constructor.
  • The slot array requires one object overhead, plus one int for size, plus one pointer for every slot, to indicate the object stored.
  • The number of slots is generally 1.3 to 2 times more than the number of stored mappings, at default load factor of 0.75, but may be less than this, depending on hash collisions.
  • Every stored mapping requires an entry object. This requires one object overhead, 3 pointers, plus the stored key and value objects, plus an integer.

所以,放它在一起(对于32/64位的Sun HotSpot JVM):
HashMap需要24个字节(本身,原始字段)+ 12个字节(插槽数组常量)+每个插槽4个或8个字节+每个条目24/40个字节+关键对象大小+值对象大小+将每个对象填充为8个字节的倍数

OR,粗略(大多数默认设置,不保证确切地说):


  • 在32位JVM上:36字节+ 32字节/映射+键&值

  • 在64位JVM上:36字节+ 56字节/映射+键和&值

  • On 32-bit JVM: 36 bytes + 32 bytes/mapping + keys & values
  • On 64-bit JVM: 36 bytes + 56 bytes/mapping + keys & values

注意:这需要更多检查,64位VM上的对象开销可能需要12个字节。
我不确定空值 - 空值指针可能会以某种方式压缩。

Note: this needs more checking, it might need 12 bytes for object overhead on 64-bit VM. I'm not sure about nulls -- pointers for nulls may be compressed somehow.

这篇关于Hashtable使用了多少内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆