Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WinForm memory
I have a List, 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 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 18-Mar-13 1:10am
supernorb2.6K
Edited 18-Mar-13 1:11am
v2

1 solution

Rate this: bad
good
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];
        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.
  Permalink  
Comments
supernorb at 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 at 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 at 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 at 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
0 OriginalGriff 190
1 Jochen Arndt 155
2 PIEBALDconsult 150
3 Afzaal Ahmad Zeeshan 120
4 DamithSL 115
0 OriginalGriff 5,695
1 DamithSL 4,591
2 Maciej Los 4,012
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,190


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 18 Mar 2013
Copyright © CodeProject, 1999-2014
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