4 ms·
You actually have a lot of control over the C# GC. One common technique is to suspend GC before entering a tight loop and then manually running GC when your pro
by mystified5016 2y ago
You actually have a lot of control over the C# GC. One common technique is to suspend GC before entering a tight loop and then manually running GC when your program has downtime. For instance, you might pause GC during your game's frame render routine, then unpause in the space between frames. Otherwise, GC runs when it feels like it, which always seems to happen in an inconvenient place that causes visible stutter.
You also have the concept of "pinning" a variable, which prevents GC from moving it around memory (for instance, if you pass a very long-lived pointer out of C#, you might want to pin it so the pointer address is always constant).
So you can suspend and resume GC globally, you can manually run GC at any time, and you have ways of exempting certiain objects from GC.
I don't know offhand if you can GC one specific object manually, but I would be very surprised if you couldn't.
Also, all of the above is considered very bad practice. As a rule of thumb, you are not smarter than the GC and you will do a worse job unless you are very sure of what you're doing.
- Dwedit 2y agoThe thing really needed is a "Collect with timeout" function. Something like "Garbage collect now, but abandon the job in a consistent state if X milliseconds have passed". (Perhaps even some other event. Like "Do GC until a File IO operation finishes")
- kevingadd 2y agoI think this is referred to as Incremental GC, and runtimes like Spidermonkey, V8 and Lua have it. I'm not sure whether there's any hope of getting it for .NET or whether Java has one though.