13

Is it possible for a programmer to programmatically start/stop the garbage collection in C# programming language? For example, for performance optimization and so on.

Manuel
  • 10,153
  • 5
  • 41
  • 60
Alex
  • 441
  • 1
  • 6
  • 15
  • 3
    Garbage collection is not part of the C# programming language. It's part of the .NET CLR. – John Saunders Jun 19 '10 at 05:44
  • "Kinda." If you never allocate anything, the GC will never collect. Pre-allocate everything you need and then call `GC.Collect()`. – Andrew Russell Jun 19 '10 at 05:57
  • 1
    Maybe also take a look at this answer: http://stackoverflow.com/questions/6005865/prevent-net-garbage-collection-for-short-period-of-time/6005949#6005949 – Gamlor May 16 '11 at 08:40

4 Answers4

11

Since .NET 4.6 it's possible, there are methods in the GCclass:

GC.TryStartNoGCRegion(...) and GC.EndNoGCRegion().

JonP
  • 627
  • 6
  • 13
Pollitzer
  • 1,580
  • 3
  • 18
  • 28
9

Not really. You can give the GC hints via methods like GC.AddMemoryPressure or GC.RemoveMemoryPressure but not stop it outright.

Besides, garbage collection is not that intensive of a process. Programmers very rarely ever worry about it.

casperOne
  • 73,706
  • 19
  • 184
  • 253
Paul Sasik
  • 79,492
  • 20
  • 149
  • 189
  • Oh,I see...How about the SuppressFinalize() method? Does it have a different purpose? – Alex Jun 19 '10 at 05:19
  • SuppressFinalize works on individual objects. Definitely not on the whole of the GC. From MSDN: Objects that implement the IDisposable interface can call this method from the IDisposable.Dispose method to prevent the garbage collector from calling Object.Finalize on an object that does not require it. (So basically it's a very slight performance enhancement.) – Paul Sasik Jun 19 '10 at 05:27
  • I don't see how this would be adding a hint to *prevent* garbage collection, per-se; if you call `GC.AddMemoryPressure` then that will more than likely result in the CLR making *more* garbage collections as it indicates that the memory available to the CLR for the process (virtual or otherwise) is less than it thinks it does. One *could* call `GC.RemoveMemoryPressure` without a corresponding call to `GC.AddMemoryPressure`, in an attempt to trick the CLR into thinking it has more memory than it does, but one would think that the API is resistant (one would hope) to such things. – casperOne May 15 '11 at 01:34
  • @CasperOne: You're right about AddMemoryPressure. I used it in the answer as a point of comparison/contrast to RemoveMemoryPressure. The title of the question refers only to stopping GC but the question refers to both starting and stopping so I mentioned both. Though neither one really starts or stops anything. They're just hints. – Paul Sasik May 15 '11 at 03:33
  • 1
    In the meantime it's possible, see [here](http://stackoverflow.com/a/34900343/3135228) – Pollitzer Jan 20 '16 at 14:47
6

In general, no. And most folks would consider it premature optimization to worry about garbage collection unless you do some profiling and find out that it's really the cause of poor performance in your application.

If you're interested in the nitty gritty of tweaking the GC for performance (or more likely, tweaking your app to improve its performance relative to the GC), MSDN has a pretty decent description of ways to do it.

miguel
  • 73
  • 1
  • 2
  • 7
Malcolm Haar
  • 256
  • 1
  • 4
4

No, there is not. At best you can trigger garbage collection yourself, though this is considered to be a Very Bad Thing since it can interfere with the built in scheduling algorithms used by the GC.

ckramer
  • 9,419
  • 1
  • 24
  • 38