【问题标题】:Where is the RAM consumption?RAM消耗在哪里?
【发布时间】:2018-01-10 23:21:00
【问题描述】:

我在使用 Python 2.7 时遇到了 RAM 消耗问题。当我加载腌制对象时,python 进程的内存使用量远远超过 RAM 上的对象大小。 代码如下:

import dill
import time
import psutil
from pympler import asizeof

with open('results/dynamic_cache_lru_0.250gb.pickle', 'rb') as fh:
    cache = dill.load(fh)
    print('memory used by cache: %.4fmb' % (asizeof.asizeof(cache) / 1024.0 / 1024))
    mi = psutil.Process().memory_info()
    print('rss: %.4fmb' % (mi[0] / 1024.0 / 1024))
    print('vss: %.4fmb' % (mi[1] / 1024.0 / 1024))

这是我得到的输出:

memory used by cache: 247.1717mb
rss: 905.2266mb
vss: 949.2617mb

我不知道是什么在使用额外的 RAM,或者它是否被对象本身使用,为什么不计入它的大小。

【问题讨论】:

    标签: python python-2.7 memory-management memory-leaks


    【解决方案1】:

    在大约 275000 个字典的列表中运行您的代码时,我看到了类似但不太极端的结果:

    before rss: 14.3086mb
    before vss: 83.2773mb
    memory used by cache: 467.1687mb
    after rss: 745.0195mb
    after vss: 823.6133mb
    

    对此我没有任何解释,但我可以告诉你,仅调用 asizeof.asizeof(cache) 就会使我的内存使用量增加 80 多兆字节:

    before rss: 14.3125mb
    before vss: 83.2773mb
    before asizeof rss: 663.1797mb
    before asizeof vss: 741.8008mb
    memory used by cache: 467.1687mb
    after asizeof rss: 745.0195mb
    after asizeof vss: 823.6055mb
    

    也许如果你做同样的检查,它会帮助你缩小原因。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-26
      • 2015-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多