Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #30486

Re: finalize() not called.

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!nntp.club.cc.cmu.edu!feeder.erje.net!eu.feeder.erje.net!newsfeed.fsmpi.rwth-aachen.de!news-1.dfn.de!news.dfn.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From Robert Klemme <shortcutter@googlemail.com>
Newsgroups comp.lang.java.programmer
Subject Re: finalize() not called.
Date Thu, 04 Dec 2014 20:37:50 +0100
Lines 29
Message-ID <cebrggFjgvoU1@mid.individual.net> (permalink)
References <850b3ab9-57ea-490c-a165-943183169841@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding 7bit
X-Trace individual.net UHnwT6cxn2axUT7fOAmBWw4RAbBVyu05PhELMtH/Em7KTkH1k=
Cancel-Lock sha1:T9ZvyJ3/eWWtAywtLJIPfJRTfTA=
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.2.0
In-Reply-To <850b3ab9-57ea-490c-a165-943183169841@googlegroups.com>
Xref csiph.com comp.lang.java.programmer:30486

Show key headers only | View raw


On 04.12.2014 17:56, 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.
> Could you please explain the behavior.

That method is only ever invoked *if* "garbage collection determines 
that there are no more references to the object".

http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#finalize()

First of all invoking System.gc() is not a guarantee that GC is actually 
done.

http://docs.oracle.com/javase/7/docs/api/java/lang/System.html#gc()

Second, even if GC is running there are no guarantees that all objects 
are collected that may be collected.

If you need to do cleanup at a particular point then the usual pattern 
in Java involves a try finally block.  More recent versions of Java 
provide additional means:

http://www.theserverside.com/tutorial/OCPJP-Exam-Objective-How-to-Use-Try-With-Resources-to-Clean-Up-Closeable-Resources

Kind regards

	robert

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


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