6

How can "size in bytes" of a array of some datatype be determined?

What makes me ask this is a question from this icse board question paper.

The question is as follows:

And the accepted answers by the board are (space occupied by one of those data types*no of elements in the array)

For example , one int array of 10 objects is said to take 4*10=40Bytes

But my question is, wouldn't the array have to store it's length and indexes somehow(increasing it's size on memory), or am I misunderstanding the question?

1
  • That is just a answer what test checker's will understand. Your understanding is correct. But dare you write it in exam. Commented Feb 17, 2014 at 15:23

1 Answer 1

7

Your understanding is indeed correct. The official answer adopts a simplistic view of how arrays are stored.

There is a fixed cost associated with every object. Additionally, arrays need to keep track of their size and possibly other things.

The answer also ignores issues such as padding, alignment, etc.

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

5 Comments

So, there is no reliable way to determine it?
@AmithKK: You could use a memory profiler (VisualVM, YourKit, etc) to determine this at runtime. The answer would of course be JVM-specific.
@AmithKK: Again, depends on the JVM, on whether it's 32- or 64-bit, etc. I've not seen any official formulas.
@AmithKK these are not formulas, they are profiling tools, much like eclipse.
I know @Batty. You can't use a profiling tool during an exam ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.