Browse Source

Merge branch 'main' into dependabot/github_actions/actions/cache-5

pull/3059/head
James Jackson-South 3 months ago
committed by GitHub
parent
commit
3432d9947d
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 18
      src/ImageSharp/Memory/Allocators/UniformUnmanagedMemoryPoolMemoryAllocator.cs
  2. 13
      tests/ImageSharp.Tests/Memory/Allocators/UniformUnmanagedPoolMemoryAllocatorTests.cs

18
src/ImageSharp/Memory/Allocators/UniformUnmanagedMemoryPoolMemoryAllocator.cs

@ -71,10 +71,6 @@ internal sealed class UniformUnmanagedMemoryPoolMemoryAllocator : MemoryAllocato
this.nonPoolAllocator = new UnmanagedMemoryAllocator(unmanagedBufferSizeInBytes); 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 /> /// <inheritdoc />
protected internal override int GetBufferCapacityInBytes() => this.poolBufferSizeInBytes; protected internal override int GetBufferCapacityInBytes() => this.poolBufferSizeInBytes;
@ -155,20 +151,14 @@ internal sealed class UniformUnmanagedMemoryPoolMemoryAllocator : MemoryAllocato
private static long GetDefaultMaxPoolSizeBytes() private static long GetDefaultMaxPoolSizeBytes()
{ {
// On 64 bit set the pool size to a portion of the total available memory.
// https://github.com/dotnet/runtime/issues/55126#issuecomment-876779327
if (Environment.Is64BitProcess) if (Environment.Is64BitProcess)
{ {
long total = GetTotalAvailableMemoryBytes(); // On 64 bit set the pool size to a portion of the total available memory.
GCMemoryInfo info = GC.GetGCMemoryInfo();
// Workaround for https://github.com/dotnet/runtime/issues/65466 return info.TotalAvailableMemoryBytes / 8;
if (total > 0)
{
return (long)((ulong)total / 8);
}
} }
// Stick to a conservative value of 128 Megabytes on other platforms and 32 bit .NET 5.0: // Stick to a conservative value of 128 Megabytes on 32 bit.
return 128 * OneMegabyte; return 128 * OneMegabyte;
} }
} }

13
tests/ImageSharp.Tests/Memory/Allocators/UniformUnmanagedPoolMemoryAllocatorTests.cs

@ -409,19 +409,6 @@ public class UniformUnmanagedPoolMemoryAllocatorTests
g = null; g = null;
} }
[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();
}
}
[Fact] [Fact]
public void Allocate_OverLimit_ThrowsInvalidMemoryOperationException() public void Allocate_OverLimit_ThrowsInvalidMemoryOperationException()
{ {

Loading…
Cancel
Save