0

How many heap space takes array object? I mean not a array reference. For example how many heap space will take this array

Integer[] array = new Integer[5];

Or perhaps you know some ways how I can to test memory usage of my application? Any suggestions and advices will be appreciated)

Thanks in advance!

trincot
  • 317,000
  • 35
  • 244
  • 286
Mike Herasimov
  • 1,319
  • 3
  • 14
  • 31

2 Answers2

1

It depends if the array is of primitive type of object types. If it is int[] = new int[5] it will be 5 times int memory size - 5x4=20 bytes.

If it is Object and not primitive like in your case (Integer) then the memory used is the number of the object multiplied by reference memory size ( the pointer size ) - 32 bits or 64 bits depending on the JVM.

Veselin Davidov
  • 7,031
  • 1
  • 15
  • 23
  • 1
    An array has a type pointer and size field in addition to the actual elements, so it is typically (count+1)*oopsize + intsize. – eckes Apr 15 '15 at 21:08
-1

I believe you mean:

Integer[] array = new Integer[5];

And integers are 4 bytes when being used... so 4*5= 20 bytes. But there is more to it than just this.. Certain overhead on the array's in such and the fact you are using the Integer class rather than just doing:

int[] array = new int[5];

I would suggest trying to google for a more explained answer or go here: Calculating Java Array Memory Usuage

Community
  • 1
  • 1
user2494817
  • 697
  • 4
  • 12