Skip to content

Commit

Permalink
workaround for #2001 / dotnet/runtime#65466
Browse files Browse the repository at this point in the history
  • Loading branch information
antonfirsov committed Feb 21, 2022
1 parent c059656 commit 854ea5d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ internal UniformUnmanagedMemoryPoolMemoryAllocator(
this.nonPoolAllocator = new UnmanagedMemoryAllocator(unmanagedBufferSizeInBytes);
}

// This delegate allows overriding the method returning the available system memory,
// so we can test our workaround for https://github.com/dotnet/runtime/issues/65466
internal static Func<long> GetTotalAvailableMemoryBytes { get; set; } = () => GC.GetGCMemoryInfo().TotalAvailableMemoryBytes;

/// <inheritdoc />
protected internal override int GetBufferCapacityInBytes() => this.poolBufferSizeInBytes;

Expand Down Expand Up @@ -152,8 +156,13 @@ private static long GetDefaultMaxPoolSizeBytes()
// https://github.com/dotnet/runtime/issues/55126#issuecomment-876779327
if (Environment.Is64BitProcess || !RuntimeInformation.FrameworkDescription.StartsWith(".NET 5.0"))
{
GCMemoryInfo info = GC.GetGCMemoryInfo();
return info.TotalAvailableMemoryBytes / 8;
long total = GetTotalAvailableMemoryBytes();

// Workaround for https://github.com/dotnet/runtime/issues/65466
if (total > 0)
{
return total / 8;
}
}
#endif

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,5 +379,18 @@ private static void AllocateSingleAndForget(UniformUnmanagedMemoryPoolMemoryAllo
g1.GetSpan()[0] = 42;
}
}

[Fact]
public void Issue2001_NegativeMemoryReportedByGc()
{
RemoteExecutor.Invoke(RunTest).Dispose();

static void RunTest()
{
// Emulate GC.GetGCMemoryInfo() issue https://github.com/dotnet/runtime/issues/65466
UniformUnmanagedMemoryPoolMemoryAllocator.GetTotalAvailableMemoryBytes = () => -402354176;
_ = MemoryAllocator.Create();
}
}
}
}

0 comments on commit 854ea5d

Please sign in to comment.