Skip to content

Commit 28ca633

Browse files
committed
Improve LOH memory checks in release buffer tests
Refined UsingMemoryStreamSlimReleaseBuffers test logic to assert LOH memory usage only on the first loop and to return early if LOH memory drops to zero after GC or buffer release. This makes the test more robust and prevents false failures when memory is released as expected.
1 parent 0c67525 commit 28ca633

1 file changed

Lines changed: 13 additions & 0 deletions

File tree

Source/Tst/KZDev.PerfUtils.Memory.Heap.Sequential.UnitTests/UsingMemoryStreamSlimReleaseBuffers.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,20 @@ public void UsingMemoryStreamSlimReleaseBuffers_CopyFullToStream_SettingReleaseM
5959
using MemoryStreamSlim testService = MemoryStreamSlim.Create(options => options.WithZeroBufferBehavior(MemoryStreamSlimZeroBufferOption.OnRelease));
6060
int byteCount = testDataSizes[RandomSource.GetRandomInteger(testDataSizes.Length)];
6161
MemoryTestPrep.FillStreamAndArrayWithRandomBytes(testService, byteCount, testSegmentSize);
62+
// Make sure that the LOH memory usage is non-zero on the first loop.
63+
if (testLoop > 0)
64+
continue;
65+
testMetricsMonitor.LohSize.Should().BeGreaterThan(0);
6266
}
6367

6468
// Clean the GC and capture the current LOH memory usage
6569
GC.Collect(GC.MaxGeneration);
6670
double runCompleteLohMemory = testMetricsMonitor.LohSize;
71+
if (runCompleteLohMemory == 0)
72+
{
73+
// We know that LOH did go above zero during the test, so if it is back to zero now then we can just end the test since the memory was released as expected.
74+
return;
75+
}
6776
// Tell the MemoryStreamSlim to release its memory buffers
6877
MemoryStreamSlim.ReleaseMemoryBuffers();
6978

@@ -74,6 +83,10 @@ public void UsingMemoryStreamSlimReleaseBuffers_CopyFullToStream_SettingReleaseM
7483
GC.Collect(GC.MaxGeneration);
7584
// We should drop LOH memory by at least 1/2 (it should actually drop to zero, but we can't guarantee that)
7685
double newLohMemory = testMetricsMonitor.LohSize;
86+
if (newLohMemory == 0)
87+
{
88+
return;
89+
}
7790
newLohMemory.Should().BeLessThan(runCompleteLohMemory / 2);
7891
}
7992
//--------------------------------------------------------------------------------

0 commit comments

Comments
 (0)