C Sharp

A Bit of History

A few years ago when the .NET project first started, a massive debate on the issue of resource management raged. Early contributors to.NET came from the COM and Microsoft Visual Basic teams, as well as many other teams across Microsoft. One of the big problems these teams faced was how to handle issues regarding reference counting, including cycles and errors resulting from misuse. One such example is the circular reference problem: one object contains a reference to another object that then contains a reference back to the first object. When and how one releases a circular reference can cause problems. Not releasing one or both references results in memory leak, which is extremely difficult to track down. And anyone who's done a lot of COM work can recount the stories of schedule time lost to chasing down bugs that resulted from reference-counting problems. Microsoft, realizing that reference-counting problems are fairly common in substantial component-based applications (such as COM applications), set out to provide a uniform solution in .NET.

The initial solution to the problem was based on automatic reference counting-so that if the programmer had forgotten reference counting it wouldn't matter-in addition to methods of detecting and handling cycles automatically. In addition, the .NET team looked at adding conservative reference collection, tracing collection, and GC algorithms that could collect a single object without doing an entire graph trace. However, for a variety of reasons-I'll cover these different approaches shortly-it was concluded that these solutions were not going to function in the typical case.

One major obstacle was that in the beginning days of .NET development, maintaining a high degree of Visual Basic compatibility was a primary goal. Therefore, the solution had to be complete and transparent with no semantic changes to the Visual Basic language itself. After much debate, the final solution was a context-based model where everything existing in a deterministic context would use reference counting on top of the GC and everything not in this context would use only the GC. This did help avoid some of the type bifurcation issues (described shortly) but did not yield a good solution for the fine-grained mixing of code between languages. So, the decision was made to make a series of changes to the Visual Basic language to "modernize" it and make it morecapable. A part of that decision was the dropping of Visual Basic's lifetime compatibility requirements. This decision also generally ended investigation into deterministic lifetime issues.