Files
1258/JinYuan.Helper/ClearMemoryHelper.cs
T

32 lines
935 B
C#
Raw Normal View History

2026-08-04 18:36:40 +08:00
using System;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
namespace JinYuan.Helper
{
public class ClearMemoryHelper
{
[DllImport("kernel32.dll")]
private static extern bool SetProcessWorkingSetSize(IntPtr proc, int min, int max);
/// <summary>
/// 释放内存
/// </summary>
public static void ClearMemory()
{
Task.Run(async () =>
{
while (true)
{
await Task.Delay(4000).ConfigureAwait(false);
GC.Collect();
GC.WaitForPendingFinalizers();
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
{
SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1);
}
}
});
}
}
}