7

In java 6, classes used to be load under perm gen space and once they are loaded they will not be removed(or Garbage collected) from permgen space either they are still in use or not at the time of GC. Right ?

But in java 8, permgen area is completely removed and classes deifinition are loaded in heap. My question is will class definition will reside in heap forever(till jvm is killed) whether they are in use or not like in java 6 or it will be garbage collected like any other object ?

emilly
  • 10,060
  • 33
  • 97
  • 172
  • Not right about 1.6. With the options `-XX:+CMSClassUnloadingEnabled -XX:+UseConcMarkSweepGC` you do get garbage collection of classes once the classloader and all classes are unreachable. http://stackoverflow.com/questions/3334911/what-does-jvm-flag-cmsclassunloadingenabled-actually-do – Erwin Bolwidt Sep 21 '16 at 05:46
  • @ErwinBolwidt I am assuming no explicit JVM parameters are set here. I believe both `-XX:+CMSClassUnloadingEnabled -XX:+UseConcMarkSweepGC` are not default jvm parameters ? – emilly Sep 21 '16 at 06:33
  • the default is that you're using the system classloader, and in that case classes will never become unreachable, on any version of Java. If you use separate classloaders and you want classes to be garbage collected when they're no longer reachable, then you need to specify those options in jdk 1.6. On Java 8 the class metadata will be put in the metaspace and garbage collected by default, but still only when the classloader becomes unreachable. There is tons of information available on StackOverflow on permgen and metaspace, suggest you research them and fine-tune your question. – Erwin Bolwidt Sep 21 '16 at 06:51
  • @ Erwin Bolwidt in that case any class loaded will always be reachaable whether it java 6 or java 8 as for standalone java process it will be loaded by system/applicatio class loader and in case of webserver(tomcat) it will always be loaded by webapp class loader. So both will be reachable and will never be GC until and unleass i use custom class loader. Right ? – user3198603 Sep 21 '16 at 07:19

0 Answers0