public NoGCRegion(int totalSize, Action actionWhenAllocatedMore) { var succeeded = GC2.TryStartNoGCRegion(totalSize, actionWhenAllocatedMore); if (!succeeded) { throw new InvalidOperationException("Cannot enter NoGCRegion"); } }
public void NoAllocationBeforeLimit() { try { triggered = false; var succeeded = GC2.TryStartNoGCRegion(totalBytes, () => triggered = true); Assert.True(succeeded); Thread.Sleep(sleepTime); Assert.False(triggered); var bytes = new byte[99]; Thread.Sleep(sleepTime); Assert.False(triggered); } finally { GC2.EndNoGCRegion(); triggered = false; } }
public void AllocatingOverLimitTriggersTheAction() { try { triggered = false; var succeeded = GC2.TryStartNoGCRegion(totalBytes, () => triggered = true); Assert.True(succeeded); Assert.False(triggered); for (var i = 0; i < 3; i++) { var k = new byte[totalBytes]; } Thread.Sleep(sleepTime); Assert.True(triggered); } finally { GC2.EndNoGCRegion(); triggered = false; } }
public void Dispose() => GC2.EndNoGCRegion();