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
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<T>.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;

34
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();
}

Loading…
Cancel
Save