1

I'm trying to stop C# from collecting garbage for some time. I found that I can do this using the GC.TryStartNoGCRegion() method. However, this throws this exception:

Unhandled Exception: System.NotImplementedException: The method or operation is not implemented. at System.GC.StartNoGCRegionWorker (System.Int64 totalSize, System.Boolean hasLohSize, System.Int64 lohSize, System.Boolean disallowFullBlockingGC) [0x00000] in <1400ea11d6fd49d4939be686a9549fb4>:0 at System.GC.TryStartNoGCRegion (System.Int64 totalSize, System.Boolean disallowFullBlockingGC) [0x00000] in <1400ea11d6fd49d4939be686a9549fb4>:0 ...

I executed this line of code:

GC.TryStartNoGCRegion(4096, true)
MomoVR
  • 113
  • 1
  • 8
  • 1
    Are you sure you aren't on some flavor of Mono? (for example, Xamarin or Maui) The source code of this method will throw that exact exception if you're on Mono. See the source: https://github.com/mono/mono/blob/main/mcs/class/referencesource/mscorlib/system/gc.cs#L618 (The [normal framework vresion](https://referencesource.microsoft.com/#mscorlib/system/gc.cs,949577f4cff3311d) does not have that check) – Kirk Woll Jul 01 '22 at 22:55

1 Answers1

1

Apparently it is Mono itself causing the error. In the source code GC.TryStartNoGCRegion() throws a System.NotImplementedException.

MomoVR
  • 113
  • 1
  • 8