Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I estimate a class's total permgen memory consumption?

Tags:

java

jvm

permgen

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:

  • How can I measure the actual permgen memory consumption of a particular class? Either at runtime with some instrumentation, or with profiling tools?
  • Is there a way to estimate the permgen memory consumption of the class as I'm writing it? (With some background knowledge, you can estimate the memory consumption of class instances as you're writing them; I'm wondering if we can estimate the permgen memory consumption of the class itself.)

Similar details for the Dalvik VM would be appreciated, but I'm mostly focused on OpenJDK and the other "mainstream" JVMs.

like image 745
Louis Wasserman Avatar asked Jun 11 '12 18:06

Louis Wasserman


1 Answers

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:

  1. The address of the class loader object – at the snapshot when the utility was run.
  2. The number of classes loaded (defined by this loader with the method(java.lang.ClassLoader.defineClass).
  3. The approximate number of bytes consumed by meta-data for all classes loaded by this class loader.
  4. The address of the parent class loader (if any).
  5. A “live” or “dead” indication – indicates whether the loader object will be garbage collected in the future.
  6. The class name of this class loader.
like image 52
fglez Avatar answered Nov 02 '22 17:11

fglez