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

how garbage collection works

 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


 
Akshay Rawal
Ranch Hand
Posts: 99
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
after repeated execution i got output...i think garbage collector don't have any pattern to collect unreferenced object
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As I understand it, System.gc() doesn't FORCE the gc to run. It's just a suggestion to the JVM saying "hey, I think now might be an OK time to run...you know...if you have nothing else to do".

But the JVM is free to ignore your request. if it has 95% of its memory free, it may say "meh...I don't need to run it now, but thanks for the suggestion", and ignore your request.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And as your program exits straight after the call to System.gc(), it makes it even less likely that the garbage collector will run.
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Akshay,

In most of my runs I get the output.

When does Java's garbage collection free a memory allocation? explains it nicely -

When you set the reference of any object to null, it becomes available for garbage collection. It still occupies the memory until the garbage collector actually runs. There are no guarantees regarding when GC will run except that it will definitely run and reclaim memory from unreachable objects before an OutOfMemoryException is thrown.

You can call System.gc() to request garbage collection, however, that's what it is - a request. It is upto GC's discretion to run.



Regards,
Dan
 
Ranch Hand
Posts: 443
3
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your right System.gc () doesn't guarantee a gc but I've observed one everytime I asked ;-). To see if a gc is forced turn on command line gc logging and you should see the GC (or not).

After a object is Gc'ed and deemed unretained its finializer is added to a queue for running (different thread) and indeed and can be saved from GC via a PhantomReference which was on the exam when I did it.

If you believe your System.gc is working you should add this after the Sytem.gc (as your program terminates immediately after) ...



.. if your codes point is to observe finalize in action.

Again its not strictly guaranteed but your code is racing the Finalizer thread which is a daemon thread and will die on main thread exit, basically the spec says finalize happens some time after.

Secret life of finalizer


PS checkedOut is written in one thread and read from another on finalize so it should be marked volatile or equivalent.





 
Dan Drillich
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Chris,

Adding this line -



And I get the error message -



Regards,
Dan
 
Bartender
Posts: 2237
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The error message explains everything.
runFinalization() is not a static method and you are trying to call it as one.

You need an instance of Runtime class to call this method.
You can get it by calling Runtime.getRuntime() method.
 
Chris Hurst
Ranch Hand
Posts: 443
3
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yep, my bad, I meant to post a link to the method but pasted it as code (Pawel is correct)...



There also was a method on runtime to tun on /off running finalizers on exit but it's been deprecated as unsafe, you could also play withPhantomreferences if you are interested in the topic.
 
reply
    Bookmark Topic Watch Topic
  • New Topic