[python-win32] Memory Leak in Threaded COM Object

Mark Hammond skippy.hammond at gmail.com
Tue May 7 02:41:52 CEST 2013


On 4/05/2013 8:04 AM, Michael Watson wrote:
> I am creating a COM client within a thread and performing several
> operations with this client. Each thread is spawned from a server that
> uses Python's |socketserver| module which has built-in threading support.
>
> When I am loading and using this COM object there is an expected spike
> in memory usage by python.exe. With 10 concurrent threads there is a
> peak in memory usage of about 500Mb. However when the operations are
> finished and the COM object is apparently released, there are 50Mb of
> additional memory used by the process than before. If I then spawn 10
> additional threads using the same server there are 13Mb additional used
> by python.exe after those COM objects are closed. Eventually every 10
> additional concurrent threads adds approximately 6Mb after they are
> done. When I end the entire python.exe process, all the memory is released.
>
> I have simplified the following code to mimic how the socketserver uses
> |threadding| and the problem is the exact same.
>
> |import  win32com.client
> import  threading
> import  pythoncom
>
> def  CreateTom():
>      pythoncom.CoInitialize()
>      tom=  win32com.client.Dispatch("TOM.Document")
>      tom.Dataset.Load("FileName")
>      tom.Clear()
>      pythoncom.CoUninitialize()
>
> for  iin  range(50):
>      t=  threading.Thread(target=  CreateTom)
>      t.daemon=  False
>      t.start()|
>
> I understand that I will unlikely get any support here around the
> specific COM library (it is an IBM product used in Market Research known
> as the TablesObjectModel). However I want to know if there is anything,
> ANYTHING, additional that I can do to release this memory. I have read
> about Apartments in COM but it sounds like pythoncom.CoInitialize should
> take care of this for me. Any help would be appreciated.

You say that deleting the object itself doesn't work in your test-case - 
so the problem may turn out to be in the COM object itself.  To 
determine if this is the case, it might be worth seeing if you can 
reproduce the problem with other COM objects - even Python implemented 
ones.  If the object itself leaks, you should also be able to show this 
increased memory usage simply by performing the same steps in sequence 
rather than simultaneously.

HTH,

Mark


More information about the python-win32 mailing list