• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Garbage Collection ..finalize()..?

 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
Can anyone explain this program,

class Finalizer
{
static int x=0;
static boolean gc=false;
public static void main(String[] args)
{
for(x=0;x<100000;x++)
new Finalizer().finalize();
x=0;
gc=true;
System.gc();
}

protected void finalize()
{
if(gc==true) x++;
if(x%10000 == 0)
System.out.println(gc+" "+x);
}
}

Why it is producing false 0
false 10000
false 20000
.
.
.
false 90000
true 10000. ---> I couln't understand how this is coming in the output?Can anyone explain this pgm?Thanks.
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by kathir vel:
true 10000. ---> I couln't understand how this is coming in the output?Can anyone explain this pgm?Thanks.

The garbage collector calls finalize after the object is eligible for garbage collection. Have a look at the API Documentation for details.
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kathir!

During your for-loop you are creating new Finalizer-objects and invoking the finalize()-method immediately. Calling the finalize()-method on an object does not mean that the object is destroyed or garbage collected - it's just a "normal" method call.

This means:
  • After the for-loop there are thousands of unused objects in the memory, thus eligible for garbage collection.
  • With System.gc() you ask the garbage collector to run (and usually it does so). The garbage collector starts to remove unused objects and calls the finalize method (although you cannot absolutely rely on that!).
  • Every time an object is removed by the garbage collector you get the output "true ###".
  • Since System.gc() is the last command of the application, the garbage collector cannot remove all objects, i.e. it might not call the finalize method of every object (therefore on shouldn't rely on that method...). In your case it typically removes a few thousands and then the application ends.

  • NOTE: you only see "true 10000", but in the background thousands of objects are removed! You can test this by just removing the if (x % 10000 == 0) statement in the finalize()-method.

    Cheers,
    Robert
     
    kathir vel
    Ranch Hand
    Posts: 92
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks Robert.I got it.
     
    Grow a forest with seedballs and this tiny ad:
    the value of filler advertising in 2021
    https://coderanch.com/t/730886/filler-advertising
    reply
      Bookmark Topic Watch Topic
    • New Topic