2

I am confused about the following question associated with memory allocation:

Assume that an object of class Circle occupies 64 bytes in memory. A reference variable occupies 8 bytes in memory. How many bytes are allocated in memory when the following line of code is executed?

Circle[] myCircleList = new Circle[10];

The correct answer is 88.

For now, my understanding is that 10 elements inside the myCircleList are reference pointer for Circle object that haven't been declared yet, so there are 8*10 = 80 bytes of memory allocated for them. Meanwhile, myCircleList is also a reference pointer in the stack, which is pointing to the array of object in heap, thus itself occupies 8 bytes. So a total of 88 bytes memory is allocated to this line of code.

However, I am uncertain about my understanding after reading this answer, and wonder if an additional of 8 bytes are required for storing the array length and alignment.

Is my understanding of the answer correct?

TobiSH
  • 2,833
  • 3
  • 23
  • 33
Suanovile
  • 31
  • 2
  • 1
    You're on the right track. An array needs to store its size, and all Java 0bjects have an overhead of 12 bytes, to maintain a reference to the object's class, the object semaphore for synchronizing, and something else I can't remember – ControlAltDel Oct 19 '20 at 12:01
  • If you take into account the phrase that you added in your question which says that reference occupies 8 bytes than 88 is correct because you have created 11 references. That phrase doesn't assume that array stores internally additional data and moreover doesn't provide types and memory size needed for those additional internal fields – Ivan Oct 19 '20 at 12:11

0 Answers0