Someone helped me to run some codes in runtime using a timer, but after a while I saw that it is just using memory and not releasing it.
I hear something about AppDomain, but I didn't figure out where to use it.
AppDomain would help me with that memory leak? Anything else would help me then?
PS: GC.Collect() doesn't help. I'm sure that the problem is that, since I made some tests, watching the memory while running the problem, if I disable the Scripter it keep the same ammount(basically), if I start the timer with some codes to execute it keep increasing and can get like 500k+ of memory used after some minutes, so yes, I'm sure that the problem is with the CSharpCodeProvider just using memory.
Here is my actual code, so if someone could help me with this problem would be great.
//It is executed in a timer of 500 ms
private void Run()
{
foreach (Code ph in codeList)
{
Code p = ph;
new Thread(delegate()
{
if (Monitor.TryEnter(p))
{
Scripter script = new Scripter();
script.Compile(p.Code);
Monitor.Exit(p);
}
}) { IsBackground = true }.Start();
}
}
//That's my compile code
public bool Compile(string script)
{
CSharpCodeProvider codeprovider = new CSharpCodeProvider();
ICodeCompiler icc = codeprovider.CreateCompiler();
CompilerParameters cp = new CompilerParameters();
cp.ReferencedAssemblies.Add("System.dll");
cp.ReferencedAssemblies.Add(System.Reflection.Assembly.GetExecutingAssembly().Location);
cp.TreatWarningsAsErrors = false;
cp.MainClass = "CodesRun";
cp.CompilerOptions = "/target:library /optimize";
cp.GenerateExecutable = false;
cp.GenerateInMemory = false; //it was true, but same problem
TempFileCollection tfc = new TempFileCollection(Application.StartupPath, false);
CompilerResults cr = new CompilerResults(tfc);
cr = icc.CompileAssemblyFromSource(cp, script);
if (cr.Errors.Count > 0)
{
//Error
}
else
{
Assembly assembly = cr.CompiledAssembly;
IScript teste = (IScript)assembly.CreateInstance("CodesRun.Script");
teste.Run();
}
tfc.Delete();
codeprovider.Dispose();
return true;
}