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.
问题:
回答1:
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.
回答2:
Meanwhile it's possible, there are methods in the GC
class:
GC.TryStartNoGCRegion(...)
and GC.EndNoGCRegion()
.
回答3:
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.
回答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.