gpt4 book ai didi

python - 内存没有返回给内核

转载 作者:太空狗 更新时间:2023-10-30 00:07:59 25 4
gpt4 key购买 nike

我有一个非常简单的分配内存的脚本,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 anythingLinux 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  Command01: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 memoryLinux 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  Command01: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 docsLinux 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  Command01: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===============================

如何确保将此内存返回给操作系统?

最佳答案

内存在 python 进程内可供重用的时间与释放给操作系统的时间之间可能存在差异。特别是,标准 Python 解释器 (CPython) 为特定类型的对象维护自己的池和空闲列表。它会自己重用这些池中的内存,但不会在使用后将其释放给操作系统。

参见 this了解更多详情。

关于python - 内存没有返回给内核,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11957539/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com