Skip to content

Commit 693d61a

Browse files
committed
Added TrimMemory to the ResourceLimits (#1854).
1 parent a79d290 commit 693d61a

File tree

4 files changed

+91
-0
lines changed

4 files changed

+91
-0
lines changed

src/Magick.NET.Core/IResourceLimits.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
// Copyright Dirk Lemstra https://github.com/dlemstra/Magick.NET.
22
// Licensed under the Apache License, Version 2.0.
33

4+
#if !NETSTANDARD2_0
5+
using System.Runtime.Versioning;
6+
#endif
7+
48
namespace ImageMagick;
59

610
/// <summary>
@@ -75,4 +79,14 @@ public interface IResourceLimits
7579
/// </summary>
7680
/// <param name="percentage">The percentage to use.</param>
7781
void LimitMemory(Percentage percentage);
82+
83+
/// <summary>
84+
/// Trims unused heap memory and returns it to the operating system, potentially reducing
85+
/// the process memory footprint.
86+
/// </summary>
87+
/// <returns>true if memory was successfully released; otherwise, false.</returns>
88+
#if !NETSTANDARD2_0
89+
[UnsupportedOSPlatform("windows")]
90+
#endif
91+
bool TrimMemory();
7892
}

src/Magick.NET/Native/ResourceLimits.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,7 @@ private static partial class NativeResourceLimits
5858
public static partial void Width_Set(ulong value);
5959

6060
public static partial void LimitMemory(double percentage);
61+
62+
public static partial bool TrimMemory();
6163
}
6264
}

src/Magick.NET/ResourceLimits.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
// Copyright Dirk Lemstra https://github.com/dlemstra/Magick.NET.
22
// Licensed under the Apache License, Version 2.0.
33

4+
using System;
5+
6+
#if !NETSTANDARD2_0
7+
using System.Runtime.Versioning;
8+
#endif
9+
410
namespace ImageMagick;
511

612
/// <summary>
@@ -229,11 +235,38 @@ public static void LimitMemory(Percentage percentage)
229235
NativeResourceLimits.LimitMemory((double)percentage / 100.0);
230236
}
231237

238+
/// <summary>
239+
/// Trims unused heap memory and returns it to the operating system, potentially reducing
240+
/// the process memory footprint.
241+
/// </summary>
242+
/// <returns>true if memory was successfully released; otherwise, false.</returns>
243+
#if !NETSTANDARD2_0
244+
[UnsupportedOSPlatform("windows")]
245+
#endif
246+
public static bool TrimMemory()
247+
{
248+
if (Runtime.IsWindows)
249+
throw new NotSupportedException();
250+
251+
return NativeResourceLimits.TrimMemory();
252+
}
253+
232254
/// <summary>
233255
/// Set the maximum percentage of <see cref="Memory"/> that can be used for image data.
234256
/// This also changes the <see cref="Area"/> limit to four times the number of bytes.
235257
/// </summary>
236258
/// <param name="percentage">The percentage to use.</param>
237259
void IResourceLimits.LimitMemory(Percentage percentage)
238260
=> LimitMemory(percentage);
261+
262+
/// <summary>
263+
/// Trims unused heap memory and returns it to the operating system, potentially reducing
264+
/// the process memory footprint.
265+
/// </summary>
266+
/// <returns>true if memory was successfully released; otherwise, false.</returns>
267+
#if !NETSTANDARD2_0
268+
[UnsupportedOSPlatform("windows")]
269+
#endif
270+
bool IResourceLimits.TrimMemory()
271+
=> TrimMemory();
239272
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright Dirk Lemstra https://github.com/dlemstra/Magick.NET.
2+
// Licensed under the Apache License, Version 2.0.
3+
4+
using System;
5+
using ImageMagick;
6+
using Xunit;
7+
8+
namespace Magick.NET.Tests;
9+
10+
#pragma warning disable CA1416 // Validate platform compatibility
11+
12+
public partial class ResourceLimitsTests
13+
{
14+
[Collection(nameof(IsolatedUnitTest))]
15+
public class TheTrimMemoryMethod
16+
{
17+
[Fact]
18+
public void ShouldThrowExceptionOnWindows()
19+
{
20+
Assert.SkipUnless(Runtime.IsWindows, "Will only throw exception on Windows.");
21+
22+
Assert.Throws<NotSupportedException>(static () => ResourceLimits.TrimMemory());
23+
}
24+
25+
[Fact]
26+
public void ShouldReturnTrueOrFalse()
27+
{
28+
Assert.SkipWhen(Runtime.IsWindows, "Will throw exception on Windows.");
29+
30+
if (Runtime.IsWindows)
31+
return;
32+
33+
var result = ResourceLimits.TrimMemory();
34+
if (result)
35+
Assert.True(result);
36+
else
37+
Assert.False(result);
38+
}
39+
}
40+
}
41+
42+
#pragma warning restore CA1416 // Validate platform compatibility

0 commit comments

Comments
 (0)