什么是Java对象数组的大小memoy它已被创建之后? [英] What is the memoy size of a Java object array after it has been created?

查看:109
本文介绍了什么是Java对象数组的大小memoy它已被创建之后?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能甚至不需要问,但我要确保我就在这。当您在Java中创建的任何对象的数组,像这样:

This probably doesn't even need asking, but I want to make sure I'm right on this. When you create an array of any object in Java like so:

Object[] objArr = new Object[10];

变量 objArr 位于堆栈存储器,并指出在数组对象位于堆的位置。在堆该数组的大小等于一个12字节对象头+ 4(或8,取决于基准尺寸)字节*数组中的条目数。这是准确的?

The variable objArr is located in stack memory, and it points to a location in the heap where the array object is located. The size of that array in the heap is equal to a 12 byte object header + 4 (or 8, depending on the reference size) bytes * the number of entries in the array. Is this accurate?

我的问题,那么,如下。由于上述数组是空的,它占用12 + 4 * 10 = 52个字节的内存堆中该行code的执行后立即?抑或是JVM的等待,直到你开始将东西放入数组它实例之前?不要在数组中的空引用占用空间?

My question, then, is as follows. Since the array above is empty, does it take up 12 + 4*10 = 52 bytes of memory in the heap immediately after the execution of that line of code? Or does the JVM wait until you start putting things into the array before it instantiates it? Do the null references in the array take up space?

推荐答案

的空引用做占用空间 - 数组的内存分配前期的一大块,并清零(使所有的内容空参考文献)。作为练习,尝试分配一个巨大的数组,一个会占用比你的JVM的内存限制更多的空间。该计划应立即与内存不足的错误终止。

The null references do "take up space" -- the array's memory is allocated up-front in one chunk, and zeroed (to make all of the contents null references). As an exercise, try allocating a huge array, one that will take up more space than your JVM's memory limit. The program should immediately terminate with an out of memory error.

这篇关于什么是Java对象数组的大小memoy它已被创建之后?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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