Calculate size of Object in Java

on April 12 | in Java | by | with No Comments


Recently I need the ability to view the actual memory allocation of a java object, in this case it was a LinkedHashMaps. The system I was working on was pulling large amounts of data from an Oracle database, over 200k rows. The result sets were then pushed into a LinkHashMap and delivered to the client via gziped json stream. The gziped data was around 10mb but it turned out that the LinkedHasMap was using over 100mb of memory. This was on a 64 bit machine, the same process on a 32 bit machine would use over 300 mb of memory. The UI allowed the data to be consumed and worked with in a excel like environment which the user base loved. In the end we chunked the data into several calls which fixed the out of memory errors.

java.SizeOf is a java agent that you can use to calculate the size of a object. Much like you would do in C with sizeof(). You can download the jar and view the instructions here or

To use this program in your java webapps you will need to start your application with the following JVM parameter

To run you application in Eclipse using a VM paramater









Pin It

Leave a Reply

Your email address will not be published. Required fields are marked *