0

According to my understanding any object without a reference is eligible for garbage collection i.e The class's finalize method should be called while collecting the garbage. I have a class below and a question why the thread objects with no reference in the main class are not calling finalize Method while garbage collection if it did.

package com;

class Customer {
    int amount = 10000;

    synchronized void withdraw(int amount) {
        System.out.println("going to withdraw...");

        if (this.amount < amount) {
            System.out.println("Less balance; waiting for deposit...");
            try {
                wait(10000);
            } catch (Exception e) {
            }
        }
        this.amount -= amount;
        System.out.println("withdraw completed...");
    }

    synchronized void deposit(int amount) {
        System.out.println("going to deposit...");
        this.amount += amount;
        System.out.println("deposit completed... ");
        notify();
    }
    
}

class Test {
    public static void main(String args[]) throws InstantiationException, IllegalAccessException {
        final Customer c = new Customer();

        //Island of Isolating a Thread
        new Thread() {
            public void run() {
//              System.out.println("withdraw thread ");
                c.withdraw(15000);
            }
        }.start();
        //Island of Isolating another Thread
        new Thread() {
            public void run() {
//              System.out.println("deposit thread ");

                c.deposit(10000);
            }
        }.start();
  //attempting to gc manually.
          System.gc();

    }
    
    //Calling a finialize() method to check whether it is garbage collected or not
    protected void finalize() throws Throwable {
        System.out.println("Finalize method called");
    }

    
}

Edit: new Thread() {//blah blah }.start(); is an non-referenced object that gets created heap. i.e It has no stack reference. In theory, any non-Stack referenced object is eligible for garbage collection which in fact applicable to garbage collection. Since they are not Stack referenced, Also It is also considered as Island of Isolation.

Wondering if my understanding is wrong in this regard. Thanks. Please share your views if my thinking is contradictory.

Harish Raj
  • 1,565
  • 3
  • 17
  • 27
  • 1
    Finalizers are called by a background thread *between* GC runs, so it'll take at least 2 GC runs to completely collect an object that has a `finalize()` method. The first GC run *detects* that object is unreachable and queues object for finalization. The second (or third or later) GC will then reclaim the memory if the `finalize()` method has completed by then. – Andreas Aug 10 '20 at 00:43
  • 3
    Your code never creates an instance of class `Test`, so why would you expect that `finalize()` method to be executed? -- Even if you moved the method to the `Customer` class, it wouldn't make a difference, since the instance is still being referenced by local variable `c` when GC runs, so there is no garbage to collect. -- Even if you clear `c` before calling `gc()`, the two threads using it will likely not even have started yet when GC runs (starting a thread is slow), so there is no garbage to collect. -- Even if the threads have ended, program ends immediately, so finalize thread don't run. – Andreas Aug 10 '20 at 00:48
  • great! then I had a wrong understanding that all the non-referenced objects in a class will be eligible for garbage collection. because that is what I kept reading in most of the site. about non-referenced objects = Island of isolation = eligible for garbage collection. – Harish Raj Aug 10 '20 at 01:15
  • 2
    In your mind, what object (instance) is non-referenced (unreachable) at the time GC runs? Explain why you think that object is unreachable. – Andreas Aug 10 '20 at 02:03
  • edited my question again with my understanding. – Harish Raj Aug 16 '20 at 04:48
  • 1
    This has been answered already. – Holger Aug 17 '20 at 12:42

1 Answers1

2

why the thread objects with no reference in the main class are not calling finalize

The garbage collector considers active threads as live, even if nothing else references those threads.

In other words, garbage collection does not make running code suddenly terminate. It would be a major headache if it did.

On top of that, you've only implemented finalize on a class which you never instantiate.

Joni
  • 108,737
  • 14
  • 143
  • 193