Garbage Collection does not reduce current memory usage - in release mode. Why?

前端 未结 10 992
渐次进展
渐次进展 2020-12-06 14:18

I built a quick program that needed to loop through an enormous log file (a couple of million records) and find various bits and pieces from inside. Because the volume of da

10条回答
  •  南笙
    南笙 (楼主)
    2020-12-06 15:16

    There is a way. I found this article, it is in spanish by sure you understand the code, it works, I tested [http://www.nerdcoder.com/c-net-forzar-liberacion-de-memoria-de-nuestras-aplicaciones/][1]

    [DllImport("kernel32.dll", EntryPoint = "SetProcessWorkingSetSize", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
    private static extern int SetProcessWorkingSetSize(IntPtr process, int minimumWorkingSetSize, int maximumWorkingSetSize);
    public static void alzheimer()
    {
        GC.Collect();
        GC.WaitForPendingFinalizers();
        SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1);
    }
    

提交回复
热议问题