开发者

Memory issues with winforms ImageList

I have an ImageList tha开发者_如何学Ct is populated with, well you guessed it, images. These images are loaded into memory in a dataset as a Bitmap. Until I loaded them into the ImageList the rise of memory is not worry. But when they are added to the ImageList the memory usage sky rockets. But the biggest problem is when I have to reload the list of images. I've tried to call dispose on every image on the list but the memory is not freed. This is the code I tried to clean up the memory:

        foreach (Image item in imageList.Images)
        {
            item.Dispose();

        }
        imageList.Images.Clear();

        GC.Collect();

What am I doing wrong?


Your dispose code is not appropriate. Iterating the Images collection actually creates a new bitmap for each image. Which you then immediately dispose again. Just call Clear().

GC.Collect() cannot have any effect either, the ImageList class is a wrapper around the native Windows component. Which stores the images in native memory, not garbage collected memory.

Last but not least your real issue: the Windows memory manager just doesn't work the way you think. It does not shrink the virtual memory size of the program when it frees memory. It simply marks the block of memory as unused and adds it to the list of free blocks. Ready to be re-used at a later time. Only in the very rare case where the freed memory happens to span the entire set of reserved memory pages can it shrink the virtual memory size. This is not a real problem. It's virtual.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜