Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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!
Thanks.

VipHaLong
Posted
Updated 18-Mar-13 0:11am
v2

1 solution

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.
C#
private void remove(List<Bitmap> list, int index)
    {
    if (list.Count > index)
        {
        Image i = list[index];
        list.RemoveAt(index);
        i.Dispose();
        }
    }
However that won't reduce the memory shown in the Task Manager - once .NET expands the heap, it doesn't reduce it.
 
Share this answer
 
Comments
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900