简体   繁体   中英

Is the PermGen space ever decreased?

I would like to know if the JVM normally unloads classes in order to decrease the PermGen space. So here my questions:

  1. Do Java classes ever get unloaded by default from a JVM?
  2. Does closing a Jar classloader unloads all the loaded classes from that jar?
  3. What commands/ways should be used to allow the unloading of classes?

FYI, I did try some of the solutions on the web but none of them answered my questions. (for example: What does JVM flag CMSClassUnloadingEnabled actually do? )

PS: I am referring to Java 6 + hibernate (The class loading is handled by hibernate)

The only way to get rid of loaded classes is to allow JVM to GC the class loader which loaded these classes by eliminating all references to this class loader. This is what happens in web servers when we undeploy a webapp.

Currently the permanent generation is being collected by default.

The Permanent Generation is comprised of a single space, but does not have its own collector. Instead, this space is collected as part of the Old Generation.

According to this visualgc page.

In old JVMs you could enable this with the -XX:+CMSPermGenSweepingEnabled flag.

AFAIK classes get unloaded when they are not referenced from any ClassLoader.

java.lang.OutOfMemoryError: PermGen space errors are cause by memory leaks in class loaders. A common problem when undeploying/redeploying web applications.

Going to assume you're talking about HotSpot, the Oracle/Sun JVM.

  1. If there are no references to them. Note that all classes have a reference to their classloader and vice-versa, and all objects have a reference to their class.
  2. Maybe. What do you mean by "close"?
  3. Start the JVM with the options -XX:+UseConcMarkSweepGC and -XX:+CMSClassUnloadingEnabled . This will allow unloading of unused classes even if their classloader still exists.

See also Unloading classes in java?

Hibernate loads all the Entity classes in memory when the SessionFactory starts. On the other hand the Entity istances are loaded by Hibernate in a lazy way, only when needed.

Classes can be unloaded by the GC when the JVM needs memory. Entity istances are garbage collected too but, as any other object, only when there are no more references pointing at them. In other words Entity istances cannot be unloaded if the Hibernate session is still open. To unload them you need to clear the persistence context or close the session.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM