Java Class Loader and Garbage Collection in Java

Java Class Loader

During execution of a Java program, the Java Virtual Machine loads the compiled Java class files using a Java class of its own called " class loader " (java.lang.ClassLoader).

  • The class loader is called when a class member is used for the first time:

The use of the class loader is invisible to you. You can see the results of the class loader by using the - verbose flag when you run your application. 

For example:
java -verbose -classpath D:\test com.example.HelloWorld

[Loaded java.lang.Object from shared objects file]

[Loaded java.io.Serializable. from shared objects file]

[Loaded java.lang.Comparable from shared objects file]

[Loaded java.lang.CharSequence from shared objects file]

[Loaded java.lang.String from shared objects file]

[Loaded java.lang.reflect.GenericDeclaration from shared objects file]

[Loaded java.lang.reflect.Type from shared objects file]

[Loaded java.lang.reflect.AnnotatedElement from shared objects file]

[Loaded java.lang.Class from shared objects file]

[Loaded java.lang.Cloneable from shared objects file]

[Loaded java.lang.ClassLoader from shared objects file]

......and many more


Garbage Collection
When an object is instantiated using the new keyword, memory is allocated for the object. 
The scope of an object reference depends on where the object is instantiated:

  • It is a process of reclaiming the memory for those objects which are currently not in used.
  • Its Save Memory


  • When someMethod completes, the memory referenced by e is no longer accessible.
  • Java's garbage collector recognizes when an instance is no longer accessible and eligible for collection.
Note: When an object's memory is freed depends upon a number of factors.



About Codingwelove

Codingwelove
Recommended Posts × +

0 comments:

Post a Comment