日期:2014-05-18  浏览次数:20878 次

循环创建控件,如何释放控件内存

现在的问题是:每执行一次下面的代码,内存就增加,如何释放控件内存。

有一个Timer,定时执行下面的代码。

代码如下:
C# code

int row = 1;
            int columnNum = 5;
            int width = 217;
            int height = 266;
            int left = 20;
            int top = 16;
  for (int z = 0; z < groupPanel5.Controls.Count; z++)

                {
                    Control c = groupPanel5.Controls[z];
                   foreach (Control cc in c.Controls)
                        cc.Dispose();
                    groupPanel5.Controls.RemoveAt(z);
                    c.Dispose(); 
                }
                groupPanel5.Controls.Clear();
                GC.Collect();
                foreach (ServersInfo sv in servs)
                {
                    DevComponents.DotNetBar.PanelEx pe = new DevComponents.DotNetBar.PanelEx();


                   DevComponents.DotNetBar.Controls.ReflectionImage refimage = new DevComponents.DotNetBar.Controls.ReflectionImage();
                    refimage.Location = new System.Drawing.Point(0, 0);
                    refimage.Name = "refimage" + sv.ID.ToString();
                    refimage.Size = new System.Drawing.Size(217, 125);                    refimage.Tag = pe;
                    pe.Name = "panelEx_Server" + sv.ID.ToString();

                    pe.Tag = sv;
                    pe.Size = new Size(width, height);

                    if (i % columnNum == 0)
                    {
                        if (i != 0)
                            row++;

                        pe.Location = new Point(left, (row - 1) * height + top * row);

                    }
                    else
                    {
                        if (row == 1)
                        {
                            pe.Location = new Point(i * width + (i + 1) * left, top);
                        }
                        else
                        {
                            pe.Location = new Point((columnNum - (((row * columnNum) - 1) - i) - 1) * width + (columnNum - (((row * columnNum) - 1) - i)) * left, (row - 1) * height + top * row);
                        }
                    }

                    DevComponents.DotNetBar.Controls.ReflectionLabel reflabel_Serverinfo = new DevComponents.DotNetBar.Controls.ReflectionLabel();
                    reflabel_Serverinfo.Location = new System.Drawing.Point(0, 100);
                    reflabel_Serverinfo.Name = "reflabel" + sv.ID.ToString();
                    reflabel_Serverinfo.Size = new System.Drawing.Size(217, 65);

                    DevComponents.DotNetBar.Controls.ReflectionLabel reflabel = new DevComponents.DotNetBar.Controls.ReflectionLabel();
                    reflabel.Location = new System.Drawing.Point(0, 100);
                    reflabel.Name = "reflabel" + sv.ID.ToString();
                    reflabel.Size = new System.Drawing.Size(217, 63);
                      pe.Controls.Add(reflabel);
                    pe.Controls.Add(reflabel_Serverinfo);
                    pe.Controls.Add(refimage);
                    this.groupPanel5.Controls.Add(pe);
                    Application.DoEvents();
              
                    i++;
                }



------解决方案--------------------
高手在忙着赚钱,菜鸟才来混论坛。
------解决方案--------------------
探讨
高手在忙着赚钱,菜鸟才来混论坛。

------解决方案--------------------
net自己的GC已经会回收的
如果你硬是不放心
就自己调用 GC.Collect() 强制释放