finalize problem
i overloaded the finalize method and the called the garbage collector throught the gc() method but the finalize method wa nt run,
isn't this method supposed to run when garbage collection is done
heres the code
class test123
{
publicstaticvoid main(String[] args)
{
int y =10;
int k=new Integer(45);
long t;
Runtime r = Runtime.getRuntime();
t = r.freeMemory();
System.out.println("the free space" + t);
r.gc();
t = r.freeMemory();
System.out.println("the free space" + t);
}
protectedvoid finalize()throws Throwable
{
System.out.println("hello");
}
}
pl reply
no. under normal circumstances you should never be invoking System.gc. the system will decide whether to run a gc when it needs to. you can call it, but all you're doing is suggesting that a gc will happen, it might not actually happen. and the behaviour of finalize isn't to be relied upon either, since you have no real control over or insight into when garbage collection will occur
also, isn't finalized invoked only when an instance of the object is being removed from memory? In that program there is no instance of test123 being removed from memory so the finalize() is never invoked.
> also, isn't finalized invoked only when an instance
> of the object is being removed from memory? In that
> program there is no instance of test123 being removed
> from memory so the finalize() is never invoked.
Well spotted. I didn't even bother reading his code because I'm so "bl00dy clever". That'll teach me
> also, isn't finalized invoked only when an instance
> of the object is being removed from memory?
You can only be certain that it will run before the storage for the object is reused.
There are a whole host of problems associated with finalizers and you should not generally use them (I won't go so far as to say never because there are a couple of legitimate uses)
Providing an explicit termination method instead is far easier and less likely to cause you problems (for example the close method of InputStream)
> > also, isn't finalized invoked only when an
> instance
> > of the object is being removed from memory?
>
> You can only be certain that it will run before the
> storage for the object is reused.
>
It will be called before the object is released back into the java heap.
> > You can only be certain that it will run before
> the
> > storage for the object is reused.
> >
>
> It will be called before the object is released back
> into the java heap.
"The Java programming language does not specify how soon a finalizer will be invoked, except to say that it will happen before the storage for the object is reused."
JLS - 12.6 Finalization of Class Instances
> > > You can only be certain that it will run before the
> > > storage for the object is reused.
> > >
> >
> > It will be called before the object is released back
> > into the java heap.
>
> "The Java programming language does not specify how
> soon a finalizer will be invoked, except to say that
> it will happen before the storage for the object is
> reused."
>
> JLS - 12.6 Finalization of Class Instances
Not sure where you got that quote from. However the last sentence of the first paragraph for the link below says this.....
"Before the storage for an object is reclaimed by the garbage collector, the Java virtual machine will invoke the finalizer of that object."
http://java.sun.com/docs/books/jls/third_edition/html/execution.html#12.6
> Not sure where you got that quote from. Third paragraph
> > Not sure where you got that quote from. > > Third paragraphOk. Contradicts the first paragraph though.