0

When I create new object say Employee emp = new Employee, then per mine understanding system class loader(in case of stand alone program) or web class loader(in case of webserver like tomcat) loads the Employee class object(including class level fields/methods) and then actual Employee instance is created.

My question is why class object is garbage collected even if reference is dead but object is garbage collected if unreachable.

Is it because Employee class object is still referred internally by class loader(system/web class loader) but employee instance is not referred by class loader ?

Update :- My question has been marked duplicate of When and how are classes garbage collected in Java? but it does not actually answer mine question. This question is mainly about comparison why class is not GC'ed but object is not so it should be reopened . That link just answered that when/when not class object can be garbage collected but my question then why not object also. Is it not reference by class loader ?

trincot
  • 317,000
  • 35
  • 244
  • 286
emilly
  • 10,060
  • 33
  • 97
  • 172
  • @azurefrog link actually does not actually answer mine question. This question is mainly about comparison why class is not GC'ed but object is ? so it should be reopened – emilly Sep 22 '16 at 17:36
  • 1
    But a `Class` *can* be GCd. The dupe's accepted answer states the conditions that must be met for it to happen. Your question is also a bit unclear since you keep mixing up "is / is not". Objects aren't referenced by classloaders. – Kayaman Sep 22 '16 at 17:59
  • @kayaman I think you answered my question with objects are not referenced by class loader. I tried googling it whether objects are referenced by class loader or not but could not confirm. I think I should have asked this instead why objects are GC'ed – emilly Sep 22 '16 at 18:03

0 Answers0