Pages

sizeof in java or how much memory is this object going to consume

Introduction

Ever wonder what is the size of an object? How much space does it take up? And where can I find out this info?
 
 
In this post we will try to answer these question. And see what difference does it make if you use primitives vs Objects (for example float vs Float).
 
The code used in this example is available here for you to try out. I ran my code with JDK 21. Feel free to try out other versions and see if makes a difference.