How can I delete object manually in Java? Is there any method like obj.delete()
or obj.kill()

- 13,351
- 8
- 59
- 84

- 997
- 12
- 35
-
We cant delete any objects manually!!! JVM will take care – gmhk Jun 24 '11 at 10:14
-
3Your answers are clear, but perhaps not what you were looking for. Is there something more behind the question that you haven't mentioned? Why do you feel the urge to delete objects? Have you got problems with leaks? Please elaborate. – David Heffernan Jun 24 '11 at 10:16
-
The whole point of managed language like Java is that you don't have to delete objects... so if you really need to control this, you ust have very specific needs. So I agree with Heffernan... you need to elaborate a bit more as to why you need to do this. – dm76 Jun 24 '11 at 11:14
4 Answers
There is no real way. Java has a special Garbage Collector which does that for you. Once your object doesn't have any references to it, it will be picked up by the Garbage Collector at some point and destroyed.
From Learning Java Tutorials:
The Garbage Collector
An object is eligible for garbage collection when there are no more references to that object. References that are held in a variable are usually dropped when the variable goes out of scope. Or, you can explicitly drop an object reference by setting the variable to the special value null. Remember that a program can have multiple references to the same object; all references to an object must be dropped before the object is eligible for garbage collection.
There is no way to delete an object. Java's Garbage Collector will do it automatically when an object has no more references.
You can however run the Garbage Collector once you have removed all references to an object by calling System.gc(). Please do read the method's documentation carefully. It only guarantees best-effort to delete all objects marked for deletion.
You should also go through these discussions

- 1
- 1

- 13,351
- 8
- 59
- 84
-
3Gosh.. * awaiting flood of comments about how System.gc() does not 'call' the garbage collector at all * – Mike Kwan Jun 24 '11 at 10:21
-
-
Edited my answer, can you please post me a comprehensive link here on this. I would delete this answer. One of them is this: http://publib.boulder.ibm.com/infocenter/javasdk/v6r0/index.jsp?topic=/com.ibm.java.doc.diagnostics.60/diag/understanding/mm_gc_manual.html – Ozair Kafray Jun 24 '11 at 10:29
One of the main reasons for Java being so popular is the Garbage Collection. You do not have to worry about allocating or deallocating memory. That being said if you want to get rid of an object just set all references to the object to null and once the garbage collector runs the object will be disposed of.
You do have to worry about closing resources such as files, sockets, database connections etc... and for that you should do it in a try/finally block.

- 24,113
- 5
- 60
- 79
-
"... once the garbage collector runs the objects *could be* disposed of." Nothing guarantees that **all** eligible objects are collected on each collection. – Joachim Sauer Jun 24 '11 at 10:14
just assign the null value to it.
Let GC take care of this
obj = null;

- 15,043
- 10
- 42
- 49
-
2
-
2Also, most people now advise against explicit nulling, it's better to just let the JVM deal with the object. – Michael Jun 24 '11 at 10:16
-
1This is not necessary since Java's garbage collector is not so primitive it only accounts for reference count. – Mike Kwan Jun 24 '11 at 10:19