WPF memory leak

前端 未结 6 1951
生来不讨喜
生来不讨喜 2021-01-13 00:35

I have a simple wpf application. In main window i have stack panel and 2 buttons. First button adds 100 my user controls (without any data bindings, events, bitmaps), and se

6条回答
  •  囚心锁ツ
    2021-01-13 01:29

    By using this Dll Invoke we can realocate the memory resources

    public class MemoryManagement
    {
    [DllImportAttribute("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 FlushMemory()
    {
    GC.Collect();
    GC.WaitForPendingFinalizers();
    if (Environment.OSVersion.Platform == PlatformID.Win32NT) { SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1);
    }
    }
    

提交回复
热议问题