Question

We have noticed a strange behavior with the Image Resources in Visual Studio c#.

If you add an image to your project resources and load the image many times you will get a out of memory exception very soon.

It just happen on some systems (on every second PC I tested).

Reproduce:

  1. Create Windows Forms Project
  2. Add an Image to the project resources (32x32 PNG)
  3. Create a for-loop with 2000 loops
  4. in the loop fill the image from resources to private list

    private List<image> _images = new List<image>();
    private void ButtonClick(object sender, EventArgs e)
    {
        for (int i = 0; i < 2000; i++)
        {
            _images.Add(Properties.Resources.myImage);
        }
    }
    

Everytime you run the loop some memory will be allocated. On a healthy system you can do this until you reach 2GB (32Bit Application).

But on a sick system. The app will already hang on 200MB.

Very remarkable is, that a sik system allocates not as same memory as a healthy system. The sik system allocates only about the half memory on a single exection of the loop. A sik system allocates 50MB per loop. A healty system allocates 120MB.

The systems are all exactly the same. Core I7, Windows7, 12GB RAM, Visual Studio 2012. On Windows8, the memory leak appears on every tested system.

Did anybody notice the same strange behaviour?

Was it helpful?

Solution

We found something interesting.

The problem is related to Internet Explorer 10. This also explains why the leak appeared on every Windows8

To uninstall IE10 does not help. It helps if you uninstall the Windows Update KB2670838.

By the way... This code has the same problem.

Image.FromFile("myImage.png");

So the problem seams to be in GDI+ not in the ResourceManager.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top