Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Garbage Collector Priority  RSS feed

 
Abhijit Choudhury
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

To invoke the garbage collector explicitly we use Runtime.getRuntime().gc()
But what is the priority of garbage collector thread?

I have heard it runs on a low priority if there are multiple threads running which are creating a lot objects simultaneously then even if i invoke GC thread it doesn't get so much CPU time and objects doesn't get collected and sometimes get out of memory exception.

So is there a way to increase garbage collector thread priority so that it runs and collects all those objects which are going out of scope while the program is still running with all rest of the threads active?
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Calling gc() does not explicitly execute the garbage collector. It suggests to the JVM that now would be a good time for some GC. The JVM may do a full GC, a bit of GC, or no GC at all, in response.

Some of your concerns are unfounded. Particularly, you will never get an OutOfMemoryError when there exist objects that could be garbage collected, and would free enough memory to allow execution to continue.

Tweaking the garbage collector is possible, to achieve better performance (for various definitions of "better"). However, it is an advanced topic (one that I am not all that expert in ). Only very rarely does it involve calling gc() - that's usually pointless. There is no direct way to change the GC thread priority, but you can adjust a variety of settings.

From the questions you are asking, I detect that you probably aren't at the stage where you should be worrying about it. For 99% of simple Java applications, the GC just happens and all is fine.
[ January 29, 2008: Message edited by: Peter Chase ]
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IMHO, this is not an appropriate forum.
This link has good information on tuning garbage collection, if you are interested.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!