为什么大造Java数组消耗这么多的内存? [英] Why does creating a big Java array consume so much memory?

查看:545
本文介绍了为什么大造Java数组消耗这么多的内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么以下行

    Object[] objects = new Object[10000000];

导致了大量的内存(40M〜)正在使用的JVM?有没有办法知道虚拟机的内部工作分配数组时?

result in a lot of memory (~40M) being used by the JVM? Is there any way to know the internal workings of the VM when allocating arrays?

推荐答案

好吧,这为分配的参考千万足够的空间,以及为数组对象本身就是一个小的开销。

Well, that allocates enough space for 10000000 references, as well as a small amount of overhead for the array object itself.

实际的大小将取决于虚拟机 - 但它肯定不会奇怪,它占用的内存相当数量的......我期望的至少的40MB,并且很可能在64 80MB比特VM,除非它使用 COM pressed哎呀的数组。

The actual size will depend on the VM - but it's surely not surprising that it's taking up a fair amount of memory... I'd expect at least 40MB, and probably 80MB on a 64-bit VM, unless it's using compressed oops for arrays.

当然,如果你填充很多不同的对象数组,这将需要更多,更多的内存...但数组本身仍然需要空间只为引用。

Of course, if you populate the array with that many distinct objects, that will take much, much more memory... but the array itself still needs space just for the references.

这篇关于为什么大造Java数组消耗这么多的内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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