Click here to Skip to main content
13,045,657 members (53,219 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I have a List<bitmap>, I tried adding to that list about 60 Bitmap objects, and opened Task Manager to see how much memory it used, after I pressed on some button to call Remove(), RemoveRange, ... methods of the List<bitmap> and see if the used memory was decreased (freed), however I didn't see any change in memory used by my application.

Could you please give me a solution for this? I have tried using System.GC.Collect() periodically (with a cycle of about 5 seconds), it seems to work OK, but some people say it's not good. I don't know why, because it really helps me free unused memory, I would like to know another solution if that is not good.

Your help would be highly appreciated!

Posted 18-Mar-13 0:10am
Updated 18-Mar-13 0:11am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

It's not good to call the GC yourself - you shouldn't need to if you obey the rules.
In this case, you are adding Bitmaps - which are large objects and which support the Dispose method.
So when you remove them from your list and stop needing them, get rid of them then.
private void remove(List<Bitmap> list, int index)
    if (list.Count > index)
        Image i = list[index];
However that won't reduce the memory shown in the Task Manager - once .NET expands the heap, it doesn't reduce it.
supernorb 18-Mar-13 6:25am
But what I want is it can reduce the memory viewed in Task Manager, does your solution mean that we shouldn't use Remove(), RemoveRange() methods of List class? Thanks!
OriginalGriff 18-Mar-13 6:30am
No, you can use what you want - but if an object you are finished with implements Dispose (as Bitmap does) then you should call it to release resources.
supernorb 18-Mar-13 8:08am
But as you said even when using Dispose() the memory shown in Task manager is still not decreased, if so my application may use up to 300-400 MB
OriginalGriff 18-Mar-13 8:25am
Don't worry about it - it will be available for other use within your app, and will only grow again when that is used up.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 18 Mar 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100