How do I get .NET to garbage collect aggressively?

后端 未结 12 765
情书的邮戳
情书的邮戳 2020-12-02 14:33

I have an application that is used in image processing, and I find myself typically allocating arrays in the 4000x4000 ushort size, as well as the occasional float and the l

12条回答
  •  独厮守ぢ
    2020-12-02 14:53

    The best way to do it is like this article show, it is in spanish, but you sure understand the code. http://www.nerdcoder.com/c-net-forzar-liberacion-de-memoria-de-nuestras-aplicaciones/

    Here the code in case link get brock

    using System.Runtime.InteropServices; 
    ....
    public class anyname
    { 
    ....
    
    [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);
    } 
    

    ....

    you call alzheimer() to clean/release memory.

提交回复
热议问题