This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

finalize

 
Sujittt Tripathyrr
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I know the concept of finalize in java but i want a program to simulate the concept.

Thanks in advance.

 
Sunil Kumar Gupta
Ranch Hand
Posts: 824
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
May be this can help you

finalize() example
 
Sujittt Tripathyrr
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class Finalize
{

public void finalize()
{
System.out.println("Going for GC");
}


public static void main(String s[])
{
Finalize f1=new Finalize();
f1=null;
System.gc();

}
}

why here the finalize block is not executing.

Thanks
 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sujittt Tripathyrr:
public class Finalize
{

public void finalize()
{
System.out.println("Going for GC");
}


public static void main(String s[])
{
Finalize f1=new Finalize();
f1=null;
System.gc();

}
}

why here the finalize block is not executing.

Thanks


Well,

Don't mind but, have you ever tried to run this program.. It is running fine at my PC>
 
Akin Ola
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
d finalize() method as u put it in ur program works on my system )
 
Vijay Raj
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.gc() does not guarantee that the finalize() method will run. It all depends upon the underlying JVM implementation. So, it may work in some computers and not in others. It also depends upon how much memory has been used up by the JVM. If very less memory is left, then garbage collection 'can' be started by the JVM itself.

Please point out if I am wrong.

regards,
vijay.
 
Sujittt Tripathyrr
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How i will know that finalize block is executing because the "Going for GC" out put is not printing.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot guarantee that the object will be garbage collected, if it is, then finalize() will be called. System.GC may or may not cause garbage collection, usually it does - but it is not certain. Sometimes it helps to call System.GC more than once - but you can never be certain that it has the effect you want.

By the way: the finalize() method in this code is public (it has been widened from protected access). If a subclass of this class wanted to use a protected finalize() method (as in Object's contract) it cannot do so.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Akin Ola:
d finalize() method as u put it in ur program works on my system )


Please Use Real Words in this forum, unless you want to perceived as illiterate. Thanks.

(Yes, I corrected a spelling mistake of my own )
[ July 19, 2006: Message edited by: Barry Gaunt ]
 
wise owen
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Try this code. It prints out "Going for GC".
 
Sujittt Tripathyrr
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I executed the code in JDeveloper and no output it is displaying.
 
wise owen
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I used eclipse and Java 5.0.
 
Sujittt Tripathyrr
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi wise

Just now i check in command prompt it is working fine and also printing "Going for GC".

Thanks for help.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic