C# 强制GC垃圾回收

在需要调用垃圾回收的地方调用ClearMemory()

public static void ClearMemory()
{
    GC.Collect();
    GC.WaitForPendingFinalizers();
    if (Environment.OSVersion.Platform == PlatformID.Win32NT)
    {
        SetProcessWorkingSetSize(Process.GetCurrentProcess().Handle, -1, -1);
    }
}

[DllImport("kernel32.dll")]
public static extern int SetProcessWorkingSetSize(IntPtr process, int minSize, int maxSize);
posted @ 2020-11-18 16:33  柠檬山竹大西瓜  阅读(1254)  评论(0编辑  收藏  举报