I would like to know if the JVM normally unloads classes in order to decrease the PermGen space. So here my questions:
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.
-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.