Posts

Showing posts from May, 2018

Java Object Size

Image
Do you really know the size of your object? I was curious to see where I was loosing so much memory in my application when I knew the objects size based on the fields within. This provoked me to write a blog on understanding object structure and calculating its size. All throughout the blog, I have used a class called MSMemory. The fields in the class keep changing to explain size for each. Configuration: OS: Windows RAM: 16GB JDK: .8.0_171 64 bit I use jmap to get the heap dump. The command is below (Use jps to get the java process id): jmap -dump:format=b,file=heap.bin <java_process_id> Then i use jhat to analyse the heap dump. The command is below: jhat heap.bin Jhat starts a HTTP server on port 7000 by default. Port number is configurable. 1. Class MSMemory has no fields. Size is for one instance of MSMemory. public class MSMemory {} /*Main class*/ public class MemoryAnalyser { public static void main(String[] args) { MSMemory memory = new MSM...