Java和精确的参考尺寸为对象,数组和基本类型 [英] Java and exact reference size for objects, array and primitive types

查看:111
本文介绍了Java和精确的参考尺寸为对象,数组和基本类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想确切地知道在内存中分配一个对象的真实空间。

我试着用一些例子来解释:使用64位JVM,指针大小应为8字节,因此:


  • 对象singletest =新的对象(); 将采取8个字节来引用对象以及对象结果
  • 的大小
  • 对象ArrayTest中=新的对象[10]; 将采取8个字节来引用其中数组存储的位置加8 * 10个字节来存储阵列加上大小每个对象的结果

  • INT singleint = INT新将只有2个字节,因为int是一种最原始的结果

  • INT [] = arrayint新INT [10]; 将采取8个字节来引用该元素的位置和10 * 2个字节结果

此外,这就是为什么Java允许写code这样的原因:结果

  INT [] [] =的doubleArray新INT [2] [];
INT [0] [] =新INT [5];
INT [1] [] =新INT [10];

什么是真正发生的是一个数组会产生像一个对象的引用(即指针),所以它其实并不重要在声明时第二个维度的大小(和尺寸可以是不同的,之间没有任何联系他们)。然后采取将空间:一个参考的的doubleArray 的(8字节),第一个维度是简单地第二个参考,让其他的8个字节* 2(第一个维度尺寸),最后2个字节* 5加2个字节* 10。

所以,最后,如果有一个真正的阶级是这样的:

 类测试{
   诠释A,B;
   INT木屐(){返回A};
   无效SETA(int类型的){this.a =一;}
   INT getB(){回报B};
   无效组B(INT B){this.b = B;}
}

当我把一个新的实例,一个指针(或命名为参考,因为它的Java)的8个字节将用于加2 + 2字节的整数存储到类中。

的问题是:我说得对还是我写的废话总额?此外,当我不实例化一个对象,但我只是把它声明,8个字节将被分配进一步使用或不?而如果我分配一个空值?

同时为原始类型,我相当肯定,刚刚宣布它将分配请求的空间(如果我宣布一个INT I那么我可以马上拨打我++,因为没有参考使用,只是内存部分设置好的上0)。

我搜索互联网上不巧妙应对......我知道,我写了很多问题,但任何帮助将AP preciated! (也许我不是唯一的一个兴趣)


解决方案

  使用64位JVM

,指针大小应为8个字节,


实际上其通常为32位,除非你有32 GB或更多的堆的最大堆大小。这是因为Java使用的引用,而不是指针(以及每个对象是一个8字节,而不是1边界)​​

在JVM可以根据哪个JVM使用,最大堆大小是改变一个参考的大小。


  

对象singletest =新的对象();将8个字节,以引用对象加在对象的大小


的对象将使用约16个字节堆。它可以或可以不使用4个字节堆栈,但它可以只使用一个寄存器。


  

对象ArrayTest中=新的对象[10];


这将使用大约16字节的头,加上10倍的参考尺寸(总共约56字节)


  

INT singleint = INT新;将只有2个字节,因为int是一种原始类型


INT 永远是32位的位,就不能创建一个原始。作为其名义上在栈上它可能会使用堆栈的第4字节或者它可能只使用一个寄存器。


  

INT [] = arrayint新INT [10];将8个字节,以引用的位置和10 * 2个字节用于元素


同样的对象很可能是大小为新对象相同的[10] (56字节)

  INT [] [] =的doubleArray新INT [2] [];
INT [0] [] =新INT [5];
INT [1] [] =新INT [10];

我不会把它的doubleArray,因为它可能与混淆双重[]

不过尺寸likkely成为的doubleArray 16 + 5 * 4 + 4(用于填充)和16 + 10 * 4 16 + 2 * 4。

在堆中分配的内存对齐到8字节边界。


  

我把一个新的实例,一个指针(或名称引用它,因为它的Java)的8个字节将用于加2 + 2字节的整数存储到类中。


基准是在栈上,通常这是不包括在内。

对象有大约16个字节的头和int类型占用2 * 4个字节。


  

当我不实例化一个对象,但我只是把它声明


Java并不让你申报的对象,只有原语和引用。


  

如果我分配一个空值是什么?


这是可以改变的参考价值,但除此之外没有任何反应。


  

如果我宣布一个INT I那么我可以马上拨打我++,因为没有参考使用,只是内存部分的0

设置好的

没有堆将被使用,可能没有堆栈将被使用(可能4字节)。可能的JIT编译器将删除code。如果它没有做任何事情。


  

也许我不是唯一一个有兴趣


...但不是太害怕问。 ;)

I would like to know exactly the real space allocated in memory for an object.

I try to explain with some example: using a 64 bit JVM, pointer size should be 8 bytes, so:

  • Object singletest = new Object(); will take 8 bytes to reference the Object plus the size of the Object
  • Object arraytest = new Object[10]; will take 8 byte to reference the position where the array is stored plus 8*10 bytes to store the array plus the size of each Object
  • int singleint = new int; will take just 2 bytes, because int is a primitive type
  • int[] arrayint = new int[10]; will take 8 bytes to reference the position and 10*2 bytes for the elements

Moreover, this is the reason why Java allows to write code like this:

int[][] doublearray = new int[2][];
int[0][] = new int[5];
int[1][] = new int[10];

What really happen is that an array will produce a reference (aka pointer) like an object, so it doesn't really matter the size of the second dimension at declaration time (and dimensions can be different, there is no link between them). Then the space taken will be: a reference to doublearray (8 bytes), first dimension is simply a reference to the second one, so other 8 bytes * 2 (first dimension size), and finally 2 bytes * 5 plus 2 bytes * 10.

So, finally, if have a real class like this:

class Test {
   int a, b;
   int getA() {return A};
   void setA(int a) {this.a = a;}
   int getB() {return B};
   void setB(int b) {this.b = b;}
}

when I call a new to instantiate, a pointer (or name it reference, because it's Java) of 8 bytes will be used plus 2+2bytes to store the integers into the class.

The questions are: am I right or I wrote total nonsense? Moreover, when I don't instantiate an object but I just declare it, 8 bytes will be allocated for further use or not? And what if I assign a null value?

Meanwhile for primitive type I'm quite sure that just declaring it will allocate the requested space (if I declare an "int i" then I can immediately call i++ because no reference are used, just a portion of memory is setted on "0").

I searched on internet without clever response... I know that I wrote a lot of questions, but any help will be appreciated! (and maybe I'm not the only one interested)

解决方案

using a 64 bit JVM, pointer size should be 8 bytes,

Actually its usually 32-bit unless you have a maximum heap size of 32 GB or more heap. This is because Java uses references, not pointers (and each object is on an 8 byte, not 1 boundary)

The JVM can change the size of a reference depending on which JVM you use and what the maximum heap size is.

Object singletest = new Object(); will take 8 bytes to reference the Object plus the size of the Object

The object will use about 16 bytes of heap. It may or may not use 4 bytes of stack, but it could just use a register.

Object arraytest = new Object[10];

This will use about 16 bytes for the header, plus 10 times the reference sizes (about 56 bytes in total)

int singleint = new int; will take just 2 bytes, because int is a primitive type

int is always 32-bit bit, you can't create a newprimitive. As its notionally on the stack it might use 4-bytes of stack or it might only use a register.

int[] arrayint = new int[10]; will take 8 bytes to reference the position and 10*2 bytes for the elements

Again the object is likely to be the same size as the new Object[10] (56 bytes)

int[][] doublearray = new int[2][];
int[0][] = new int[5];
int[1][] = new int[10];

I wouldn't call it a doublearray as it could be confused with double[]

However the size is likkely to be about 16 + 2 * 4 for doublearray and 16 + 5*4 + 4 (for padding) and 16 + 10 * 4.

Memory allocated on the heap is aligned to an 8 byte boundary.

I call a new to instantiate, a pointer (or name it reference, because it's Java) of 8 bytes will be used plus 2+2bytes to store the integers into the class.

The reference is on the stack and usually this is not included.

The object has a header of about 16 bytes and the int values take up 2 * 4 bytes.

when I don't instantiate an object but I just declare it

Java doesn't let you declare Objects, only primitives and references.

what if I assign a null value?

That could change the value of the reference, but otherwise nothing happens.

if I declare an "int i" then I can immediately call i++ because no reference are used, just a portion of memory is setted on "0"

No heap will be used, possibly no stack will be used (possibly 4 bytes). Possible the JIT compiler will remove the code if it doesn't do anything.

maybe I'm not the only one interested

... but was not too afraid to ask. ;)

这篇关于Java和精确的参考尺寸为对象,数组和基本类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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