A tool to measure memory usage of Java objects at runtime by traversing the object graph and adding up the bytes for fields and objects.
- Investigate heap usage problems. Profilers are good for looking at heap-wide histograms and finding allocation hotspots, but can't easily drill down to specific objects. Also, taking a heap dump usually requires halting execution.
- Planning.
- e.g. What's the memory overhead of caching X?
- Curiosity?
I wanted a tool that met the following criteria:
- Accuracy
- Performance
- Clear licensing (Apache License, 2.0)
- Stability
- Hosted in a public repo
- No dependencies
You can either build from scratch using mvn
:
- Clone this repo
mvn package
- Add
target/javasizer-VERSION.jar
to your classpath
Add this to your JVM arguments:
--add-opens java.base/java.lang=ALL-UNNAMED
--add-opens java.base/java.util=ALL-UNNAMED
org.jh.Sizer.sizeof(root)
will return the bytes used by the object graph.