Hello everybody,
I stumbled upon an OutOfMemory-Exception when working with lots of pictures (sequentially, not in parallel). I reproduced the behavior in some small portion of code like this:
class ImageHolder
{
public Image Image;
~ImageHolder()
{
Image.Dispose();
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < 1000; i++)
{
ImageHolder h = new ImageHolder() { Image = new Bitmap(1000, 1000) };
}
}
}
The memory usage rises and rises until I get an exception (sometimes ArgumentException, sometimes OutOfMemory Exception).
My question is NOT what I can do about this (I could implement IDisposable in ImageHolder and use a using-block, for example).
My question is rather: Why doesn't garbage collection destroy my objects of type ImageHolder (the destructor is never called), because there's no reference on them and I'm running out of memory!
Thanks for an explanation,
Philipp