From fd94dbfb310af7200f3caa99348ca3d83cd53621 Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Sat, 7 Aug 2021 23:41:32 +0200 Subject: [PATCH] fix trimming --- .../Internals/UniformUnmanagedMemoryPool.cs | 9 ++--- .../LoadResizeSaveParallelMemoryStress.cs | 34 +++++++++++++------ 2 files changed, 28 insertions(+), 15 deletions(-) diff --git a/src/ImageSharp/Memory/Allocators/Internals/UniformUnmanagedMemoryPool.cs b/src/ImageSharp/Memory/Allocators/Internals/UniformUnmanagedMemoryPool.cs index c3e41cf7e..97599b5ab 100644 --- a/src/ImageSharp/Memory/Allocators/Internals/UniformUnmanagedMemoryPool.cs +++ b/src/ImageSharp/Memory/Allocators/Internals/UniformUnmanagedMemoryPool.cs @@ -44,6 +44,7 @@ namespace SixLabors.ImageSharp.Memory.Internals #if NETCORE31COMPATIBLE Gen2GcCallback.Register(s => ((UniformUnmanagedMemoryPool)s).Trim(), this); #endif + this.lastTrimTimestamp = Stopwatch.ElapsedMilliseconds; } } @@ -262,6 +263,7 @@ namespace SixLabors.ImageSharp.Memory.Internals // Trim all: for (int i = this.index; i < buffersLocal.Length && buffersLocal[i] != null; i++) { + buffersLocal[i].Dispose(); buffersLocal[i] = null; } } @@ -316,13 +318,12 @@ namespace SixLabors.ImageSharp.Memory.Internals public class TrimSettings { // 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; - // Be more strict about high pressure threshold than ArrayPool.Shared. - // A 32 bit process can OOM before reaching HighMemoryLoadThresholdBytes. - public float HighPressureThresholdRate { get; set; } = 0.5f; + // Be more strict about high pressure on 32 bit. + public unsafe float HighPressureThresholdRate { get; set; } = sizeof(IntPtr) == 8 ? 0.9f : 0.6f; public bool Enabled => this.Rate > 0; diff --git a/tests/ImageSharp.Tests.ProfilingSandbox/LoadResizeSaveParallelMemoryStress.cs b/tests/ImageSharp.Tests.ProfilingSandbox/LoadResizeSaveParallelMemoryStress.cs index 53960b954..f03a91449 100644 --- a/tests/ImageSharp.Tests.ProfilingSandbox/LoadResizeSaveParallelMemoryStress.cs +++ b/tests/ImageSharp.Tests.ProfilingSandbox/LoadResizeSaveParallelMemoryStress.cs @@ -232,8 +232,8 @@ namespace SixLabors.ImageSharp.Tests.ProfilingSandbox [Option('f', "file", Required = false, Default = null)] public string FileOutput { get; set; } - [Option('t', "trim-time", Required = false, Default = 60)] - public int TrimTimeSeconds { get; set; } + [Option('t', "trim-time", Required = false, Default = null)] + public int? TrimTimeSeconds { get; set; } public static CommandLineOptions Parse(string[] args) { @@ -257,15 +257,27 @@ namespace SixLabors.ImageSharp.Tests.ProfilingSandbox return ArrayPoolMemoryAllocator.CreateDefault(); #pragma warning restore CS0618 case AllocatorKind.Unmanaged: - return new UniformUnmanagedMemoryPoolMemoryAllocator( - 1024 * 1024, - (int)B(this.MaxContiguousPoolBufferMegaBytes), - B(this.MaxPoolSizeMegaBytes), - (int)B(this.MaxCapacityOfUnmanagedBuffersMegaBytes), - new UniformUnmanagedMemoryPool.TrimSettings - { - TrimPeriodMilliseconds = this.TrimTimeSeconds * 100 - }); + if (this.TrimTimeSeconds.HasValue) + { + return new UniformUnmanagedMemoryPoolMemoryAllocator( + 1024 * 1024, + (int)B(this.MaxContiguousPoolBufferMegaBytes), + B(this.MaxPoolSizeMegaBytes), + (int)B(this.MaxCapacityOfUnmanagedBuffersMegaBytes), + 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: throw new ArgumentOutOfRangeException(); }