Browse Source

fix trimming

af/UniformUnmanagedMemoryPoolMemoryAllocator-02-MemoryGuards
Anton Firszov 5 years ago
parent
commit
fd94dbfb31
  1. 9
      src/ImageSharp/Memory/Allocators/Internals/UniformUnmanagedMemoryPool.cs
  2. 34
      tests/ImageSharp.Tests.ProfilingSandbox/LoadResizeSaveParallelMemoryStress.cs

9
src/ImageSharp/Memory/Allocators/Internals/UniformUnmanagedMemoryPool.cs

@ -44,6 +44,7 @@ namespace SixLabors.ImageSharp.Memory.Internals
#if NETCORE31COMPATIBLE #if NETCORE31COMPATIBLE
Gen2GcCallback.Register(s => ((UniformUnmanagedMemoryPool)s).Trim(), this); Gen2GcCallback.Register(s => ((UniformUnmanagedMemoryPool)s).Trim(), this);
#endif #endif
this.lastTrimTimestamp = Stopwatch.ElapsedMilliseconds;
} }
} }
@ -262,6 +263,7 @@ namespace SixLabors.ImageSharp.Memory.Internals
// Trim all: // Trim all:
for (int i = this.index; i < buffersLocal.Length && buffersLocal[i] != null; i++) for (int i = this.index; i < buffersLocal.Length && buffersLocal[i] != null; i++)
{ {
buffersLocal[i].Dispose();
buffersLocal[i] = null; buffersLocal[i] = null;
} }
} }
@ -316,13 +318,12 @@ namespace SixLabors.ImageSharp.Memory.Internals
public class TrimSettings public class TrimSettings
{ {
// Trim half of the retained pool buffers every minute. // Trim half of the retained pool buffers every minute.
public int TrimPeriodMilliseconds { get; set; } = 20_000; public int TrimPeriodMilliseconds { get; set; } = 60_000;
public float Rate { get; set; } = 0.5f; public float Rate { get; set; } = 0.5f;
// Be more strict about high pressure threshold than ArrayPool<T>.Shared. // Be more strict about high pressure on 32 bit.
// A 32 bit process can OOM before reaching HighMemoryLoadThresholdBytes. public unsafe float HighPressureThresholdRate { get; set; } = sizeof(IntPtr) == 8 ? 0.9f : 0.6f;
public float HighPressureThresholdRate { get; set; } = 0.5f;
public bool Enabled => this.Rate > 0; public bool Enabled => this.Rate > 0;

34
tests/ImageSharp.Tests.ProfilingSandbox/LoadResizeSaveParallelMemoryStress.cs

@ -232,8 +232,8 @@ namespace SixLabors.ImageSharp.Tests.ProfilingSandbox
[Option('f', "file", Required = false, Default = null)] [Option('f', "file", Required = false, Default = null)]
public string FileOutput { get; set; } public string FileOutput { get; set; }
[Option('t', "trim-time", Required = false, Default = 60)] [Option('t', "trim-time", Required = false, Default = null)]
public int TrimTimeSeconds { get; set; } public int? TrimTimeSeconds { get; set; }
public static CommandLineOptions Parse(string[] args) public static CommandLineOptions Parse(string[] args)
{ {
@ -257,15 +257,27 @@ namespace SixLabors.ImageSharp.Tests.ProfilingSandbox
return ArrayPoolMemoryAllocator.CreateDefault(); return ArrayPoolMemoryAllocator.CreateDefault();
#pragma warning restore CS0618 #pragma warning restore CS0618
case AllocatorKind.Unmanaged: case AllocatorKind.Unmanaged:
return new UniformUnmanagedMemoryPoolMemoryAllocator( if (this.TrimTimeSeconds.HasValue)
1024 * 1024, {
(int)B(this.MaxContiguousPoolBufferMegaBytes), return new UniformUnmanagedMemoryPoolMemoryAllocator(
B(this.MaxPoolSizeMegaBytes), 1024 * 1024,
(int)B(this.MaxCapacityOfUnmanagedBuffersMegaBytes), (int)B(this.MaxContiguousPoolBufferMegaBytes),
new UniformUnmanagedMemoryPool.TrimSettings B(this.MaxPoolSizeMegaBytes),
{ (int)B(this.MaxCapacityOfUnmanagedBuffersMegaBytes),
TrimPeriodMilliseconds = this.TrimTimeSeconds * 100 new UniformUnmanagedMemoryPool.TrimSettings
}); {
TrimPeriodMilliseconds = this.TrimTimeSeconds.Value * 1000
});
}
else
{
return new UniformUnmanagedMemoryPoolMemoryAllocator(
1024 * 1024,
(int)B(this.MaxContiguousPoolBufferMegaBytes),
B(this.MaxPoolSizeMegaBytes),
(int)B(this.MaxCapacityOfUnmanagedBuffersMegaBytes));
}
default: default:
throw new ArgumentOutOfRangeException(); throw new ArgumentOutOfRangeException();
} }

Loading…
Cancel
Save