【C#】内存问题汇总

一、关闭窗体内存不释放

[DllImport("kernel32.dll")]
private static extern bool SetProcessWorkingSetSize(IntPtr process, int minSize, int maxSize);
//关闭窗体按钮 
private void btnReturn_Click(object sender, EventArgs e)
{
  this.Close();
  FlushMemory();
}
//刷新存储器
private static void FlushMemory()
{
  GC.Collect();
  GC.WaitForPendingFinalizers();
  if (Environment.OSVersion.Platform == PlatformID.Win32NT)
  {
    SetProcessWorkingSetSize(Process.GetCurrentProcess().Handle, -1, -1);
  } 
}

来源:C# winform在关闭窗体的时候及时释放内存问题 - QianTM - 博客园 (cnblogs.com)

posted @ 2024-03-09 10:33  不溯流光  阅读(55)  评论(0编辑  收藏  举报