• 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

when System.gc cannot gaurantee that garbage collection would be run then what is it used for?

 
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I read that System.gc cannot gaurantee that garbage collection would be run. If is it so then why would anyone use System.gc?

thanks
 
Bartender
Posts: 3323
86
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The simple answer is you shouldn't ever need to call gc() - although it probably won't make much difference if you do as it only gives a suggestion to the garbage collector that it may want to run.

It may have been that in the early days of Java this had some useful function (I really can't remember) but certainly these days the garbage collector(s) are much better at knowing when to run than you are. They also have less of a performance hit than they used to so you don't notice them running so much - I do remember occasions in the early days when you would get noticeable pauses when a garbage collection cycle ran. I guess improvement to garbage collection algorithms, general performance improvements and massive increases in raw computing power have sorted that out.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Personally, I have run System.gc() right after generating some complex display that I knew the user will take some time to evaluate.

As Tony said, this was more significant with earlier versions of Java where GC was slower and we were running on slower processors.

Bill
 
Sasparilla and fresh horses for all my men! You will see to it, won't you tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic