问题描述:我在体格方法中产生了N个对象,我试着释放这些对象,发现无效,不解?
代码如下:class abc
{
................... 
    List<Object> list = new List<Object>();  //定义保存对象的记录集
        string gggg;                         //对象ID
        private void button1_Click(object sender, EventArgs e) 
        {
            for (int ps = 1; ps <=100; ps++)
            {
                myuser nn = new myuser();  //生成一个myuser对象
                gggg = ps.ToString();      //生成对象ID
                list.Add(nn);              //保存
                list.Add(gggg);            //保存
            }
        }
}class myuser:IDisposable {
        bool _isDisposed;        public User() {
        }        void ThreadProc() {
        }        protected virtual void Dispose(bool isDisposing) {
            if (_isDisposed) return;            if (isDisposing) {
            }
            _isDisposed = true;
        }        public void Dispose() {
            Dispose(true);
            GC.SuppressFinalize(this);
        }        ~myuser() {
            Dispose(false);
        }
    }
// 执行button1_Click函数后,发现程序占用32M内存
//之后执行销毁用户对象的操作
 private void button3_Click(object sender, EventArgs e)
        {
            for (int f = 0; f < listBox1.Items.Count; f = f + 2)
            {
                if (f >= 100) break;
                ((myuser)list[f]).Dispose();  
            }
            list.Clear();
        }按理说,执行完button3_Click后,程序占用的内存会减少,可是我发现没有变化?
请高手看看!!!!!

解决方案 »

  1.   

    C#貌似是由GC来回收资源的,可以手动强制回收吗?
      

  2.   


    GC没有回收的原因。
    ***********************************
    冰凝制作室
    MAIL:[email protected]
    URL:http://bingning.net/free/SOURCE/index.html
      

  3.   

    你可以在list加后直接试一下内存是否下降,代码如下:
      private void button1_Click(object sender, EventArgs e) 
            { 
                for (int ps = 1; ps <=100; ps++) 
                { 
                    myuser nn = new myuser();  //生成一个myuser对象 
                    gggg = ps.ToString();      //生成对象ID 
                    list.Add(nn);              //保存 
                    list.Add(gggg);            //保存 
                } 
              list.Clear(); //新加的
            } 如果有下贱,说明你的方法可能存在一些问题.
      

  4.   

     ((myuser)list[f]).Dispose();  不能回收内存 
      

  5.   

    我觉得你们的回答不对,因为我在 class myuser中加入线程代码
    执行private void button3_Click(object sender, EventArgs e) 
            { 
                for (int f = 0; f < listBox1.Items.Count; f = f + 2) 
                { 
                    if (f >= 100) break; 
                    ((myuser)list[f]).Dispose();  
                } 
                list.Clear(); 
            } 
    后,发现,线程没有终止!!!!!
      

  6.   

    net是靠回收器去做的,不是你想回收就能立即回收滴
      

  7.   

    我觉得正常执行   Dispose(true),后,对象已经失效,尽管不会马上释放资源,但是对象中的线程一定是要终止的!!
    可是线程不终止
    现在的类如下:
    class myuser:IDisposable
        {
            bool _isDisposed;
            Thread _thread;
            public event getval haha;
            int pc = 1000;
            public void Dispose()
            {
                Dispose(true);
                GC.SuppressFinalize(this);
            }
            protected virtual void Dispose(bool isDisposing)
            {
                if (_isDisposed) return;            if (isDisposing)
                {
                }            if (_thread != null && _thread.IsAlive)
                {
                    _thread.Abort();   
                    //_thread.Join();  //释放该处的2行代码页无效,线程不终止
                    //_thread = null;  //
                }
                _isDisposed = true;
            }        void ThreadProc()
            {
            }        public myuser()
            {
              
            }
             public myuser(string abc) {
                 ThreadPool.QueueUserWorkItem(MyNowWork,abc);   //执行线程  
            }
            //初始化线程池
            public static void init_pools()
            {
                ThreadPool.SetMaxThreads(500, 500);
            }        public void MyNowWork(object ssa)
            {
                if ((string)ssa == "1")  //刷新textbox1控件
                {
                while(true)
                {
                 pc=pc-1;
                    if (pc==0)break;
                    Thread.Sleep(1000);
                    if (haha != null)
                        haha(pc.ToString());
                   }
                }            if ((string)ssa == "2")  //刷新textbox2控件
                {            }        }  
        }