python线程执行完后释放内存,使用Python的线程COM对象中的内存泄漏

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 i in 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.

解决方案

As it turns out this increase in Memory was in fact due to the COM object written in .NET and had nothing to do with threading. Here is a detailed description of Task Manager giving misleading information about memory usage for .NET apps. To resolve this issue I added the following to my code and I am all set. Hopefully someone else reads this response before they start tearing their hair out trying to find a memory leak in their code.

from win32process import SetProcessWorkingSetSize

from win32api import GetCurrentProcessId, OpenProcess

from win32con import PROCESS_ALL_ACCESS

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()

SetProcessWorkingSetSize(handle,-1,-1) #Releases memory after every use

pid = GetCurrentProcessId()

handle = OpenProcess(PROCESS_ALL_ACCESS, True, pid)

for i in range(50):

t = threading.Thread(target = CreateTom)

t.daemon = False

t.start()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值