Pages

Saturday 10 August 2019

Garbage collection

Agenda:
  • Introduction
  • The way to make an object eligible for Garbage Collection
  • The methods for requesting JVM  to run Garbage Collection
  • Finalization
  • Memory Leaks
Introduction:
  • Garbage Collection: It is a process of collecting unused objects from the memory(heap).
  • And the actor who is collecting this garbage is called Garbage Collector.
  • Garbage Collection is an automatic process, JVM will do this process with the help of the Garbage collector.
  • As a developer, you can't start the execution of Java's garbage collector. You can only request it to be started by calling
  • System.gc() or Runtime.getRuntime().gc().But calling this method doesn't guarantee when the garbage collector would start.
The way to make an object eligible for Garbage Collection.

1.Nullifying the reference variable:
If an object is no longer required then you can make eligible for Garbage Collection by assigning null
 to all its reference variable.
Studnet s1 = new Student();
Studnet s2 = new Student();
s1 = null;
s2 = null;

2. Reassign the reference variable:
If any object is no longer required then reassign all its reference variables to some other objects then the old object is by default eligible for Garbage Collection.

3.An object created inside a method:
Objects created inside a method are by default eligible for Garbage Collection once method completes.
Example 1:
  1. class Test{
  2. public static void main(String[] args){
  3. createObject();
  4. }
  5. public static void createObject(){
  6. Student s1 = new Student();
  7. Student s2 = new Student();
  8. }
  9. }
Example 2:
  1. class Test{
  2. public static void main(String[] args){
  3. Student s = createObject();
  4. }
  5. public static Student createObject(){
  6. Student s1 = new Student();
  7. Student s2 = new Student();
  8. return s1;
  9. }
  10. }

Example 3:
  1. class Test{
  2. public static void main(String[] args){
  3. createObject();
  4. }
  5. public static Student createObject(){
  6. Student s1 = new Student();
  7. Student s2 = new Student();
  8. return s1;
  9. }
Example 4:
  1. class Test{
  2. static Student s1;
  3. public static void main(String[] args){
  4. createObject();
  5. }
  6. public static void createObject(){
  7. s1 = new Student();
  8. Student s2 = new Student();
  9. }
  10. }
4. Island of Isolation:
  1. class Test{
  2. Test i;
  3. public static void main(String[] args){
  4. Test t1 = new Test();
  5. Test t2 = new Test();
  6. Test t3 = new Test();

  7. t1.i = t2;
  8. t2.i = t3;
  9. t3.i = t1;

  10. t1 = null;

  11. t2 = null;

  12. t3 = null;
  13. }
  14. }
Note: If an object doesn't have any reference then it always eligible for Garbage Collection.
Note: Even though the object having reference still it is eligible for Garbage collection sometimes.
Ex: Island of isolation, all references are internal references.

The methods for requesting JVM to run Garbage Collection:
  • Once you made an object eligible for Garbage Collection. It may not be destroyed immediately by the Garbage Collector.
  • Whenever JVM runs Garbage collection then the only objects will be destroyed by Garbage Collector.
  • You can request JVM  to run a garbage collector programmatically, but whether JVM accepts your request or not there is no guaranty. But most of the time JVM will accept our request.
The following are various ways for requesting JVM to run Garbage Collector:
By System class:
  • System class contains a static method Garbage Collection for this purpose.
  • Ex: System.gc();
By Runtime class:
  • A java application can communicate with JVM by using the Runtime object.
  • Runtime class is a singleton class present in java.lang Package.
  • You can create a Runtime objects by using the factory method getRuntime().
    E.g.: Runtime r = Runtime.getRuntime();
  • Once you got Runtime object you can call the following methods on that object.
  • freeMemory(): returns the free memory present in the heap.
  • totalMemory(): returns total memory of the heap.
  • gc(): for requesting JVM to run garbage collector.
Note: gc() method present in System class is static, whereas it is an instance method in Runtime class.
Note: Over Runtime class gc() method, System class gc() method is recommended to use.
Note: In java, it is not possible to find the size of an object and address of an object.

Finalization:
  • Just before destroying any object garbage collector always calls finalize() method to perform cleanup activities.
  • If the corresponding class contains finalize() method then it will be executed otherwise Object class finalize() method will be executed.
  • Which is declared as follows
  • protected void finalize() throws Throwable.
Case 1:
Just before destroying any object garbage collector calls finalize() method on the object which is eligible for Garbage Collection then the corresponding
class finalize() method will be executed.
For example: If the String object is eligible for Garbage Collection then the String class finalize() method is executed but not Test class finalize() method.
Example:
  1. class Test{
  2. public static void main(String[] args){
  3. String s = new String("kalawati");
  4. Test t = new Test();
  5. s = null;
  6. System.gc();
  7. System.out.println("End of main");
  8. }
  9. public void finalize(){
  10. System.out.println("Finalize() method is executed"): 
  11. }
  12. }
Case 2:
You can call finalize() method explicitly then it will be executed just like a normal method call and object won't be
destroyed. But before destroying any object Garbage Collector always calls finalize() method.
Example:
  1. class Test{

  2. public static void main(String[] args){
  3. Test t = new Test();
  4. t.finalize();
  5. t.finalize();
  6. t=null;
  7. System.gc();
  8. System.out.println("End of main");
  9. }
  10. public void finalize(){
  11. System.out.println("Finalize() method is executed"): 
  12. }
  13. }
Case 3:
  • finalize() method can be call either by the programmer or by the Garbage Collector.
  • If the programmer calls explicitly finalize() method and while executing the finalize() method if an exception is raised and uncaught then the program will be terminated abnormally.
  • If Garbage Collector calls finalize() method and while executing the finalize() method if an exception raised and uncaught then JVM simply ignores that exception and the program will be terminated normally.
Example:
  1. class Test{
  2. public static void main(String[] args){
  3. Test t = new Test();
  4. //t.finalize();
  5. t=null;
  6. System.gc();
  7. System.out.println("End of main");
  8. }
  9. public void finalize(){
  10. System.out.println("finalize() method is called");
  11. System.out.println(0/0);
  12. }
  13. }
If we are not comment line4 then programmer calling finalize() method explicitly and while executing the finalize() method
ArithmeticException raised which is uncaught hence the program terminated abnormally.
If you comment line4 the Garbage collector calls finalize() method and JVM ignores ArithmeticException and the program will be terminated normally.

Which of the following is true?
While executing finalize() method JVM ignores every exception(invalid);
While executing finalize() method JVM ignores only uncaught exception(valid);

Case 4:
On any object, GC calls finalize() method only once.
Example:
  1. class FinalizeDemo{
  2. static FinalizeDemo s;
  3. public static void main(String[] args) throws Exception{
  4. FinalizeDemo f = new FinalizeDemo();
  5. System.out.println(f.hasCode());
  6. f=null;
  7. System.gc();
  8. Thread.sleep(5000);
  9. System.out.println(s.hasCode());
  10. s=null;
  11. System.gc();
  12. Thread.sleep(5000);
  13. System.out.println("End of main method");
  14. }
  15. public void finalize(){
  16. System.out.println("finalize() method is called");
  17. s=this;
  18. }
  19. }
Memory leaks:
An object which is not using in our application and it is  not eligible for Garbage Collection such type of objects are called memory leaks.