Linux上的Python内存消耗:物理和虚拟内存正在增长,而堆大小保持不变

时间:2014-04-29 16:12:15

标签: python linux memory-consumption

我正在研究用Python编写的某种系统服务(实际上它只是一个日志解析器)。这个程序应该连续工作很长时间(希望我的意思是几天和几周没有失败和重新启动的需要)。这就是为什么我担心内存消耗。

我将来自不同站点的进程内存使用情况的不同信息汇总到一个简单的函数中:

#!/usr/bin/env python
from pprint import pprint
from guppy import hpy
from datetime import datetime
import sys
import os
import resource
import re

def debug_memory_leak():
    #Getting virtual memory size 
    pid = os.getpid()
    with open(os.path.join("/proc", str(pid), "status")) as f:
        lines = f.readlines()
    _vmsize = [l for l in lines if l.startswith("VmSize")][0]
    vmsize = int(_vmsize.split()[1])

    #Getting physical memory size  
    pmsize = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss

    #Analyzing the dynamical memory segment - total number of objects in memory and heap size
    h = hpy().heap()
    if __debug__:
        print str(h)
    m = re.match(
        "Partition of a set of ([0-9]+) objects. Total size = ([0-9]+) bytes(.*)", str(h))
    objects = m.group(1)
    heap = int(m.group(2))/1024 #to Kb

    current_time = datetime.now().strftime("%H:%M:%S")
    data = (current_time, objects, heap, pmsize, vmsize)
    print("\t".join([str(d) for d in data]))

这个函数已被用来研究我长时间播放过程中内存消耗的动态,我仍然无法解释它的行为。您可以看到,在这20分钟内物理和虚拟内存增加了11%和1%时,堆大小和对象总量没有改变。

UPD :到目前为止,这个过程已经持续了近15个小时。堆仍然相同,但物理内存增加了六倍,虚拟内存增加了50%。除了凌晨3点的异常异常值外,曲线似乎是线性的:

  

Time Obj Heap PhM VM

     

19:04:19 31424 3928 5460 143732

     

19:04:29 30582 3704 10276 158240

     

19:04:39 30582 3704 10372 157772

     

19:04:50 30582 3709 10372 157772

     

19:05:00 30582 3704 10372 157772

     

(...)

     

19:25:00 30583 3704 11524 159900

     

09:53:23 30581 3704 62380 210756

我想知道我的进程的地址空间是怎么回事。堆的常量大小表明所有动态对象都被正确释放。但我毫不怀疑,从长远来看,不断增长的内存消耗将影响这一关键生命过程的可持续性。

enter image description here

有人可以澄清这个问题吗?谢谢。

(我使用RHEL 6.4,内核2.6.32-358和Python 2.6.6)

1 个答案:

答案 0 :(得分:6)

如果不知道你的程序在做什么,这可能会有所帮助。

我在前一段时间处理项目时遇到过这篇文章: http://chase-seibert.github.io/blog/2013/08/03/diagnosing-memory-leaks-python.html 其中说,"长时间运行的Python作业在运行时消耗大量内存可能无法将该内存返回到操作系统,直到进程实际终止,即使所有内容都是垃圾收集正确。"

我最终使用多处理模块让我的项目分支一个单独的进程并在需要工作时返回,并且我从未注意到任何内存问题。

那或在Python 3.3 http://bugs.python.org/issue11849

中尝试