Geeks With Blogs

Here is an excellent article on types of memory leaks you can still have in a managed app:

One thing I was blown away with was this code snippet that dynamically creates an assembly in memory:

CodeCompileUnit program = new CodeCompileUnit();
CodeNamespace ns = new
ns.Imports.Add(new CodeNamespaceImport("System"));

CodeTypeDeclaration class1 = new CodeTypeDeclaration("CodeDomHello");
CodeEntryPointMethod start = new CodeEntryPointMethod();
start.ReturnType = new CodeTypeReference(typeof(void));
CodeMethodInvokeExpression cs1 = new CodeMethodInvokeExpression(
  new CodeTypeReferenceExpression("System.Console"), "WriteLine",
    new CodePrimitiveExpression("Hello, World!"));

CSharpCodeProvider provider = new CSharpCodeProvider();
CompilerResults results = provider.CompileAssemblyFromDom(
  new CompilerParameters(), program);

(Beware of dynamically generating MSIL using System.CodeDom like this. If you are regenerating code then you are leaking unmanaged heap memory.)

Posted on Tuesday, January 16, 2007 6:35 PM .NET , Testing | Back to top

Copyright © Devin A. Rychetnik | Powered by: