Recently, I was writing a class in which I discovered that I could reduce memory consumption of instances by ~10 bytes/element, but only at the cost of making the code much more complex. This increased the size of the compiled .class
file by ~10KB.
I assume that the JVM has to load the .class
file into memory, so these changes wouldn't pay for themselves unless there were at least 1000 elements or so. But that arithmetic doesn't work out unless the extra 10KB in the class file is the only cost of the increased code complexity.
This Oracle blog suggests that there's a fair amount of extra memory getting consumed by the class in the permgen that isn't just based on the .class
file -- for example, I suspect that more complex code might require more memory for optimization metadata.
So, this question has two parts:
Similar details for the Dalvik VM would be appreciated, but I'm mostly focused on OpenJDK and the other "mainstream" JVMs.
How can I measure the actual permgen memory consumption of a particular class? Either at runtime with some instrumentation, or with profiling tools?
One approach could be to load the class to measure in a different classloader and use jmap -permstat
as presented in this blog entry.
For each class loader object, the following details are printed:
- The address of the class loader object – at the snapshot when the utility was run.
- The number of classes loaded (defined by this loader with the method(java.lang.ClassLoader.defineClass).
- The approximate number of bytes consumed by meta-data for all classes loaded by this class loader.
- The address of the parent class loader (if any).
- A “live” or “dead” indication – indicates whether the loader object will be garbage collected in the future.
- The class name of this class loader.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With