欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

對(duì)python程序內(nèi)存泄漏調(diào)試的記錄

 更新時(shí)間:2018年06月11日 14:41:12   作者:ybdesire  
今天小編就為大家分享一篇對(duì)python程序內(nèi)存泄漏調(diào)試的記錄,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧

問題描述

調(diào)試python程序時(shí),用下面這段代碼,可以獲得進(jìn)程占用系統(tǒng)內(nèi)存值。程序跑一段時(shí)間后,就能畫出進(jìn)程對(duì)內(nèi)存的占用情況。

def memory_usage_psutil():
 # return the memory usage in MB
 import psutil,os
 process = psutil.Process(os.getpid())
 mem = process.memory_info()[0] / float(2 ** 20)
 return mem

發(fā)現(xiàn)進(jìn)程的內(nèi)存占用一直再上漲,而這從邏輯上來說是不正常的,所以想到程序可能發(fā)生了Memory Leak。

python程序的Mem Leak

python程序不可能像C/C++一樣出現(xiàn)malloc了的內(nèi)存沒有free這樣的Memory Leak。但也會(huì)遇到“邏輯上沒free”的情況,如下代碼所示。

def foo(a=[]):
 a.append(time.time())
 return a

參數(shù)a這樣可迭代的對(duì)象,稍不注意,它就能增長(zhǎng)的很快。說白了,python的Memory Leak,就是“進(jìn)程占用的內(nèi)存莫名其妙一直再升高”。進(jìn)程占用內(nèi)存一直升高,與邏輯預(yù)期不一致,就可能發(fā)生了Memory Leak。

以下面程序?yàn)槔f明Memory Leak調(diào)試的過程:

def memory_usage_psutil():
 # return the memory usage in MB
 import psutil,os
 process = psutil.Process(os.getpid())
 mem = process.memory_info()[0] / float(2 ** 20)
 return mem

def get_current_obj(a=[]):
 a.append([0]*1000)
 return a

def main(): 
 obj = []
 for i in range(10000):
 obj = get_current_obj(obj)
 if(i%100==0):
  print(memory_usage_psutil())

if __name__=='__main__':
 main()

調(diào)試過程

用pmap -x [pid]查看進(jìn)程占用的堆內(nèi)存大小

首先想到,會(huì)不會(huì)是上面用的memory_usage_psutil函數(shù)統(tǒng)計(jì)錯(cuò)誤呢。

先運(yùn)行程序,再用pmap查看,發(fā)現(xiàn)進(jìn)程內(nèi)存占用確實(shí)很高。多次執(zhí)行該命令,也可以發(fā)現(xiàn)內(nèi)存一直升高。

強(qiáng)制執(zhí)行GC(gc.collect())

在需要執(zhí)行GC的地方加上gc.collect()

def main(): 
 obj = []
 for i in range(10000):
 obj = get_current_obj(obj)
 import gc;gc.collect()
 if(i%100==0):
  print(memory_usage_psutil())

可以看到,強(qiáng)制GC后,程序執(zhí)行變慢,但內(nèi)存依然不斷升高。

使用memory_profiler查看

安裝memory_profiler

pip install -U memory_profiler

用@profile修飾需要查看內(nèi)存的函數(shù)

@profile
def main(): 
 obj = []
 for i in range(10000):
 obj = get_current_obj(obj)
 if(i%100==0):
  print(memory_usage_psutil())

用如下命令運(yùn)行程序

python -m memory_profiler main.py

可以看到程序執(zhí)行完成后,輸出結(jié)果如下

Line # Mem usage Increment Line Contents
================================================
 12 28.570 MiB 0.000 MiB @profile
 13    def main():
 14 28.570 MiB 0.000 MiB obj = []
 15 106.203 MiB 77.633 MiB for i in range(10000):
 16 106.203 MiB 0.000 MiB  obj = get_current_obj(obj)
 17 106.203 MiB 0.000 MiB  if(i%100==0):
 18 105.445 MiB -0.758 MiB  print(memory_usage_psutil())

這樣就能看到導(dǎo)致內(nèi)存上漲最快的那幾行代碼。

用guppy查看python對(duì)象占用的堆內(nèi)存大小

將main修改如下,即可查看python對(duì)堆內(nèi)存的占用量。

def main(): 
 obj = []
 for i in range(10000):
 obj = get_current_obj(obj)
 if(i%100==0):
  print(memory_usage_psutil())
  from guppy import hpy;hxx = hpy();heap = hxx.heap()
  print(heap)

下面就是輸出結(jié)果,python程序中各個(gè)對(duì)象對(duì)內(nèi)存的占用從大到小排列。

 Index Count % Size % Cumulative % Kind (class / dict of class)
 0 10124 22 81944416 95 81944416 95 list
 1 16056 34 1325464 2 83269880 96 str
 2 9147 20 745616 1 84015496 97 tuple
 3 102 0 366480 0 84381976 98 dict of module
 4 287 1 313448 0 84695424 98 dict of type
 5 2426 5 310528 0 85005952 98 types.CodeType
 6 2364 5 283680 0 85289632 99 function
 7 287 1 256960 0 85546592 99 type
 8 169 0 192088 0 85738680 99 dict (no owner)
 9 123 0 142728 0 85881408 99 dict of class

可以從結(jié)果中看到,95%的進(jìn)程內(nèi)存,都被一個(gè)list占用。

還可以通過下面這種方式,查看這個(gè)占內(nèi)存最大的list中的數(shù)據(jù)類型。

from guppy import hpy;hxx = hpy();byrcs = hxx.heap().byrcs; byrcs[0].byid

關(guān)于guppy的詳細(xì)用法,可以看這里(http://smira.ru/wp-content/uploads/2011/08/heapy.html)。

以上這篇對(duì)python程序內(nèi)存泄漏調(diào)試的記錄就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論