how can I disable GC for a third of a second

  • Thread starter Thread starter not_a_commie
  • Start date Start date
N

not_a_commie

I have a tight time constraint on a small chunk of processing. It
takes about a third of a second to run. Occasionally, this function
will get stalled for (what I can only guess) is a garbage collection
(GC) run. Is there any way I can disable the GC for that brief third
of a second?
 
You can't. And to be honest, if this is a requirement, then .NET isn't
a platform you should be doing this work in. You should do it in an
unmanaged piece of code, like C++.
 
Are you sure it is the GC kicking in? Is your process attempt to access any
files, databases etc, because if it is these could be causing your random
stall. Also IMO a third of a second is an age for any computer.

If you search on Google you will find lots of good articles about garbage
collection in .Net - all the articles will tell you that garbage collection
is non deterministic, you can't predict or determine when it will run and you
can't control in the manner you require. What can be said about garbage
collection is that it will only run when system resources become scarce, i.e.
when the amount of memory available for the process starts to run low it will
attempt to free any unwanted memory.

If it is GC causing the stall then you need to analyse your application from
memory point of view, as in what objects are being created, are they being
used efficiently and disposed of correctly etc. This won't remove the
possibility completely that the GC will run during your critical phase but it
can reduce it.

HTH

Ollie Riches
 
I would only add that under heavy CPU usage, the GC generally won't run.
I've had apps that wouldn't GC because the CPU was pegged and I've had to
force GC periodically to avoid paging problems.

I suspect if it's happening regularly for that piece of code, it likely
isn't the GC, but something else. You might try downloading nProf (a free
open source .NET profiler,
http://www.mertner.com/confluence/display/NProf/Home) and running your code
under that to see what's using up the time. I've found it extremely helpful.
 
not_a_commie said:
I have a tight time constraint on a small chunk of processing. It
takes about a third of a second to run. Occasionally, this function
will get stalled for (what I can only guess) is a garbage collection
(GC) run. Is there any way I can disable the GC for that brief third
of a second?

If you run the GC before starting that processing, it's extremely unlikely
that you would need the GC again in such a short time.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top