What will happen if developer try to access an object which is garbage collected? More specifically suppose developer has created an object but forget to use it, so at what time and on what basis it will get garbage collected and if it is garbage collected then what will happen if developer try to access it?
-
What language are you talking about? Usually an object is garbage collected, if no reference is pointing to the object anymore. – ssc-hrep3 Nov 05 '17 at 10:09
-
I am talking about java, What if developer want to access the object after it is garbage collected? – Vivek Baranwal Nov 05 '17 at 11:45
-
2You cannot access an object anymore, if you don't have any reference to it. It is only garbage collected, if any reference is removed. So, a developer won't be able to access the object, because he simply does not have a reference to it. See also https://stackoverflow.com/questions/13144899/when-is-the-object-eligible-for-garbage-collection – ssc-hrep3 Nov 05 '17 at 13:03
-
The possibility of such an access is the reason for many bugs (crashes or worse) in languages lacking GC. In pure Java (without native code), this simply can't happen by the very definition of GC: If it's reachable, it's not garbage. – maaartinus Nov 06 '17 at 23:45
1 Answers
What will happen if developer try to access an object which is garbage collected?
It cannot happen.
If a (pure Java) program was able to access an object, then the object would by definition be reachable. If an object is reachable, it cannot be garbage collected.
The caveat is "pure Java". If you do nasty things in native code, you can arrange that Java code attempts to use a reference to an object that no longer exists ... or that has been relocated by the GC. If that happens, the behavior is unspecified, but you are likely to crash the JVM.
Most Java garbage collectors work by finding the objects that are still reachable and copying them to another "space". When that is done, the JVM will start to allocate new objects that overlap the space used by the deleted or relocated objects. A "JVM panic" is a likely (and the best) outcome if an old reference is used. In the worst case, the application might get an unpredictable "wrong" value and proceed without detecting it.

- 698,415
- 94
- 811
- 1,216
-
I don't think that the GC zeros out the memory, see the fourth point in https://shipilev.net/blog/2016/arrays-wisdom-ancients/#_conclusion. – maaartinus Nov 06 '17 at 23:41
-
1) At the very least it depends on the GC and the version. 2) Even if the GC doesn't zero or reinitialize the memory itself, *something* does. The net effect will be the same. Eventually the deleted object will be trashed. And the trashing could happen immediately: `new int[big_number]` followed by random reads. – Stephen C Nov 07 '17 at 00:01
-
2) Agreed concerning the effect (with doubly used memory, anything may happen; also the new object may get corrupted by writes to the old one). 1) Some time ago I searched for some information about GC zeroing memory (which I find interesting as it can run in parallel and out of the critical path) and couldn't find anything besides https://community.oracle.com/thread/1542624?start=0&tstart=0 – maaartinus Nov 07 '17 at 00:50
-
The gory details will all be in the OpenJDK source codebase if you are prepared to dig them out. – Stephen C Nov 07 '17 at 03:12
-
Agreed ..... I have rewritten the footnote. Though I think that a "panic" is the best case. – Stephen C Nov 07 '17 at 10:13