delegates - C# Actions and GC -


i using actions in c# , wondering if need set instance of action null once wish gc collect objects properly? here example:

public class {  public action a; }  public class b {   public string str; }  public class c {  public void dosomething()  {    aclass = new a();    b bclass = new b();    aclass.a = () => { bclass.str = "hello"; }  } } 

inside main method have this:

public void main(...) {   c cclass = new c();   cclass.dosomething();    console.writeline("at point dont need object or b anymore gc collect them automatically.");   console.writeline("therefore giving gc time letting app sleep");   thread.sleep(3000000);   console.writeline("the app propably sleeping long enough gc have tried collecting objects @ least once not sure if , b objects have been collected");  } } 

please read console.writeline text understand asking here.

if apply understanding of gc example gc never collect objects since cannot destroyed because holds instance of b. right?

how can collect 2 objects? need set instances of actions null let gc collect objects before end of application or there kind of smart mechanism gc knows how destroy objects have actions such , b are?

edit: question gc , collecting objects properly. not calling method collect().

there numerous problems question. rather answer question directly i'm going answer questions should asking.

let's first disabuse of notions gc.

will sleeping long time activate garbage collector?

no.

what activates garbage collector?

for testing purposes can use gc.collect() , gc.waitforpendingfinalizers(). use these testing purposes; bad practice use them in production code except in rare circumstances.

under normal situations things trigger gc complicated; gc highly tuned piece of machinery.

what semantics of garbage collection insofar closed-over outer variables concerned?

the lifetime of closed-over outer variable of lambda converted delegate extended not shorter lifetime of delegate.

suppose have variable of type action initialized lambda closed on outer local variable of reference type. in order make object referred variable eligable collection, have set variable of type action null?

in vast majority of cases, no. the garbage collector smart; let work , not worry it. runtime determine action variable cannot reached live root , make eligable collection; closed-over outer variable become eligible.

there may extremely rare situations in want throw away references action sooner, rare; vast majority of time, let gc job without interference.

are there situations in outer variables can have lifetimes extended long?

yes. consider:

void m() {     expensive e = new expensive();     cheap c = new cheap();     q.longlived = ()=>c; // static field     q.shortlived = ()=>e; // static field } 

when m() executed closure created both delegates. suppose shortlived going set null soon, , longlived set null far in future. unfortunately both local variables have lifetimes extended lifetime of object referred longlived, though c still reachable. expensive resource e not released until reference in longlived dead.

numerous programming languages have problem; implementations of javascript, visual basic, , c# have problem. there talk of fixing in roslyn release of c# / vb not know if come fruition.

in case solution avoid situation in first place; don't make 2 lambdas share closure if 1 of delegates live longer other.

under circumstances local not closed-over outer variable become eligable collection?

the moment runtime can prove local cannot read again, thing references becomes eligable collection (assuming local root of course.) in example program there no requirement references in aclass , bclass remain alive until end of method. in fact, there rare possible circumstances in gc can deallocating object on 1 thread while still in constructor on thread! gc can very aggressive determining dead, careful.

how keep alive in face of aggressive gc?

gc.keepalive() of course.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -