7
  1. Is there any way to measure the currently used size of permanent generation (PermGen) within my Java application? I cannot use external profiling tools such as VisualVM.

  2. Even better would be an estimation of the memory consumption of a Java class in the PermGen. Is it nearly proportional to the size of the bytecode file?

s106mo
  • 1,243
  • 2
  • 14
  • 20

5 Answers5

9

You could use MemoryMXBean that comes with JDK. But I don't think there is a way to query on the permgen usage from within a running application. Docs about MemoryMXBean.

Community
  • 1
  • 1
Aravind Yarram
  • 78,777
  • 46
  • 231
  • 327
  • Thanks! That answer helped me very much. I will vote your answer useful as soon as I have enough reputation! :) – s106mo Nov 03 '10 at 08:44
2

You can use jvisualvm tool from JDK with Visual GC plugin to monitor all JVM heap areas including PermGen.

Eugene Kuleshov
  • 31,461
  • 5
  • 66
  • 67
2

If you're not on Windows, you could try jmap which is shipped with the JDK.

Damien
  • 2,254
  • 1
  • 22
  • 30
1

You can use the jmap command:

jmap [option] (to connect to running process)

jmap [option] (to connect to a core file)

jmap [option] [server_id@] (to connect to remote debug server)

where options is one of:

-heap : to print java heap summary

-permstat : to print permanent generation statistics

doelleri
  • 19,232
  • 5
  • 61
  • 65
  • thank you for your answer, however the decisive point of my question was that I wanted to measure the memory consumption within my application (i.e., programmatically) ... so the MemoryMXBean is the right way to go – s106mo Sep 10 '13 at 20:12
0

The size of PermGen has nothing to do with the size of your class files. The default size of PermGen according to Sun is 64 MB. If you wish to increase it you can set it explicitly using:

-XX:PermSize=164m

in your Java command line or startup script. This is also how you can know what it's set to without relying on an external tool.

EDIT:

Read this article to determine approximately how much PermGen is currently being used programmatically (i.e. no external tools):

http://frankkieviet.blogspot.com/2006/10/how-to-fix-dreaded-permgen-space.html

Bert F
  • 85,407
  • 12
  • 106
  • 123
Amir Afghani
  • 37,814
  • 16
  • 84
  • 124
  • The *max* size doesn't necessarily have anything to do with the size of any file. But stefan's looking for the current (used) size, not the max size. – Mark Peters Nov 01 '10 at 17:50
  • According to http://blogs.sun.com/jonthecollector/entry/presenting_the_permanent_generation, e.g. the bytecodes of the class methods have an influence on the size of PermGen! – s106mo Nov 01 '10 at 17:51
  • I did not specify the max size. The flag for the max perm size is MaxPermSize – Amir Afghani Nov 01 '10 at 17:51
  • @stefan, you're confused. Read the document thoroughly. PermSize is where your class files are loaded yes, but the *size* of PermSize is not determined by your specific class files. – Amir Afghani Nov 01 '10 at 17:52
  • Oh I see, you want to know how much of PermSpace is currently occupied. You should clarify that in your question. – Amir Afghani Nov 01 '10 at 17:54