memory management - Mechanism of destroying an object by CLR in c#.net -


we can declare destructor not know when run, since decided clr when destroy object no longer referenced type variable. clr decides using garbage collector build map reachable objects , deallocate unreachable objects (objects has no reference).

here clear can never destroy object our own code in c#.

now

when use using statement (not using directive) shown below

using(manageme manage = new manageme())         {             manage.add(4,8);             console.writeline("using statement executed");         } 

q1: happens object reference manage (type variable) after end of using statement? means destroyed or clr decide destroy when needs?

q2: how can check whether object not reference type variable destroyed or not during execution of programme?

q1: happens object reference manage (type variable) after end of using statement? means destroyed or clr decide destroy when needs?

it happens whatever happens local method reference.

q2: how can check whether object not reference type variable destroyed or not during execution of programme?

i'll answer question new question: how can check reference against condition if don't own whole reference? object lives in memory unless there's no reference pointing given object. otherwise, garbage collector collect it's fired automatically clr. in other words: if program has no reference object, can sure object garbage-collected.

that is, garbage-collector may collect object implementing idisposable , it'll able reclaim memory but underlying resources required , used whole object still alive because garbage collector won't call idisposable.dispose you.

the problem question: idisposable pattern has nothing garbage collection

idisposable interface has nothing garbage collection. it's interface define objects capable of releasing resources. these resources can whatever:

  • a file system lock.
  • a tcp port.
  • a database connection.
  • ...

usually, idisposable implemented classes handling unmanaged resources and, since they're unmanaged, need manually dispose them. otherwise, released garbage collector (which handles managed resources, of course...).

in other hand, using statement try-finally simplification:

using(manageme manage = new manageme()) {      manage.add(4,8);      console.writeline("using statement executed"); } 

...is:

manageme manage = new manageme(); try  {      manage.add(4,8);      console.writeline("using statement executed"); }  {     manage.dispose(); } 

thus, said in answer q1, using block syntactic sugar sure idisposable.dispose method called once block ends. using block expects idisposable can either variable declaration of idisposable itself:

manageme me = new manageme(); using(me)  {  } 

Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -