c# - Action<string> not garbage collected. Why? -
can't seem action collected gc. why?
weakreference reference = null; weakreference reference2 = null; new action(() => { action<string> deliverymessage = (ans) => { }; object = new object(); reference = new weakreference(deliverymessage); reference2 = new weakreference(a); }) (); gc.collect(); gc.waitforpendingfinalizers(); assert.null(reference2.target); //true assert.null(reference.target); //false
i'm going use action in weakreference list, need simple test go through first...
the lambda expression not use captured variables transformed static method. optimization, prevent additional gc overhead caused allocations, static delegate field created. static field accessed when assignment executed. new delegate not created each time. static delegate, once initialized, never collected. can see viewing decompilation.
Comments
Post a Comment