蟒蛇 - 没有还给内存内核(python - memory not being given back

2019-06-27 01:07发布

我有一个非常简单的脚本,分配内存, dels一个相当大的对象仅供参考,所有的同时打印heapypidstat报告。 运行该脚本后,heapy告诉我,虽然pidstat告诉我对面不应该有正在使用多少内存:

from guppy import hpy
import time
import sys
import os

'''
1) print heapy and pidstat report after starting and before actually doing any work
2) allocate some memory in a simple 2d array
3) print heapy and pidstat report
4) del the d2 array (attempt at garbage collection)
5) print heapy and pidstat report
6) sleep so pidstat can continue to be run to check on memory
'''

def pidstat(msg):
    print '==============================='
    print msg
    os.system('pidstat -r -p %s' % os.getpid())
    print '+++++++++++++++++++++++++++++++'
    print hpy().heap()[0]
    print '==============================='

pidstat('before doing anything')
docs = []
for doc in range(0, 10000):
    docs.append([j for j in range(0, 1000)])

pidstat('after fetching all the docs into memory')
del docs

pidstat('after freeing the docs')
time.sleep(60)

输出如下所示:

===============================
before doing anything
Linux 2.6.38-15-generic (hersheezy)     08/14/2012  _x86_64_    (4 CPU)

01:05:20 PM       PID  minflt/s  majflt/s     VSZ    RSS   %MEM  Command
01:05:20 PM      5360      0.44      0.00   44768   9180   0.11  python
+++++++++++++++++++++++++++++++
Partition of a set of 19760 objects. Total size = 1591024 bytes.
 Index  Count   %     Size   % Cumulative  % Kind (class / dict of class)
     0  19760 100  1591024 100   1591024 100 str
===============================
===============================
after fetching all the docs into memory
Linux 2.6.38-15-generic (hersheezy)     08/14/2012  _x86_64_    (4 CPU)

01:05:21 PM       PID  minflt/s  majflt/s     VSZ    RSS   %MEM  Command
01:05:21 PM      5360      8.95      0.00  318656 279120   3.49  python
+++++++++++++++++++++++++++++++
Partition of a set of 7431665 objects. Total size = 178359960 bytes.
 Index  Count   %     Size   % Cumulative  % Kind (class / dict of class)
     0 7431665 100 178359960 100 178359960 100 int
===============================
===============================
after freeing the docs
Linux 2.6.38-15-generic (hersheezy)     08/14/2012  _x86_64_    (4 CPU)

01:05:29 PM       PID  minflt/s  majflt/s     VSZ    RSS   %MEM  Command
01:05:29 PM      5360     40.23      0.00  499984 460480   5.77  python
+++++++++++++++++++++++++++++++
Partition of a set of 19599 objects. Total size = 1582016 bytes.
 Index  Count   %     Size   % Cumulative  % Kind (class / dict of class)
     0  19599 100  1582016 100   1582016 100 str
===============================

我怎样才能确保这个内存返回到操作系统?

Answer 1:

可以有当内存是由可重用内部之间的差异python的过程,当它被释放到操作系统。 特别是,标准Python解释器(CPython的)维护它自己的游泳池和特定类型的对象的自由列表。 它会重复使用这些池本身的内存,但一旦它被用来永远不会释放到操作系统。

请参阅此了解更多详情。



Answer 2:

我怎样才能确保这个内存返回到操作系统?

它一般不会。 Python中的“竞技场”分配内存,即使引用在解释被删除,这将保留该内存竞技场以后使用。 我想在Python来取消声明领域较新版本的机制,如果他们完全是空的。 但是,你必须在那里你的对象得到安置的控制。



文章来源: python - memory not being given back to kernel