Old Fashioned Coding Style May Help Garbage Collector to Reclaim Memory Quickly

    3
    17
    Spread the love
    • 1
    •  
    •  
    •  
    •  
    •  
    •  
    •  
    •  
    •  
      1
      Share

    Old Fashioned Coding Style May Help Garbage Collector to Reclaim Memory Quickly

    In my last post about Garbage Collector I have explained how Garbage Collector works.In this post, my objective is to code in such an old fashioned manner that will help Garbage collector to reclaim its memory.


    Yes!! this post is based on C++ like memory de- allocation technique.

    In C++ , a new operator will allocate memory space and delete operator will delete or free up the memory space.In Java,JVM runs a low priority thread called Garbage Collector to reclaim memory while new operator remain same for allocating memory.

    As Garbage Collector is a low priority thread,Lets help GC to work better

    How??

    obj=null;

    yes!! setting an object to null will ease the memory reclaim even if the program is running.
    Lets create a class called pen:

    public class Pen {
    int countofpen=0;
    public Pen() {
    countofpen=1;
    }
    }

    Now create another class called penApp:

    public class penApp {
    public static void main(String[] args) {
    //some code
    Pen pen = new Pen();
    //Some code
    pen=null;
    }
    }

    In this old fashioned manner we can help GC to collect memory while the code is running.

    Don't miss out!
    Subscribe To Newsletter

    Receive top technical news, lesson ideas, travel tips and more!

    Invalid email address
    Give it a try. You can unsubscribe at any time.

    Spread the love
    • 1
    •  
    •  
    •  
    •  
    •  
    •  
    •  
    •  
    •  
      1
      Share

    3 COMMENTS

    LEAVE A REPLY

    Please enter your comment!
    Please enter your name here