Garbage collection in JAVA-vishmy1.blogspot.com

JAVA GARBAGE COLLECTION

In JAVA, garbage means unreferenced objects in Java, the destruction of objects from memory is done automatically by the JVM. When there is no reference to an object, then that object is assumed to be no longer needed and the memory occupied by the object is released. this technique is called Garbage Collection.

Garbage Collection in Java is the process of memory management so that the Java Programs can perform automatically. 

Garbage Collection of JAVA is an automatic process by which the programmer does not need to explicitly mark objects to be deleted.

Garbage Collector always runs in the background.

The main objective of the Garbage Collector is to free the heap memory by destroying the unreachable objects.


Garbage collection in JAVA-vishmy1.blogspot.com
Java Garbage collection


finalize() method:-

The finalize method is invoked each time before the object is garbage. The garbage collector of the JVM collects only those objects that are created by a new keyword.

finalize() method is defined in a class called java.lang.object, therefore it is available for all the classes.

finalize() method can be defined as protected in object class.

finalize() method is called only once by gc() (garbage collector) thread.
ex:-

protected void finalize()
{
      //finalize code;
}



System.gc() method:- 

It is used to invoke the garbage collector to perform cleanup processing. This thread calls the finalize method before the object is garbage collected. gc() method is present in the System or runtime class.

ex:-

class test
{  

    public void finalize()
   {
        System.out.println("destroyed");
   }
public static void main(String args[])
{
     test t1= new test();
     t1=null;//unreachable object
     System.gc();
}
}

Benefits of Java garbage Collection:-

The Java Garbage Collection that it handles the deletion of unused objects or unreachable objects from the memory is the biggest benefit.

The Java Garbage Collection is the process of memory management that is done automatically by the JVM(Java Virtual Machine).

Java Garbage Collection is increases memory efficiency and decreases the chances of memory leak.
     

Note:- Please write a comment if you find anything incorrect, this will help me to improve to provide you better information. 


Post a Comment

5 Comments

  1. It keeps me to engage on the content and it gives good explanation of the topics.
    JAVA Course in Chennai
    Learn Java Online
    JAVA Course in Bangalore
    What is JVM

    ReplyDelete
  2. Awesome article! You are providing us very valid information. This is worth reading. Keep sharing more such articles.
    Data Science Course in Chennai
    Data Science Online Training
    Data Science Course in Coimbatore
    why devops

    ReplyDelete
  3. I loved your post.Much thanks again. Fantastic.net coure
    net training

    ReplyDelete
  4. infomative blog, keep posting . If you want to learn about java web developer then checkout java course in pune

    ReplyDelete