c# - How to destroy an un-managed object using IDisposable Interface? -


i know how use idisposable interface in class. but, don't know how free memory allocated class variables? example in class have created list<bitmap> 500 bitmap images in list , clearing list in dispose method. but, shows same result when fetch total memory using gc.gettotalmemory().

here, have created sample class implemented idisposable interface.

public class disposable : idisposable {     list<bitmap> list = new list<bitmap>();     public disposable()     {         (int i=0; i< 500; i++)         {             bitmap bmp = new bitmap(1024,768);                                 using (graphics g = graphics.fromimage(bmp))             {                 g.fillrectangle(brushes.red, new rectangle(0,0,1024,768));             }             list.add(bmp);                             }     }       public void dispose()     {         list.clear();         list = null;     } } 

then have executed following code

static void main(string[] args) {      long l1 = gc.gettotalmemory(true);     using (disposable d = new disposable())     {         //nothing     }      long l2 = gc.gettotalmemory(false);      console.writeline(l1.tostring());     console.writeline(l2.tostring());      console.readkey(); } 

the output says.

181764 //bytes before creating object of disposable class 222724 //bytes after creating object of disposable class 

then have tried without using keyword simple declaration.

dim d new disposable(); 

but, giving me same result.

so, question why memory don't free allocated bitmap images dispose object. doesn't make difference. clearing items list , assigning null value list object.

i have tried same thing declaring byte array instead of list<bitmap> , sqlconnection object.

but, not showing difference in total memory allocated between both methods(simple declaration , using statement).

long l1 = gc.gettotalmemory(true);  //sqlconnection cnn = new sqlconnection("");    //1st method  using (sqlconnection cnn = new sqlconnection("")) //2nd method {     //nothing }  long l2 = gc.gettotalmemory(false);  console.writeline(l1.tostring()); console.writeline(l2.tostring()); 

i can't understand why not clear memory allocated both managed , un-managed objects?

the garbage collector not run time when necessary. in tests may run once in 5 minutes. can force garbage collector collect gc.collect(); , gc.waitforpendingfinalizers();


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -