How to work around memory-leaking 3rd party DLL (no source code) accessed by Tomcat application?
We have a project where a 3rd party DLL is accessed through JacoZoom in a Tomcat application. Apparently the DLL leaks memory (confirmed by the vendor), but the vendor has no intention to fix this. The memory leak forces Tomcat to be restarted at regular intervals, which is naturally a great inconvenience for the users.
What would be the best way to work around this problem? One option we consider is having two instances of the Tomcat server and regularly restarting the other one, and redirecting the user to the other one.
Edit: solved by creating another DLL which kills and recreates the vendor DLL when needed. Basically these three kernel32 calls were used to accomplish the functionality:
Private Declare Function GetModuleHandle Lib "kernel32.dll" Alias "GetModuleHandleW" (ByVal DllName As Long) As Long
Privat开发者_开发知识库e Declare Function FreeLibrary Lib "kernel32" (ByVal hLibModule As Long) As Long
Private Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As Long
Luckily the JacoZoom JAR file doesn't seem to mind that the DLL is killed and recreated.
I'm assuming that obvious things like "don't use that DLL" aren't on the table.
Can you create a wrapper, service, or layer around the crappy DLL that can be managed and restarted independently, and have Tomcat / Jacozoom / whatever call that service instead? In a sense, moving the memory leak to some other application outside the container?
I think Mike's suggestion of using a wrapper is the only way for you to go really.
You could write a COM server that hosts the 3rd party control and access it from your application. The wrapper process would still of course leak but you could arrange it such that it exits when there are no outstanding references to the hosted library.
You could also potentially use tools like LeakDiag to see if you can figure out where the leaks are coming from and try to persuade your reluctant vendor to play ball.
[Edit: exists->exits - Thanks Mark.]
精彩评论