, 1 min read
What is the size of a byte[] array in Java?
Java allows you to create an array just big enough to contain 4 bytes, like so:
byte[] array = new byte[4];
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 |
---|---|
0 | 16 bytes |
1 | 24 bytes |
2 | 24 bytes |
3 | 24 bytes |
4 | 24 bytes |
5 | 24 bytes |
6 | 24 bytes |
7 | 24 bytes |
8 | 24 bytes |
9 | 32 bytes |
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).