Why is the Invoke of an assembly taking so much RAM?
I'm successfuly running assembly (exe) from byte array. My code is:
public static开发者_运维问答 void Execute(byte[] assembly, string arg) {
if (assembly[0x3c] == 0x80) {
object[] o = new object[] { new string[] { arg } };
try {
Assembly.Load(assembly).EntryPoint.Invoke(null, o);
} catch (TargetInvocationException e) {
throw e.InnerException;
}
} else {
throw new Exception("File is not a valid .NET assembly.");
}
}
All fine, but the executable keeps leaking memory. The original needs 6-10MB, this one after the run produces 40-60 and up to 145mb (and then drops down to 10 and loops again).
Why is this happening, what leaks memory and any ideas how to fix that?
It's not leaking memory; you're seeing the effects of garbage collection. Garbage collection can be deferred until some point in the future, when the system determines that it needs more memory; that's what's happening in your instance when the process usage suddenly drops.
Don't worry about it; it's perfectly normal. Moreover, it's the way the system is designed; this behavior is not affecting your execution time or overall memory usage.
Under normal circumstances you don't have an N+1 copy of the assembly (the byte array) in memory. I would look there for your disparity.
精彩评论