• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Need help on this piece of code about GC and finalize()

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I tried to run this piece of code:
public class Test{

public static void main(string arg[]){
Object obj = buildTest(3);
System.gc();
System.out.println("Existing");
}

public static test buildTest(int n){
Test t = null;
for (int i = 0; i < n; i++){
t = new Test(i);
}
return t;
}
string name;
public Test(int n){
name = "Number " + n;
}

public void finalize(){
System.out.print("finalize " + name);
}
}

The output I got was:
finalize Number 1
finalize Number 0
Existing

My questions is, why it does not print out "finalize Number 2"? If I pass 4 to buildTest in main(), it will not print "finalize Number 3" as well... Can anyone explain this? Is it because of the first line "Test t = null;" in buildTest()?
Any help is appreciated!!

[ June 09, 2006: Message edited by: Jay Cat ]
[ June 09, 2006: Message edited by: Jay Cat ]
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Object obj = buildTest(3);
The buildTest(3) returns the object ("Number 2") to caller and assign to variable obj.
 
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's because obj is referring to an Test object. It keeps referring to it all the way until the left brace "}" in main () is reached.

But setting obj to null as follows:

public class Test{
public static void main(String arg[]){
Object obj = buildTest(3);
obj= null;
System.gc();
System.out.println("Existing");
}
static public Test buildTest(int n){
Test t = null;
for (int i = 0; i < n; i++){
t = new Test(i);
}
return t;
}
String name;
public Test(int n){
name = "Number " + n;
}
private Test(){}

public void finalize(){
System.out.println("finalize " + name);
}
}

gives OUTPUT:

finalize Number 2
finalize Number 1
finalize Number 0
Existing
 
Jayes Herryl
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now I see why...thanks a lot for the explaination!
 
reply
    Bookmark Topic Watch Topic
  • New Topic