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!

4
  • Did you try to google it? Commented Apr 15, 2015 at 19:24
  • Have you ever seen this? Commented Apr 15, 2015 at 19:27
  • 1
    If you want to read the layout of a given object and its size you could use JOL. Commented Apr 15, 2015 at 21:09
  • @eckes thanks for your comment, as far as I see this thing is suitable for my purpose (wow it show how many space each Object use and many other interesting information) Commented Apr 16, 2015 at 5:44

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

An array has a type pointer and size field in addition to the actual elements, so it is typically (count+1)*oopsize + intsize.
-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

1 Comment

@eckes Yes... I shortly explained that in my answer...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.