简体   繁体   中英

Watching the garbage collector bin an object

I am trying to see when the garbage collector "garbage collects" an object. According to the documentation, the finalize() method is called once when the garbage collector "deletes" an object.

I tried to override the finalise() to see if i can see at what point it is called after i nullify the object but i seem to be waiting indefinately. Should this work?

class Dog{
    ZiggyTest2 z;
    public Dog(ZiggyTest2 z){
        this.z = z;
    }

    protected void finalize() throws Throwable {
        try {
            synchronized(z){
                System.out.println("Garbage collected");
                z.notify();
            }
        } finally {
            super.finalize();
        }
    }
}

And the main class:

class ZiggyTest2{

    public static void main(String[] args){

    ZiggyTest2 z = new ZiggyTest2();        
    Dog dog = new Dog(z);   

        synchronized(z){
            try{
                dog = null;
                z.wait();
            }catch(InterruptedException ie){
                System.out.println("Interrupted");
            }           
        }
    }   
}

What i want to do is to see the finalise() method get called after i nullify the Dog object. This is why i put the notify() statement inside the finalize() method. It is not working in that it just keeps waiting..

Edit

Thanks guys. I got it to work after i modified ZiggyTest2 to add System.gc();

class ZiggyTest2{

    public static void main(String[] args){

    ZiggyTest2 z = new ZiggyTest2();        
    Dog dog = new Dog(z);   

        synchronized(z){
            try{
                dog = null;
                System.gc();
                z.wait();
            }catch(InterruptedException ie){
                System.out.println("Interrupted");
            }           
        }
    }   
}

output:

C:\>java ZiggyTest2
Garbage collected

设置dog = null后,尝试添加System.gc()

The garbage collector only runs when it needs to. If your program doesn't need to perform a GC it can run all day without one. You can call System.gc() to try and trigger a GC.

Note: if you have a finalize method the gc doesn't wait for the finalize methods to be called in a background thread so the object may exist until the GC AFTER finalize has been called.

Well, first of all there's no guarantee as when the garbage collector runs and what will be collected. Most likely the collector doesn't run in your example since there's plenty of free memory left. You could use System.gc() but there's no guarantee that this will actually trigger the collector, it is just a hint to the JVM that you'd like it to run the garbage collector.

Additionally, finalize() will only be run once per instance, ie when finalize() prevents the object to be collected any further garbage collection run will not call finalize() again, but it might collect the object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM