Wednesday, November 14, 2012

Are you having a memory leak because of Strucutre map?

So here's the situation....


(My parents went away for a weeks vacation...(Soory, can't help myself, child of the 80s...))



Running a winforms app using a Library that has structure map all through it, and it uses ObjectFactory directly instead of IContainer....


So we get memory leaks that we can't get rid of as Structure map is hanging on to instances of the objects so they can't be garbage collected...



Here's the quick hack to fix in,



Since what I'm doing is in a loop, at the bottom of the loop construct I just type:



// strucutre map memory leak hack, works okay i guess, it's slower as expected - EWB
ObjectFactory.ResetDefaults();



and it flushes all the objects out of Structure map...

Of course it's slower because all the objects get re init-ed in each loop, but the memory
got tons better...

Also note that in an upcoming release,



ObjectFactory.ResetDefaults();



will get changed to



ObjectFactory.Reset();

No comments:

Post a Comment