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);

Published by

风君子

独自遨游何稽首 揭天掀地慰生平

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注