我对python函数的内存使用感到困惑。
我正在运行一个函数,其中返回pandas数据帧(1161 X 240),参数为(bamfile,熊猫.数据帧(1161 X 50))。在
现在我将通过profiler给出内存使用情况:Line # Mem usage Increment Line Contents
================================================
120 983.363 MiB 0.000 MiB @profile
121 def overlapping_peaks_distribution(bam_peak1, overlap_df):
122 '''
123 Returns dataframe for tag count distribution for overlapping peaks within 500bp (+,-) from summit.
124 This function also considers the gene transcrition direction.
125 :param bam_peak1:
126 :param overlap_df:
127 :return:
128 '''
129 983.363 MiB 0.000 MiB import pandas as pd
130 983.363 MiB 0.000 MiB import sys
131 983.363 MiB 0.000 MiB peak_distribution_sample = pd.DataFrame()
132 983.363 MiB 0.000 MiB print 'Process: Feature extraction from BAM started'
133 1783.645 MiB 800.281 MiB for ind, row in overlap_df.iterrows():
134 1782.582 MiB -1.062 MiB sys.stdout.write("\rFeature extraction for peak:%d" % ind)
135 1782.582 MiB 0.000 MiB sys.stdout.flush()
136 1782.582 MiB 0.000 MiB chr = str(row['chr'])
137 1782.582 MiB 0.000 MiB orientation = row['Next transcript strand']
138 1782.582 MiB 0.000 MiB middle = row['start'] + row['summit']
139 1782.582 MiB 0.000 MiB start = middle - 3000
140 1782.582 MiB 0.000 MiB stop = start + 50
141 1782.582 MiB 0.000 MiB list_sample1 = []
142 #total_tags = int(bam_peak1.mapped) will get total no of mapped reads
143
144 1782.586 MiB 0.004 MiB for i in range(0, 120):
145 1782.586 MiB 0.000 MiB tags1 = bam_peak1.count(chr, start, stop)
146 1782.586 MiB 0.000 MiB start = stop
147 1782.586 MiB 0.000 MiB stop = start + 50 # divide peaks into length of 25 bp
148 1782.586 MiB 0.000 MiB list_sample1.append(tags1)
149 1782.586 MiB 0.000 MiB if orientation > 0: # Direction gene transcription
150 #print 'Towards 5 prime'
151 1780.883 MiB -1.703 MiB peak_distribution_sample = peak_distribution_sample.append(pd.Series(list_sample1), ignore_index=True)
152 else:
153 #print 'Towards 3 prime'
154 1783.645 MiB 2.762 MiB peak_distribution_sample = peak_distribution_sample.append(pd.Series(list_sample1[::-1]), ignore_index=True)
155 #print peak_distribution_sample
156 1783.645 MiB 0.000 MiB return peak_distribution_sample
我不明白为什么在第133行,它的增量是800MB(疯狂)。这是在吞噬我记忆中所有的空间。我不知道这是我的错吗?在
我用对象图来查找内存泄漏。
函数启动前的对象数:
^{pr2}$
函数完成后的对象数。在(Pdb) import objgraph
(Pdb) objgraph.show_growth()
function 16360 +1067
dict 3546 +460
list 2459 +354
tuple 4414 +306
getset_descriptor 1508 +273
builtin_function_or_method 1895 +240
weakref 2049 +215
module 593 +123
wrapper_descriptor 1877 +117
type 1341 +109
我们可以看到物体的显著增加。
我还制作了一些图表。
我相信红色字体框应该被释放,但它们没有。在