A small high performance utility for measuring object memory usage in Java.
long bytes = MemoryUtil.sizeOf( object );
This utility is not meant to replace the excellent range of profiling tools available for Java such as VisualVM, YourKit or JProfiler, it is aimed for runtime debug/logging of selected structures in a production system.
Common primitive and object sizes:
| boolean | 1 byte |
| byte | 1 byte |
| short | 2 bytes |
| integer | 4 bytes |
| long | 8 bytes |
| float | 4 bytes |
| double | 8 bytes |
| Boolean | 16 bytes |
| Byte | 16 bytes |
| Short | 16 bytes |
| Integer | 16 bytes |
| Long | 16 bytes |
| Float | 16 bytes |
| Double | 16 bytes |
| Currency | 16 bytes |
| Date | 24 bytes |
| BigInteger | ~56 bytes |
| BigDecimal | ~144 bytes |
| GregorianCalendar | ~460 bytes |