views:

353

answers:

3

I am trying to reduce the amount of memory my application uses. In my application, I use System.Reflection.Emit.EnumBuilder to create enumerations on the fly from information I receive at runtime. I only use the EnumBuilder to create an individual enumeration, and after that I have no further use for it. While using CLRProfiler, I noticed that the EnumBuilders are never destroyed. My application can make thousands of enumerations while running, so the amount of memory used by these EnumBuilders can add up. EnumBuilder does not define a Dispose method, so I cannot destroy it that way. Is it possible to somehow remove leftover EnumBuilders from memory?

+1  A: 

I have noticed the same. It's a 'leak' in ModuleBuilder that hangs on to the bytes and strings IIRC.

You can have a look in WinDbg with SOS loaded for better memory info.

leppie
+4  A: 

EnumBuilder creates the enumeration in a dynamic assembly. According to How to: Load and Unload Assemblies (C# Programming Guide), there's no way to unload an assembly without unloading all app domains that use it. In particular:

"Even if the assembly goes out of scope, the actual assembly file will remain loaded until all application domains that contain it are unloaded."

Jim Mischel
+2  A: 

My application can make thousands of enumerations while running,

Why? It sounds like you are doing something that you probably shouldn't be doing.

Jonathan Allen