一个winform程序,大致框架是有一个MainForm,MainForm里有一个panel,每次点击菜单时,panel.controls.clear(),然后再在panel里加载需要的子窗体,每点击一次菜单,虚拟内存和内存都增加,现在通过empty.exe这个工具能将内存给释放掉,但是虚拟内存不知道怎么释放了,请大家给给建议。

解决方案 »

  1.   

    不知我的思路是否有点跑题,不过照 lz 的描述,似乎应该 GC.Collect() 一下吧,强制进行垃圾回收。
    详情参见 MSDN
    虽说不知道对机器有多明显的效果,但至少能够使程序更有效地利用内存吧,我想。
      

  2.   

    解决了,虽然没有完全消除,但增副不大,基本控制在31m虚拟内存左右解决方案:1,定义一个接口
            public interface CustomForm
            {
                void EmptyRam();
                System.Windows.Forms.Form currentForm { get; set; }        }
    2,让窗体集成这个接口,在EmptyRam中释放和GC.Collect(),eg
        public partial class AlarmForm : Form, Helper.CustomForm
        {
            public AlarmForm()
            {
                InitializeComponent();
                p_current = this;
            }
            public static AlarmForm p_current = null;
            public System.Windows.Forms.Form currentForm
            {
                get
                {
                    return p_current;
                }
                set
                {
                    p_current = (AlarmForm)value;
                }
            }        public void EmptyRam()
            {
                this.Dispose();
                this.Close();
                p_current = null;
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }    }3,在菜单click前调用窗体的EmptyRam方法,自定义form在_panel里
                    Helper.CustomForm customForm = (Helper.CustomForm)_panel.Controls[0];
                    customForm.EmptyRam();
      

  3.   

    附带用Empty.exe释放内存的代码,empty.exe是Windowsxp和win2003自带的内存管理工具,只有40多K,很好用
                System.Diagnostics.ProcessStartInfo p = null;
                System.Diagnostics.Process Proc;
                string path = System.IO.Directory.GetCurrentDirectory();
                if (!File.Exists(path + "\\empty.exe"))
                {
                    MessageBox.Show(System.IO.Directory.GetCurrentDirectory() + "\\empty.exe不存在");
                }
                p = new ProcessStartInfo(path + "\\empty.exe", "Hisign.ACS.Main.exe");
                p.WorkingDirectory = path;//设置此外部程序所在windows目录
                p.WindowStyle = ProcessWindowStyle.Hidden;//在调用外部exe程序的时候,控制台窗口不弹出
                //如果想获得当前路径为            Proc = System.Diagnostics.Process.Start(p);//调用外部程序
                System.Threading.Thread.Sleep(100);
      

  4.   

    搞了一天,谢谢wsj1983920,kbryant捧场,各散20,谢谢LaoBai_2006的回答,散60,呵呵