Java allows you to create an array just big enough to contain 4 bytes, like so:
byte array = new byte;
How much memory does this array take? If you have answered “4 bytes”, you are wrong. A more likely answer is 24 bytes.
I wrote a little Java program that relies on the jamm library to print out some answers, for various array sizes:
|size of the array||estimated memory usage|
This is not necessarily the exact memory usage on your system, but it is a reasonable guess.
Further work: A library such as JOL might provide a more accurate measure, according to a reader (Bempel).