在Java中分配数组与分配链表时要使用多少内存? [英] How much memory is used when allocating an array vs allocating an linked list in Java?
问题描述
我的猜测是每个存储在数组中的值的32位/64位字(取决于CPU).因此它将是数组大小X 32位/64位.
My guess is its a 32-bit/64-bit word (depending on the CPU) per value stored in the array. So it would be array size X 32-bit/64-bit.
对于链表,存储指向下一个元素的引用将是链接列表的两倍.因此它将是2 *数组大小X 32位/64位.
In the case of linked lists, it would be twice this to store the reference which points to the next element. So it would be 2 * array size X 32-bit/64-bit.
这是正确的,我错过了什么吗?
Is this correct, am I missing anything?
推荐答案
更多.链表中的每个元素都有:
Much more. Each element in a linked list has:
- 指向下一个元素的指针,指向上一个元素的指针,指向项目值的指针(12个字节)+对象开销(大约另外12个字节).说24个字节.
- 每个元素都不是原始元素,而是一个包装器.如果每个元素都不相同,它将占用空间.对于整数,请说另外16个字节.
总计:每个元素40个字节.
Total: 40 bytes per element.
别以为是面子,这只是给你一个主意.如果您想要精确的数字,请启动内存分析工具(例如Eclipse MAT).
Don't take this at face value, it's just to give you an idea. If you want precise numbers fire up a memory analysis tool (e.g. Eclipse MAT).
这篇关于在Java中分配数组与分配链表时要使用多少内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!