Delete an object in java

@haloamar (157)
India
February 7, 2007 10:47am CST
How to delete an object in java? Is there any way by which i can delete objects on my own. I dont want to depend on GC for deleting object
1 person likes this
7 responses
• Philippines
13 Feb 07
just assign the object to null. for example, Dog dog = new Dog(); dog = null;
• Philippines
13 Feb 07
I would like to expound more on this. The dog = null drops a hint to the garbae collector that it is now fine to free that objects space right away... This i think is a good practice because, those unreferenced objects get collected till memory is needed and then a full gc process starts kicking. This will pause all operations until it is done. By doing this, you are already helping out the gc to do full garbage collection at a later time.
@chachan (81)
• Indonesia
13 Feb 07
Why don't you want to depent on GC? I think it is safer than just delete it manually. But if you want to delete an object manually, you can assign a null value to that object.
@layney (1053)
• Italy
15 Feb 07
It would be safer let GC delete unused objects for us but it is really frustrating about performance. If we could control when delete objects it would be a great enhancement.
• India
1 Mar 07
we can not delete a object directly.but we can force the garbage collection using System.gc() method.
@khusboo (28)
• India
15 Feb 07
sorry. You can not delete an object your self. The garbage collector does it automatically. If you try to delete it, but you never know whether it is deleted or not. The garbage collector does it automatically depending upon the version you use. I think no ones knows exactly after what interval it do deletes the objext.....
• India
1 Mar 07
Hi dear as i suppose there is no chance to delete an object by using java construct. but it is possible by using Java Native Interface concept of java. Thank YOu
• India
15 Feb 07
It is not possible to manually delete an object in Java. Java's GC takes care of the object once it is not more needed. Besides, why would anyone want to delete an object manually when you already have the work done for you.
• India
8 Feb 07
In my nowledge its not possible to delete an obj manually. But u will assisgn null value to that object, it may automatically delete that particular object when no longer use.