• Post Reply Bookmark Topic Watch Topic
  • New Topic

Garbage Collection  RSS feed

 
Raja gopal Y
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi...

I Know that Garbage Collection is a low priority thread and runs periodically to reclaim the memory. and We can SUGGEST the GC To Reclaim the memory by using system.gc() / runtime.gc().

Manually We can't FORCE The GC to Reclaim the Mrmory.

I am Confused here abt these two statements, Why the Java Team provides these two methods, even by calling these methods guarantees runs the GC?

Hope i explained Clearly abt my Doubt?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The JVM designers left themselves lots of wiggle room to change and improve their algorithms over time. If you can't rely on the suggestion to GC, you shouldn't be hurt by any future changes. Every new JRE version from Sun has had new GC tricks and tuning. I encourage people to not even think about GC unless they absolutely have to (and I'm not sure how to tell when that is). If you optimize your code for today's GC algorithm it may be awful on the next one.
 
Avisesh Jain
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By using system.gc() in your program you are requesting for a garbage collection, there is no guarantee that garbage collection will take place at the point the program requested it , it is purely dependent on the JVM as to when the garbage collection takes place.
 
Raja gopal Y
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes...Avisesh.. you are right...but my question is if therez no guarantee, and ie completely JVM responsibility, why what's the use of the gc() method?
 
David Ulicny
Ranch Hand
Posts: 724
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read this tip, I think it will clarify your question.
http://www.devx.com/tips/Tip/14847
 
Raja gopal Y
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why this """ short pause in the application's execution?""""
 
Arjun K
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
GC is a VM low priority thread thats invoked by the java virtual machine upon some memory thresholds; not necessary periodically. When the gc kicks in it temporarily suspends all the applications operations for reclaiming the memory.

The main advantage of System.gc() or Runtime.gc()(here both are synonymous).. is that these methods wait for safe point to kick. A wait or sleep or idle ness of an application is a safe point in GC terms; for example if the system is already in a wait stage and has some objects need to be reclaimed then System.gc() or Runtime.gc() works immediately and gc kicks in for freeing up the memory.

If the system is not in the safe point mode presently then it will wait for the immediate or next safe point.

Hope this helps,
Arjun.
 
Raja gopal Y
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks..Very good Information
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I said it's good to try to ignore GC but there are some external things you can think about. If you set your heap size way up you can build more objects in memory. But when you finally run out of room and GC has to run, it might take longer to clean up all this extra stuff. In earlier JVMs you would notice a pause in the application while GC ran and there were articles on how to tune the heap to balance the frequency of GC against the delay. In the latest JVMs GC is incremental and spreads out the work so it's less of a problem. JDK 5 has a neat monitoring console in the sample apps so you can watch memory and threads and such in motion. It has a button to force GC right now using a monitor/debug API that we mortals should not mess with.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!