Came across this blog post by Daniel Lemire. It’s not his most interesting or informative post, but made me remember a little utility I wrote many years ago. MemoryEstimator is a tool to estimate the memory footprint of any Java type. (It’s part of ojAlgo.) Just had to verify if its estimates are aligned with the numbers presented in that blog post. In that post there is a table of estimated memory usage for byte arrays of different sizes. These are the numbers:

size of the arrayestimated memory usage
016 bytes
124 bytes
224 bytes
324 bytes
424 bytes
524 bytes
624 bytes
724 bytes
824 bytes
932 bytes

Now, let’s see if MemoryEstimator produces the same estimates.

Example/Test Code

Console Output

class TheMemoryEstimator
ojAlgo
2022-12-01


Size of byte[] of different lengths
===================================
byte[0] == 16 bytes
byte[1] == 24 bytes
byte[2] == 24 bytes
byte[3] == 24 bytes
byte[4] == 24 bytes
byte[5] == 24 bytes
byte[6] == 24 bytes
byte[7] == 24 bytes
byte[8] == 24 bytes
byte[9] == 32 bytes

Memory footprint of Java's basic types
======================================
boolean 1 bytes
	16 bytes when wrapped/boxed in a class
byte 1 bytes
	16 bytes when wrapped/boxed in a class
char 2 bytes
	16 bytes when wrapped/boxed in a class
double 8 bytes
	24 bytes when wrapped/boxed in a class
float 4 bytes
	16 bytes when wrapped/boxed in a class
int 4 bytes
	16 bytes when wrapped/boxed in a class
long 8 bytes
	24 bytes when wrapped/boxed in a class
Object 4 bytes
	16 bytes when wrapped/boxed in a class
short 2 bytes
	16 bytes when wrapped/boxed in a class

Memory footprint of some specific types
=======================================
BigDecimal     == 40 bytes
LocalDate      == 24 bytes
Instant        == 24 bytes
OptionalDouble == 24 bytes