我正在处理一个问题,这个问题需要在集合上大量使用add和remove操作。集合的总大小保持不变(即,以后替换相同数量的已删除元素)。然而,我观察到内存消耗随着时间的推移而增加。
这是产生问题的代码的简化:
import psutil, os
def mem():
return psutil.Process(os.getpid()).memory_info().rss / 1024**2
print("START:", mem())
s = set()
for i in range(2000000):
s.add((i, i))
print(f"FILLED WITH 'add()' ({len(s)} items):", mem())
for i in range(2000000):
s.remove((i, i))
print(f"REMOVED ({len(s)} items):", mem())
for i in range(2000000):
s.add((i, i - 1))
print(f"FILLED WITH 'add()' ({len(s)} items):", mem())
在我的机器(ubuntu 20.04,python 3.8.5)中,此代码生成以下输出:
START: 11.76953125
FILLED WITH 'add()' (2000000 items): 261.9609375
REMOVED (0 items): 76.61328125
FILLED WITH 'add()' (2000000 items): 323.88671875
如果我用一个新的集合替换集合s,我将获得以下值:
import psutil, os
def mem():
return psutil.Process(os.getpid()).memory_info().rss / 1024**2
print("START:", mem())
s = set()
for i in range(2000000):
s.add((i, i))
print(f"FILLED WITH 'add()' ({len(s)} items):", mem())
for i in range(2000000):
s.remove((i, i))
print(f"REMOVED ({len(s)} items):", mem())
s = set()
print(f"NEW SET:", mem())
for i in range(2000000):
s.add((i, i - 1))
print(f"FILLED WITH 'add()' ({len(s)} items):", mem())
START: 11.7265625
FILLED WITH 'add()' (2000000 items): 261.91015625
REMOVED (0 items): 76.56640625
NEW SET: 12.5625
FILLED WITH 'add()' (2000000 items): 354.44921875
我对这些记忆增长有点困惑。
谢谢你的帮助!
暂无答案!
目前还没有任何答案,快来回答吧!