多少内存分配给元素对象[] [英] How much memory allocates to element in Object[]
问题描述
考虑以下情况
INT [] anArray = INT新[10];
分配内存上heap.Right每个 32位的10个元素?
因此,什么是如果元素类型是对象的元件的尺寸。 ??
像
对象[] = objArray新的对象[10];
有多少内存堆分配现在?我只是ssing 的ArrayList
。
私人瞬时对象[] elementData中;
刚试过这条线在我的机器
列表<串GT; S =新的ArrayList<串GT;(Integer.MAX_VALUE的);
结果
异常线程mainjava.lang.OutOfMemoryError:要求数组大小超过VM限制
在java.util.ArrayList中<&初始化GT;(未知来源)
在com.nextenders.server.guice.actions.servlets.Test.main(Test.java:13)
所以我想知道多少内存分配。
因此,什么是如果元素类型是对象的元件的尺寸。 ??
块引用>大小的
对象[]
数组元素是一个参考的大小。
在一个32位JVM,一提的是32位(4字节)
在64位JVM,一提的是64位(8字节),或32位如果COM pressed接力已启用优化和堆大小小于32GB。
所以你的数组分配将分配大约含有4×10或8×10个字节...加上约12字节对象头的开销。一个堆对象
刚试过这条线在我的机器
块引用>列表<串GT; S =新的ArrayList<串GT;(Integer.MAX_VALUE的);
在幕后,你试图分配试图分配2 33 2 34 字节之间包含数组。随着32位JVM,这是保证到不行。对于64位JVM,你至少需要8G的堆(COM pressed OOP)或16GB的为工作。
Consider the below case
int[] anArray = new int[10];
Allocates memory for 10 elements of each 32-bit on heap.Right ?
So what is the size of the element if the element type is Object. ??
like
Object[] objArray = new Object[10];
How much memory allocated now on heap ? I just got the doubt by ssing the source code of
ArrayList
.private transient Object[] elementData;
Just tried this line in my machine
List<String> s = new ArrayList<String>(Integer.MAX_VALUE);
results
Exception in thread "main" java.lang.OutOfMemoryError: Requested array size exceeds VM limit at java.util.ArrayList.<init>(Unknown Source) at com.nextenders.server.guice.actions.servlets.Test.main(Test.java:13)
So I'm trying to know how much memory allocated.
解决方案So what is the size of the element if the element type is Object. ??
The size of an
Object[]
array element is the size of a reference.
On a 32 bit JVM, a reference is 32 bits (4 bytes)
On a 64 bit JVM, a reference is 64 bits (8 bytes), or 32 bits if the "compressed oop" optimization is enabled and the heap size is less than 32Gb.
So your array allocation will allocate a heap object containing roughly 4 x 10 or 8 x 10 bytes ... plus about 12 bytes of object header overhead.
Just tried this line in my machine
List<String> s = new ArrayList<String>(Integer.MAX_VALUE);
Under the covers, you are attempting to allocate attempting to allocate an array containing between 233 and 234 bytes. With a 32bit JVM, that is guaranteed to not work. With a 64 bit JVM, you'd need a heap of at least 8Gb (compressed oop) or 16Gb for that to work.
这篇关于多少内存分配给元素对象[]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!