Search code examples
javamemory-managementgarbage-collectionclassloader

class object referred by class loader?


why class objects are referred by class loader ? should it not be other way around i.e. class loader should have been referred so that class object could have become eligible for GC if class object is unreachable and lesser memory consumption ?

I know i am missing some thing basic here but not sure what. I tried googling it but could not find answered


Solution

  • Because the ClassLoader loads classes and its responsibility is to know what classes it has loaded. Class objects also have a reference to the ClassLoader that loaded it, hence the getClassLoader() method in Class.

    Memory consumption has absolutely nothing to do with anything here, I can't imagine why you would think it does.