Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #30487
| Path | csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!us.feeder.erje.net!feeder.erje.net!eu.feeder.erje.net!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail |
|---|---|
| From | v_borchert@despammed.com (Volker Borchert) |
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: finalize() not called. |
| Date | 4 Dec 2014 19:05:23 GMT |
| Organization | Private site at Eddersheim, Germany |
| Lines | 26 |
| Distribution | world |
| Message-ID | <m5qb9j$45d$1@Gaia.teknon.de> (permalink) |
| References | <850b3ab9-57ea-490c-a165-943183169841@googlegroups.com> |
| X-Trace | individual.net R9P6ch1LilxEMe2g8AJs+AzExTQeLWqOiyYfkYEEvM9v/XZJr0 |
| Cancel-Lock | sha1:I84CehEXTry2nJIYlCAKd8JDmG8= |
| Xref | csiph.com comp.lang.java.programmer:30487 |
Show key headers only | View raw
somenathpal@gmail.com wrote:
> Now when System.gc() is called I expect finalize() to be called.
> But it does not get called. But when I uncomment the following
> System.out.println("Calling GC"); the finalize() gets called.
> I am not able to understand this behavior. I was expecting
> finalize() method yo be called.
RTFB (= Read The Fine Book ;-), in this case, "Effective Java"
item #7 (in the 2nd edition) on why finalizers have been a really
bead idea right from the beginning, but for reasons probably just
not yet understood all those years ago.
Basically, there are no guarantees as to when, on which thread, and
whether at all finalizers are called.
If you need to make sure that locks are released or resources freed,
put the release or free into finally blocks. If you are just curious
to know when objects are GCed, or want to do non-urgent bookkeeping
like eventually removing the map entries for GCed keys or values from
a cache, use PhantomReference in cunjonction with ReferenceQueue.
--
"I'm a doctor, not a mechanic." Dr Leonard McCoy <mccoy@ncc1701.starfleet.fed>
"I'm a mechanic, not a doctor." Volker Borchert <v_borchert@despammed.com>
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
finalize() not called. somenathpal@gmail.com - 2014-12-04 08:56 -0800
Re: finalize() not called. Robert Klemme <shortcutter@googlemail.com> - 2014-12-04 20:37 +0100
Re: finalize() not called. v_borchert@despammed.com (Volker Borchert) - 2014-12-04 19:05 +0000
Re: finalize() not called. Joshua Cranmer 🐧 <Pidgeot18@verizon.invalid> - 2014-12-04 14:24 -0600
Re: finalize() not called. Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-12-06 16:07 -0500
Re: finalize() not called. Lew <lewbloch@gmail.com> - 2015-01-04 16:13 -0800
csiph-web