1- // Copyright (c) Six Labors.
1+ // Copyright (c) Six Labors.
22// Licensed under the Apache License, Version 2.0.
33
44using System ;
55using System . Buffers ;
66using System . Runtime . CompilerServices ;
77using System . Runtime . InteropServices ;
8- using System . Threading ;
98using SixLabors . ImageSharp . Memory . Internals ;
109
1110namespace SixLabors . ImageSharp . Memory
@@ -22,7 +21,7 @@ internal sealed class UniformUnmanagedMemoryPoolMemoryAllocator : MemoryAllocato
2221 private readonly int poolCapacity ;
2322 private readonly UniformUnmanagedMemoryPool . TrimSettings trimSettings ;
2423
25- private UniformUnmanagedMemoryPool pool ;
24+ private readonly UniformUnmanagedMemoryPool pool ;
2625 private readonly UnmanagedMemoryAllocator nonPoolAllocator ;
2726
2827 public UniformUnmanagedMemoryPoolMemoryAllocator ( int ? maxPoolSizeMegabytes )
@@ -74,6 +73,12 @@ internal UniformUnmanagedMemoryPoolMemoryAllocator(
7473 this . nonPoolAllocator = new UnmanagedMemoryAllocator ( unmanagedBufferSizeInBytes ) ;
7574 }
7675
76+ #if NETCOREAPP3_1_OR_GREATER
77+ // This delegate allows overriding the method returning the available system memory,
78+ // so we can test our workaround for https://github.com/dotnet/runtime/issues/65466
79+ internal static Func < long > GetTotalAvailableMemoryBytes { get ; set ; } = ( ) => GC . GetGCMemoryInfo ( ) . TotalAvailableMemoryBytes ;
80+ #endif
81+
7782 /// <inheritdoc />
7883 protected internal override int GetBufferCapacityInBytes ( ) => this . poolBufferSizeInBytes ;
7984
@@ -152,8 +157,13 @@ private static long GetDefaultMaxPoolSizeBytes()
152157 // https://github.com/dotnet/runtime/issues/55126#issuecomment-876779327
153158 if ( Environment . Is64BitProcess || ! RuntimeInformation . FrameworkDescription . StartsWith ( ".NET 5.0" ) )
154159 {
155- GCMemoryInfo info = GC . GetGCMemoryInfo ( ) ;
156- return info . TotalAvailableMemoryBytes / 8 ;
160+ long total = GetTotalAvailableMemoryBytes ( ) ;
161+
162+ // Workaround for https://github.com/dotnet/runtime/issues/65466
163+ if ( total > 0 )
164+ {
165+ return total / 8 ;
166+ }
157167 }
158168#endif
159169
0 commit comments