Destroying Objects

Recently, I was asked a question in an interview:

There are 20 objects and 7 need to be destroyed. Write an algorithm to destroy only 7 objects using garbage collector only.

I used to traditional way to destroy the objects using System.gc().

public class TestGarbage {
    public static void main(String[] args) throws InterruptedException {

        for (int i = 0; i < 20; i++) {
            TestGarbage ti = new TestGarbage();
            // System.out.println(ti);
        }

        for (int i = 0; i < 7; i++) {
            // Nullifying the reference variable
            TestGarbage ti = null;

            // requesting JVM for running Garbage Collector
            System.gc();
            // Runtime.getRuntime().gc();
        }

    }

    @Override
    // finalize method is called on object once
    // before garbage collecting it
    protected void finalize() throws Throwable {
        System.out.println("Garbage collector called");
        System.out.println("Object garbage collected : " + this);
    }
}

public class TestGarbage {
    public static void main(String[] args) throws InterruptedException {

        for (int i = 0; i < 20; i++) {
            TestGarbage ti = new TestGarbage();
            // System.out.println(ti);
        }

        for (int i = 0; i < 7; i++) {
            // Nullifying the reference variable
            TestGarbage ti = null;

            // requesting JVM for running Garbage Collector
            System.gc();
            // Runtime.getRuntime().gc();
        }

    }

    @Override
    // finalize method is called on object once
    // before garbage collecting it
    protected void finalize() throws Throwable {
        System.out.println("Garbage collector called");
        System.out.println("Object garbage collected : " + this);
    }
}

But, the interviewer was not satisfied at all, he asked me that there is no guarantee that above method will definitely run the garbage collector.

So, how can it be solved? I have an understanding that after Java 8 or up versions, the programmers don’t need to take care of destroying the objects that are out of use. The garbage collector takes care of it.

1 answer

  • answered 2019-10-08 03:50 Ankur

    So, after your input I made a few changes:

    public class TestGarbage {
        public TestGarbage(int i) {
            // TODO Auto-generated constructor stub
        }
    
        public static void main(String[] args) throws InterruptedException {
    
            TestGarbage[] testGarbage = new TestGarbage[20];
    
            for (int i = 0; i < 20; i++) {
    
                testGarbage[i] = new TestGarbage(i);
                //System.out.println(testGarbage[i]);
            }
    
            for (int i = 0; i < 7; i++) {
                // Nullifying the reference variable
                testGarbage[i] = null;
    
                // requesting JVM for running Garbage Collector
                System.gc();
                // Runtime.getRuntime().gc();
            }
    
        }
    
        @Override
        // finalize method is called on object once
        // before garbage collecting it
        protected void finalize() throws Throwable {
            System.out.println("Garbage collector called");
            System.out.println("Object garbage collected : " + this);
        }
    }
    

    But, sometimes garbage collector is called 7 times in the output-

    Garbage collector called
    Object garbage collected : TestGarbage@c7b3681
    Garbage collector called
    Object garbage collected : TestGarbage@2bcc97e
    Garbage collector called
    Object garbage collected : TestGarbage@6835df1
    Garbage collector called
    Object garbage collected : TestGarbage@4381c345
    Garbage collector called
    Object garbage collected : TestGarbage@169b2a8a
    Garbage collector called
    Object garbage collected : TestGarbage@7208e61e
    Garbage collector called
    Object garbage collected : TestGarbage@6ef8517b
    

    and some times its only calling only two times-

    Garbage collector called Object garbage collected : TestGarbage@c7b3681 Garbage collector called Object garbage collected : TestGarbage@2bcc97e