CompilerParameters.GenerateInMemory = true导致C#内存泄漏

时间:2016-04-14 02:12:03

标签: c# memory-leaks

static void compile()
{
    CodeNamespace cn = new CodeNamespace("System.IO.TextReader");
    CodeCompileUnit ccu = new CodeCompileUnit();
    ccu.Namespaces.Add(cn);

    CSharpCodeProvider icc = new CSharpCodeProvider(); 
    CompilerParameters cplist = new CompilerParameters();
    cplist.GenerateExecutable = false;
    cplist.GenerateInMemory = false;  //★ memory is high when true

    cplist.ReferencedAssemblies.Add("System.dll");
    cplist.ReferencedAssemblies.Add("System.XML.dll");
    cplist.ReferencedAssemblies.Add("System.Web.Services.dll");
    cplist.ReferencedAssemblies.Add("System.Data.dll");
    CompilerResults cr = icc.CompileAssemblyFromDom(cplist, ccu);

}
static void Main(string[] args)
{

    Process proc = Process.GetCurrentProcess();
    for (int i = 0; i < 300; i++)
    {
        compile();
    }
    long usedMemory = proc.PrivateMemorySize64;
    Console.WriteLine(usedMemory);  //print the memory size
}

GenerateInMemory设置为true时,产品会分配更多内存吗?

结果是:

GenerateInMemory      Memory Size (byte)
 true                   21688320 
 false                  16424960

我的问题是:

  1. 内存中的编译结果不会由VM进行GC吗?
  2. appdomain不会卸载程序集吗?

0 个答案:

没有答案