2
使用RemoveRange后,项目似乎保留在内存中。我对这些物品没有其他参考。我应该只使用一种解决方法,在其中复制我想要的项目并完全清除旧列表?c#列表<T> RemoveRange删除项目会发生什么?
打了个比方来说明:
private void Form1_Load(object sender, EventArgs e)
{
bmp = new Bitmap(5000, 5000, PixelFormat.Format32bppPArgb);
pictureBox1.Image = bmp;
pictureBox1.Width = bmp.Width;pictureBox1.Height = bmp.Height;
bmp2 = new Bitmap(some_image_file);//500x500 bitmap
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
bitmap_list.Add(new Bitmap(bmp));
Graphics.FromImage(bmp).DrawImage(bmp2, e.X - bmp2.Width/2, e.Y - bmp2.Height/2);
pictureBox1.Refresh();
}
private void button1_Click(object sender, EventArgs e)
{// where do the items go? memory is not freed until running a manual GC
bitmap_list.RemoveRange(1, bitmap_list.Count - 1);
}
private void button2_Click(object sender, EventArgs e)
{// if this is not clicked, memory will run out even after clearing the list
// down to one item
GC.Collect();
}
谢谢!
谢谢,就是这样。我通常使用很多处置,但由于某种原因,在这里没有想到它。 – george