What is the purpose of garbage collection in Java? When is it used?

What is the purpose of garbage collection in Java? When is it used?

Asked on November 13, 2018 in Java.
Add Comment

  • 3 Answer(s)

        In java, garbage means un-referenced objects.

        Garbage Collection is process to reclaim the run-time unused memory automatically. In other words, it is a way to destroy the unused objects.

        By using free () function in C language and delete () in C++.For java it is performed automatically. So, java provides better memory management.

    Advantage of Garbage Collection

    • It makes java more memory efficient because garbage collector removes the unreferenced objects from heap memory.
    • It is automatically done by the garbage collector (a part of JVM) so we don’t need to make extra efforts.

    When does java perform garbage collection:

    1. When the object is no longer reachable
    2. When one reference is copied to another reference.

    Invoking the Java garbage collector:

        Invoking the Java garbage collector requires a simple two-step process. First to create a Java Runtime object. If Runtime objects will interface with the environment in which your application is running. Then, after creating the Runtime object, you’ll invoke the gc() method (“garbage collector”) of the Runtime class.

    These two steps look like this:

    Runtime r = Runtime.getRuntime();
    Answered on November 13, 2018.
    Add Comment

    Every time you create an Object memory is allocated from your computers f to store that object. If you never got rid of these objects then your program would use more and more memory as the more it allocated objects.

    In order to fix this you need some way of deleting objects when you don’t need them anymore. In older languages like C++ the programmer was responsible for deleting objects manually. The programmer would have to use some method to determine when an object was no longer needed and then manually remove that object.

    This is a pain and can lead to memory leaks when programmers fail to do it correctly. When the Java language was designed its creators decided that the computer should take responsibility for removing objects when they were no longer necessary rather than the programmer.

    Java therefore contains a garbage collector. This is an automatic memory management system that detects when objects are no longer necessary and removes them. It does this by checking if there is anyway the programmer could get a reference to that object. If there are no accessible references to the object left the garbage collector marks the object as trash and deallocates it at the next opportunity.

    One result of this is that you can still get memory leaks in Java by maintaining references to objects that are not longer required.

    Answered on January 18, 2019.
    Add Comment

    It frees memory allocated to objects that are not being used by the program any more – hence the name “garbage”. For example:

    public static Object otherMethod(Object obj) {
        return new Object();
    public static void main(String[] args) {
        Object myObj = new Object();
        myObj = otherMethod(myObj);
        // ... more code ...  

    I know this is extremely contrived, but here after you call otherMethod() the original Object created is made unreachable – and that’s “garbage” that gets garbage collected.

    In Java the GC runs automatically, but you can also call it explicitly with System.gc() and try to force a major garbage collection. As Pascal Thivent points out, you really shouldn’t have to do this and it might do more harm than good (see this question).

    For more, see the wikipedia entry on Garbage collection and Tuning Garbage Collection (from Oracle)

    Answered on January 18, 2019.
    Add Comment

  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.