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