java Hashtable 使用多少内存?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1425221/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 16:31:04  来源:igfitidea点击:

How much memory does a Hashtable use?

javahashtablememory-management

提问by Vinicius Pinto

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"?

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

回答by BobMcGee

Edit; Oh geez, I'm an idiot, I gave info for HashMap, not HashTable. However, after checking, the implementations are identical for memory purposes.

编辑; 哦,天哪,我是个白痴,我提供了 HashMap 的信息,而不是 HashTable。但是,经过检查,实现对于内存目的是相同的。

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.

这取决于您的 VM 的内部内存设置(项目的打包、32 位或 64 位指针以及字对齐/大小)并且不是由 java.lang.

Basic info on estimating memory use can be found here.

可以在此处找到有关估计内存使用的基本信息。

You can estimate it like so:

你可以这样估计:

  • 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 位 VM 上,一个指针是 4 个字节,在 64 位 VM 上,它是 8 个字节。
  • 对象开销是 8 字节的内存(对于空对象,不包含任何内容)
  • 对象被填充为 8 字节的倍数(呃)。
  • 每个 hashmap 都有一个小的、恒定的开销:一个浮点数、3 个整数,加上对象开销。
  • 有一组插槽,其中一些将具有条目,其中一些将保留给新的。填充插槽与总插槽的比率不超过构造函数中指定的负载因子。
  • 槽数组需要一个对象开销,加上一个表示大小的整数,加上每个槽的一个指针,以指示存储的对象。
  • 时隙数通常是存储映射数的 1.3 到 2 倍,默认负载因子为 0.75,但可能少于此值,具体取决于哈希冲突。
  • 每个存储的映射都需要一个条目对象。这需要一个对象开销,3 个指针,加上存储的键和值对象,再加上一个整数。

So, putting it together (for 32/64 bit Sun HotSpot JVM): HashMap needs 24 bytes (itself, primtive fields) + 12 bytes (slot array constant) + 4 or 8 bytes per slot + 24/40 bytes per entry + key object size + value object size + padding each object to multiple of 8 bytes

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

OR, roughly (at most default settings, not guaranteed to be precise):

或者,大致(最多默认设置,不保证准确):

  • On 32-bit JVM: 36 bytes + 32 bytes/mapping + keys & values
  • On 64-bit JVM: 36 bytes + 56 bytes/mapping + keys & values
  • 在 32 位 JVM 上:36 字节 + 32 字节/映射 + 键和值
  • 在 64 位 JVM 上:36 字节 + 56 字节/映射 + 键和值

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.

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

回答by Boune

It is hard to estimate. I would read this first: http://www.codeinstructions.com/2008/12/java-objects-memory-structure.html

很难估计。我会先读这个:http: //www.codeinstructions.com/2008/12/java-objects-memory-structure.html

Just use the sunjdk tools to figure out the size of K, V and

只需使用 sunjdk 工具计算出 K、V 和

jmap -histo [pid]

jmap -histo [pid]

num #instances #bytes class name

num #instances #bytes 类名

1: 126170 19671768 MyKClass

1:126170 19671768 MyKClass

2: 126170 14392544 MyVClass

2:126170 14392544

3: 1 200000 MyHashtable

3: 1 200000 MyHashtable

Also you may want to use HashMap instead of Hashtable if you do not need synchronization.

此外,如果您不需要同步,您可能希望使用 HashMap 而不是 Hashtable。