• 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

finalize

 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Ranch Hand
Posts: 824
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1252
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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>
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
d finalize() method as u put it in ur program works on my system )
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How i will know that finalize block is executing because the "Going for GC" out put is not printing.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I used eclipse and Java 5.0.
 
Sujittt Tripathyrr
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic