How to handle memory leak in an external DLL

I have an external (.Net) dll that I have to use for the application. Unfortunately, this DLL has several known memory leaks. We are working to ensure that the dll authors fix memory leaks, but at the same time I was wondering what is the best way to use a DLL without dealing with memory leaks?

+3
source share
5 answers

Moving the .Net assembly to your own AppDomain so that you can unload the module periodically can help if there are leaks due to the fact that they hang on object references for too long, but most of the memory leak situations I encountered. Pure managed code does not actually allow unmanaged resource handles to be freed โ€” releasing descriptors, closing files, closing network connections, etc.

If the resources belong to a responsible .Net managed object that frees its unmanaged descriptors in IDispose.Close (), but some backbone code does not call IDispose.Close when it suggested, forcing a GC loop or unloading the AppDomain assembly can help return these lost unmanaged resources by "acceleration" of the possible disposal of GC-owned managed objects.

, , AppDomain .

Windows, , , . , GC AppDomain , .NET .NET, . , , . , . , , .

. , , , AppDomain .

+3

, .NET Reflector , , , .

. , , , . , .NET Reflector.

+2

, ( Java), , . , , : , , - . , , , , .

, , DLL, , , DLL " ".

+1
source

If this is a serious problem, load the assembly into a separate AppDomain and instantiate your dodgy classes separately. It also provides a good safety margin.

If there are static elements on the variables, you cannot do anything about the leak in your current appdomain, so using another is a great way to isolate components.

Once an assembly is loaded into IAppDomain, it cannot be unloaded, but the entire AppDomain can be torn down and the GC will assemble it.

0
source

Source: https://habr.com/ru/post/1738592/


All Articles