强制Python中的垃圾收集释放内存

2024-04-29 22:39:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个Python2.7应用程序,它使用了大量的dict对象,这些对象大多包含键和值的字符串。

有时,这些听写和字符串不再需要了,我想把它们从内存中删除。

我尝试了不同的方法,del dict[key]del dict等等,但是应用程序仍然使用相同的内存量。

下面是一个例子,我希望它能为我的记忆收费。但事实并非如此:

import gc
import resource

def mem():
    print('Memory usage         : % 2.2f MB' % round(
        resource.getrusage(resource.RUSAGE_SELF).ru_maxrss/1024.0/1024.0,1)
    )

mem()

print('...creating list of dicts...')
n = 10000
l = []
for i in xrange(n):
    a = 1000*'a'
    b = 1000*'b'
    l.append({ 'a' : a, 'b' : b })

mem()

print('...deleting list items...')

for i in xrange(n):
    l.pop(0)

mem()

print('GC collected objects : %d' % gc.collect())

mem()

输出:

Memory usage         :  4.30 MB
...creating list of dicts...
Memory usage         :  36.70 MB
...deleting list items...
Memory usage         :  36.70 MB
GC collected objects : 0
Memory usage         :  36.70 MB

我希望这里能收集一些对象,释放一些内存。

我做错什么了吗?删除未使用对象的任何其他方法,或至少查找意外使用对象的位置。


Tags: 对象方法内存字符串import应用程序usagemb
1条回答
网友
1楼 · 发布于 2024-04-29 22:39:17

Frederick Lundh explains

If you create a large object and delete it again, Python has probably released the memory, but the memory allocators involved don’t necessarily return the memory to the operating system, so it may look as if the Python process uses a lot more virtual memory than it actually uses.

and Alex Martelli writes

The only really reliable way to ensure that a large but temporary use of memory DOES return all resources to the system when it's done, is to have that use happen in a subprocess, which does the memory-hungry work then terminates.

因此,可以使用multiprocessing生成子进程,执行内存占用计算,然后确保在子进程终止时释放内存:

import multiprocessing as mp
import resource

def mem():
    print('Memory usage         : % 2.2f MB' % round(
        resource.getrusage(resource.RUSAGE_SELF).ru_maxrss/1024.0,1)
    )

mem()

def memoryhog():
    print('...creating list of dicts...')
    n = 10**5
    l = []
    for i in xrange(n):
        a = 1000*'a'
        b = 1000*'b'
        l.append({ 'a' : a, 'b' : b })
    mem()

proc = mp.Process(target=memoryhog)
proc.start()
proc.join()

mem()

收益率

Memory usage         :  5.80 MB
...creating list of dicts...
Memory usage         :  234.20 MB
Memory usage         :  5.90 MB

相关问题 更多 >