• Post Reply Bookmark Topic Watch Topic
  • New Topic

garbage collection and speed  RSS feed

 
Ron Price
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, when the garbage collection runs everything stops.
Also, even if you ask the garbage collector to run there
is no guarantee that it will run.
This can be very bad, especially if your writing a time sensitive application.

From what I've been taught there is no way to force the garbage collector to run, but here goes my question anyway.

Does anyone have any links, ideas, comments on how to force the garbage collector run or maybe just ensure that it runs with in some amount of time?
(using a normal JVM, I'm not speaking to java real-time here.)


Please let the ideas fly!
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First response is that, if you have a true real-time application, ordinary Java is an irretrievably wrong choice of language/platform.

Presumably, the reason for wanting to force the garbage collection at a particular time is that you hope this will stop it doing garbage collection at some time soon after, when your time-critical task is running. This is never going to be guaranteed.

Be aware that Java garbage collectors these days are very clever, and there is not just one type of GC. There are different generations of objects, GCd at different times. Short-lived objects are GCd frequently, but these GC runs take very little time.

One reason why you are not given programmatic control over GC is to allow the implementors maximum flexibility; this has helped them to develop much better GC algorithms over the years. A programmatic API, conceived in the early days of Java, would probably have constrained what they could have done, and we wouldn't have such good GC now.

In practice, calling System.gc() a few (e.g. 5) times generally does cause a full GC. But it is not, and won't be, guaranteed. Calling System.gc() is usually wrong, but in your case, calling it during an idle time, before a period of time-critical activity, could be considered worthwhile, I think.

There are lots of options for the GC. You cannot control them once the JVM is started; they are start-up options. You might be interested in the parallel collector, for instance.
 
arulk pillai
Author
Ranch Hand
Posts: 3404
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also it is worth noting that GC runs on a low priority non-daemon thread. As said before you can provide GC options before you start the JVM process.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!