我怎样才能prevent CompileAssemblyFromSource从内存泄露? [英] How can I prevent CompileAssemblyFromSource from leaking memory?

查看:519
本文介绍了我怎样才能prevent CompileAssemblyFromSource从内存泄露?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这是使用CSHARP codeProvider.CompileAssemblyFromSource在内存中创建一个装配一些C#code。大会已收集垃圾后,我的应用程序使用比它在创建组装之前做了更多的内存。我的code是一个ASP.NET Web应用程序,但我已经在一个WinForm重复这个问题。我使用System.GC.GetTotalMemory(真)和红门蚂蚁内存分析器来衡量的增长(与code样品约600个​​字节)。

I have some C# code which is using CSharpCodeProvider.CompileAssemblyFromSource to create an assembly in memory. After the assembly has been garbage collected, my application uses more memory than it did before creating the assembly. My code is in a ASP.NET web app, but I've duplicated this problem in a WinForm. I'm using System.GC.GetTotalMemory(true) and Red Gate ANTS Memory Profiler to measure the growth (about 600 bytes with the sample code).

这是我做了搜索,这听起来像泄漏来自于创造新的类型,不是真的从我手里拿的引用任何对象。一些网页,我发现刚才提到一些有关的AppDomain,但我不明白。有人能解释一下是怎么回事,如何解决?

From the searching I've done, it sounds like the leak comes from the creation of new types, not really from any objects that I'm holding references to. Some of the web pages I've found have mentioned something about AppDomain, but I don't understand. Can someone explain what's going on here and how to fix it?

下面是一些示例code是否泄漏:

Here's some sample code for leaking:

private void leak()
{
    CSharpCodeProvider codeProvider = new CSharpCodeProvider();
    CompilerParameters parameters = new CompilerParameters();
    parameters.GenerateInMemory = true;
    parameters.GenerateExecutable = false;

    parameters.ReferencedAssemblies.Add("system.dll");

    string sourceCode = "using System;\r\n";
    sourceCode += "public class HelloWord {\r\n";
    sourceCode += "  public HelloWord() {\r\n";
    sourceCode += "    Console.WriteLine(\"hello world\");\r\n";
    sourceCode += "  }\r\n";
    sourceCode += "}\r\n";

    CompilerResults results = codeProvider.CompileAssemblyFromSource(parameters, sourceCode);
    Assembly assembly = null;
    if (!results.Errors.HasErrors)
    {
        assembly = results.CompiledAssembly;
    }
}

更新1:这个问题可能涉及:<一href=\"http://stackoverflow.com/questions/1162686/dynamically-loading-and-unloading-a-a-dll-generated-using-csharp$c$cprovider\">Dynamically装载和卸载一个dll产生使用CSHARP codeProvider

更新2:试图了解应用领域多了,我发现这一点:的什么是一个应用程序域 - 一个解释的.Net初学者

Update 2: Trying to understand application domains more, I found this: What is an application domain - an explanation for .Net beginners

更新3::要澄清,我正在寻找,没有内存泄漏提供了相同的功能code以上(编译和提供访问产生code)的解决方案。它看起来像解决方案将包括创建一个新的AppDomain和编组。

Update 3: To clarify, I'm looking for a solution that provides the same functionality as the code above (compiling and providing access to generated code) without leaking memory. It looks like the solution will involve creating a new AppDomain and marshaling.

推荐答案

卸载程序集,不支持。为什么有些信息可以在这里找到
使用一个AppDomain的一些信息可以在这里找到

Unloading an assembly is not supported. Some information on why can be found here. Some information on using an AppDomain can be found here.

这篇关于我怎样才能prevent CompileAssemblyFromSource从内存泄露?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆