BT

Closures and Object Lifetime in C# and Visual Basic

| by Jonathan Allen Follow 639 Followers on Jun 20, 2007. Estimated reading time: 1 minute |

C# 2 and yet to be released VB 9 allow developers to reference local variables in anonymous functions. When an anonymous function 'closes over' a variable, the local variable is promoted to an instance variable and stored in an object called a closure. This allows the variable to exist long after the method call that created it has been completed.

The closure referenced by anonymous functions is normally eligible for garbage collection when any delegates pointing to the anonymous functions go out of scope. Notice that functions is plural. All anonymous functions in the same scope share the same closure. Eric Lippert thinks this point is not being given enough attention.

However, a related issue which I haven't seen anyone talk much about is what the consequences of having one closure per scope are. Though it makes the closure semantics conceptually easier to think about (and implement!), it can lead to an unfortunate problem with garbage collection.

In the example he gives on his blog, two anonymous functions are created. The short-lived function is released immediately while the long-lived function is returned from the method and may exist for an indeterminate amount of time.

The closure for the short-lived delegate owns the expensive resource. But since there is one closure per scope, both the short-lived and the long-lived delegates own a single closure. The closure cannot be collected until every delegate that owns it is dead. Therefore the expensive resource is not released until the long-lived delegate is released, even though the long-lived delegate does not reference the expensive resource!


Rate this Article

Adoption Stage
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread
Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Discuss
BT