InfoQ

News

.NET Memory Leaks

Posted by Jonathan Allen on Apr 13, 2007 03:27 PM

Community
.NET
Topics
Dynamic Languages ,
.NET Framework
Tags
Memory Leaks
A problem with .NET that isn't talked about is the problems caused by using dynamic code generation. In a nut shell, dynamic code generation, which is used in XML Serialization, Regular Expressions, and XSLT transformations, can lead to memory leaks.

While the Common Language Runtime (CLR) can unload whole App Domains, it cannot unload individual assemblies. Code generation relies on creating temporary assemblies. These assemblies are often loaded into the primary app domain, meaning that cannot be unloaded until the application exits.

For libraries like XML Serialization, this isn't much of a problem. Usually the serialization code for a given type is cached, which limits the application to one temporary assembly per type. But there are XMLSerializer overloads that don't use caching. If a developer uses one of these without providing some sort of application-level cache, memory can be slowly leaked as new instances of essentially the same code is loaded into memory. For more information specifically on XML, see .NET Memory Leak: XmlSerializing your way to a Memory Leak.

The potential problems are far worse for code that habitually rewrites itself at runtime such as those written in LISP. For that sort of language, either the code has to be run entirely interpreted, that is never being compiled to IL code, or placed in separate AppDomains that can be purged from time to time. Either way, it poses a challenge for language developers targeting the Common Language Runtime.

4 comments

Reply

What about dynamic proxies ? by Sadek Drobi Posted Apr 13, 2007 3:51 PM
Re: What about dynamic proxies ? by Jonathan Allen Posted Apr 13, 2007 11:11 PM
Re: What about dynamic proxies ? by Stefan Wenig Posted Apr 14, 2007 1:01 AM
DynamicMethod by Stefan Wenig Posted Apr 14, 2007 1:03 AM
  1. Back to top

    What about dynamic proxies ?

    Apr 13, 2007 3:51 PM by Sadek Drobi

    What about dynamic proxies generated by projects like Castle and Spring?

  2. Back to top

    Re: What about dynamic proxies ?

    Apr 13, 2007 11:11 PM by Jonathan Allen

    Any framework that emits IL at runtime has the potential for a memory leak, but without in-depth knowledge of the code or running a profiler I cannot say for certain. I would suggest contacting the respective teams or using reflection to see if the number of loaded assemblies is growing over time.

  3. Back to top

    Re: What about dynamic proxies ?

    Apr 14, 2007 1:01 AM by Stefan Wenig

    Castle caches everything by default, and I'd be very surprised if spring.net wouldn't. The fact that assemblies cannot be individually unloaded is well known after all. With DynamicProxy, you usually create a small number (often 1) of proxy classes for a number of classes in assemblies that are already referenced statically. Memory usage in these scenarios is usually quite flat. I've never seen a situation where the lack of unloading of dynamic proxies actually leads to problems.
    With regex and XSLT you have to be careful: if you compile input to your app, you'll eventually run out of memory. compilation should therefore only be enabled for app resources (i.e., regular expression or XSL-files that you created and delivered with your app).
    For dynamic proxies to leak in the same way, you'd have to dynamically load assemblies in your default app domain. Leaking-wise, you'd be doomed even without dynamic proxies.

  4. Back to top

    DynamicMethod

    Apr 14, 2007 1:03 AM by Stefan Wenig

    The article should mention that .net 2.0 includes a new mechanism called DynamicMethod. It's a way to generate IL code that is basically garbage collected like any other object. It can only generate methods though, not types.

Exclusive Content

Diary of a Fence Sitting SOA Geek

In this presentation, Mark Little explains the history of SOAP/WSDL/WS-*-based web services and RESTful HTTP and highlights how the two approaches might converge into a single solution.

Flex for XML and JSON

Platforms need interoperability. In this article Flex interoperability with JSON and XML is explored including direct mapping to chart and grid components.

Measuring Agile in the Enterprise: 5 Success Factors for Large-Scale Agile Adoption

Michael Mah analyzes the development process in 5 companies: 2 Agile (one of them BMC) and 3 classic. He presents the factors which contributed to the success of BMC's Agile adoption.

Tom Preston-Werner on Powerset, GitHub, Ruby and Erlang

In this interview filmed at RubyFringe 2008, Tom Preston-Werner talks about how both Powerset and GitHub use Ruby and Erlang, as well as tools like Fuzed, god, and more.

David Laribee on Alt.NET and its Mission

David Laribee discusses the purpose of ALT.NET, its mission and future.

Discover RailsKits and Stop Writing Redundant Code

Ruby on Rails has become a popular Ruby framework for creating web applications in recent years. An aspect of creating a web application is the need to repeatedly create the same base functionality.

A Formal Performance Tuning Methodology: Wait-Based Tuning

Steven Haines talks about tackling web application performance tuning by proposing a method called wait-based tuning.

Shaw and Fowler About Forging a New Alliance

Shaw and Fowler talk about the need for a new relationship between the business department and the IT department. Studies have shown that projects mostly fail due to miscommunication between the two.