Deleting a class object in java

自作多情 提交于 2020-01-01 18:02:05

问题


I have a class named Point as below:

public class Point {
    public int x;
    public int y;

    public Point(int X, int Y){
        x = X;
        y = Y;
    }

    public double Distance(Point p){
        return sqrt(((this.x - p.x) * (this.x - p.x)) + ((this.y - p.y) * (this.y - p.y)));
    }

    protected void finalize()
    {
        System.out.println( "One point has been destroyed.");
    } 
}

I have an object from this class named p as below:

Point p = new Point(50,50);

I want to delete this object, I searched how to do it, the only solution I found was:

p = null;

But the finalize method of Point didn't work after I did it. What can I do?


回答1:


After you do p = null; the last reference of your point is deleted and the garbage collector collects the instance now because there is no reference to this instance. If you call System.gc(); the garbage collector will recycle unused objects and invoke the finalize methods of this objects.

    Point p = new Point(50,50);
    p = null;
    System.gc();

Output: One point has been destroyed.




回答2:


You cannot delete object in java, thats the work of GC (Garbage Collector) , which finds and deletes unreferenced instance variables. What that means is variables that are no longer pointed or referenced to , which means they have now no way of being called over. Hence when you do this p = null; , you assign a null to the reference variable holding reference to Point object. Hence now Point object that was pointed by p is garbage collectible.

Also according to javadoc for finalize() method,

Called by the garbage collector on an object when garbage collection 
determines that there are no more references to the object. 
A subclass overrides the finalize method
to dispose of system resources or to perform other cleanup.

But there is no guarantee of calling of finalize() method, as GC is not guaranteed to run at a specific time (deterministic time).




回答3:


An object CAN BE DESTROYED when threre are no remaining pointer to this object.

It is removed by the garbage collector at random time. You can call System.gc() but it is not recommended. The system should be the one to be able to manage the memory.




回答4:


In java there is no such thing as "delete an object". Objects are automatically removed by the garbage collector when it finds that there is no reference to it. The finalize() method is also automatically called by the garbage collector before the object is permanently removed from the memory. You have no control over when this happens.




回答5:


You do not need to destroy the object, the garbage collector will do it for you.




回答6:


Actually p = null will only make the Object lost reference in java heap. But, the Object P still active. If you use System.gc() you will be able to clean all active Objects include it's reference in java heap. So, i recommend using System.gc() after doing p = null




回答7:


GC reclaims object, when an object no longer referenced becomes claimable candidate. For basic app one cannot be sure if GC runs with in process lifetime. Thus here is a small example for testing theory:

public class Test {
    static class Sample {
        @Override
        protected void finalize() throws Throwable {
            System.out.print(this + " The END");    
        } 
    }
    public static void main(String...args) throws Exception {
        // Extra care 
        Runtime.getRuntime().runFinalization();
        Sample sample = new Sample(); sample = null; // Object no longer referenced
        System.gc(); // Ensures FULL GC before application exits
        Thread.sleep(1000); // Relax and see finalization out log
    }
}



回答8:


If there are no methods calling point and its not because of memory management issues you leave it to the garbage collector or call it explicitly. Otherwise mention why you want to delete it so that other ways are suggested.



来源:https://stackoverflow.com/questions/24692682/deleting-a-class-object-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!