R
Ragnar Agustsson
Hi all
I have been wandering about the best way to sandbox memory leaks in 3rd
party libraries when using them from the .Net framework.
I have a 3rd party library, written in C++, that leaks a lot of memory
but I still had to use it.
1.
After using DLLImport and seeing the memory leak I tried to load and
unload the library using the kernel's LoadLibrary and FreeLibrary. That
did not work as the leaked memory was not reclamed, not surprising as
my main .Net app's process was still alive.
2.
Then I read about AppDomain, that sounded pretty cool as it can unload
assemblies. So my hope was that the GC would be intelligent enough to
see the leaked memory and do a cleanup at that time. So I wrapped the
leaking C++ DLL in a C# assembly and loaded it, saw it leak memory,
then I unloaded the assembly and and saw it being unloaded. But the
memory was still missing...
3. Then I did the only thing I new would work, I wrote an out of
process COM object that I kill off with Marshal.ReleaseComObject to get
my memory back. I have thought about putting the logic into the COM
object itself, i.e. it would perform harakiri when consuming too much
memory or just with a certain interval or inactivity timeout. But this
approach does seem like a bit of a fudge to me...
So my question is:
Is there anyone out there who has found an elegant way of cleaning up
unmanaged memory leak using .Net?
Best regards,
Ragnar Agustsson
I have been wandering about the best way to sandbox memory leaks in 3rd
party libraries when using them from the .Net framework.
I have a 3rd party library, written in C++, that leaks a lot of memory
but I still had to use it.
1.
After using DLLImport and seeing the memory leak I tried to load and
unload the library using the kernel's LoadLibrary and FreeLibrary. That
did not work as the leaked memory was not reclamed, not surprising as
my main .Net app's process was still alive.
2.
Then I read about AppDomain, that sounded pretty cool as it can unload
assemblies. So my hope was that the GC would be intelligent enough to
see the leaked memory and do a cleanup at that time. So I wrapped the
leaking C++ DLL in a C# assembly and loaded it, saw it leak memory,
then I unloaded the assembly and and saw it being unloaded. But the
memory was still missing...
3. Then I did the only thing I new would work, I wrote an out of
process COM object that I kill off with Marshal.ReleaseComObject to get
my memory back. I have thought about putting the logic into the COM
object itself, i.e. it would perform harakiri when consuming too much
memory or just with a certain interval or inactivity timeout. But this
approach does seem like a bit of a fudge to me...
So my question is:
Is there anyone out there who has found an elegant way of cleaning up
unmanaged memory leak using .Net?
Best regards,
Ragnar Agustsson