C# winform在关闭窗体的时候怎么处理可以及时释放内存?dispose方法应该不能解决这个问题

2024-12-16 02:11:39
推荐回答(2个)
回答(1):

//不知道我的回答是不是你所需要的,希望对你有帮助啊~~
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Runtime.InteropServices;
using System.Diagnostics;
namespace ReleaseMemory_Win
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
this.Close();
FlushMemory();
}

[DllImport("kernel32.dll")]
private static extern bool SetProcessWorkingSetSize(IntPtr process, int minSize, int maxSize);
private static void FlushMemory()
{
GC.Collect();
GC.WaitForPendingFinalizers();
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
SetProcessWorkingSetSize(Process.GetCurrentProcess().Handle, -1, -1);
}
}
}

回答(2):

当窗体关闭了 ,.NET会自动内存回收的。