天天看點

winform 關閉窗體 釋放記憶體

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);

        }

    }