• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

About finalize()

 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wrote this piece of code and ran it:

package com.nik.practice;

import java.io.IOException;

public final class StrTest {
static String st;
public static void main(String[] args) {
StrTest t = new StrTest();
st = "first";
st = null;//the object is made eligible for garbage collection here
try{
t.finalize();
}catch(Throwable e){
}
System.gc();
System.out.println("st is "+st);
}

protected void finalize(){
st = "first";//here the object is again made alive
}
}

In above I call finalize explicitly and in this case the object is properly referenced again.So "first" is printed as expected.

Now I just commented the call to finalize and ran the code again:
i.e I did

try{
//t.finalize();
}catch(Throwable e){
}

This time though null is printed.I would like to know why because I have overridden the finalize method.And as per the JVM spec when System.gc is called an Object's finalize() is called.Why then doesn't the object get referenced again?I am I missing some important point?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Nikhil,

There are a number of things in your code that are not correct, or do not do what you think they are doing. For example this:

Which object do you think is made eligible for garbage collection here?

And this:

Which object do you think is "again made alive"? Note: Once an object is made eligible for garbage collection, it is impossible to "make it alive" again.

Note that the variable t still points to an instance of StrTest on the line you are doing System.gc(), so that object isn't eligible for garbage collection at that point.
[ November 21, 2006: Message edited by: Jesper Young ]
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As Jesper mentioned, the StrTest object is still referred to by the "t" variable that is still in scope on the gc() call, so the StrTest object will not be garbaged collected (or finalized).

As for how an object can "again made alive", it's quite simple. You just need to have the object be referenceable again. There are numerous way to do this -- one is similar to what you've done -- you can make the object referenced from a static context.



Of course, you can only do this once. If the object is eligible for GC again, it will be just collected. The finalize() method is only called once by the GC.

Henry
 
Nikhil Sun
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for the first post riddled with errors.It was posted in a hurry.My doubt is cleared.

public final class StrTest {
String st = "first";
static StrTest t;
public static void main(String[] args) {
t = new StrTest();
t = null;//Object is eligible for garbage collection
try{
t.finalize();
}catch(Throwable e){
}
System.gc();
System.out.println("string is "+t.st);
}

protected void finalize(){
t = new StrTest();
t.st = "second";
}
}

I ran the following code first as it is and a second time by commenting the finalize() call:

try{
//t.finalize();
}catch(Throwable e){
}

i just wanted to see if the Garbage collector calls the overridden finalize method for this object which creates a reference to the 'dead' object.

Thanks guys
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Explicitly calling finalize() is just like any other method call. No special garbage collection magic occurs. It will just execute whatever code is in finalize().

The finalize() magic only occurs when the JVM calls finalize() for you, as part of its preparation for garbage-collecting an eligible object.

As a last point, the finalize() mechanism is pretty worthless, 99.9% of the time. It very rarely does what you want. I occasionally find it useful to print debug trace on finalisation, and that's about all. The various subclasses of java.lang.ref.Reference are much more useful.
 
Why am I so drawn to cherry pie? I can't seem to stop. Save me tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic