Hey, On Wed, Jan 18, 2012 at 11:43 PM, Ken Beckett <[email protected]> wrote: > There seems to be a terrible memory leak in the design of Mono Cecil > when doing such a thing, in that the DefaultAssemblyResolver class > keeps a private cache of all loaded AssemblyDefinition objects, and > yet provides no means of ever purging this cache. That sort of thing > is always a nasty thing to do. The simple addition of a PurgeCache() > method would be nice - otherwise it's necessary to create a custom > AssemblyResolver that derives from BaseAssemblyResolver to get around > this problem.
The suggested pattern here is: * Create your own IAssemblyResolver, scoped for your task. * Pass it to your AssemblyDefinition/ModuleDefinition when reading them. * When you're done processing the assemblies, the resolver will be collected along with the assemblies. and: * If you're not satisfied about the behavior of the DefaultAssemblyResolver, which is a caching resolver, you can write your own implementation that either delegates or extends BaseAssemblyResolver. Nothing terrible here. The one thing I'm particularly annoyed at, is the GlobalAssemblyResolver ugly ass singleton that's going to go away before 1.0, as this one, is indeed a memory leak if you keep resolving assemblies while not specifying a resolver. > In summary, Mono Cecil appears to be designed to do a one-time load of > a group of related assemblies that you never unload from memory. It > does not appear to handle (as-is) repeated loads of different groups > of assemblies with different search paths and proper unloading. This > would seem to require a custom AssemblyResolver with different > instances (and search paths) used for each related group that is > loaded, and any AssemblyDefinition caching must be done at a higher/ > separate level in order to work properly. If you follow the pattern described up there, no problem. And if with that you're still having memory issues, you can try the lowermemuse branch, which is a prototype where a handle on the file is kept to avoid loading the metadata in memory. Jb -- -- mono-cecil
