python for循环 内存_python中for循环的内存分配

我对

python的内存使用感到困惑.

我正在运行一个返回pandas数据帧的函数(1161 X 240),参数是(bamfile,pandas.Dataframe(1161 X 50)).

现在我将通过探查器给出内存使用情况:

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(疯狂).这占用了我记忆中的所有空间.我不知道这是我的错吗?

我使用对象图来查找内存泄漏.

函数启动前的对象数:

(Pdb) objgraph.show_most_common_types()

function 15293

tuple 4115

dict 3086

cell 2670

list 2107

weakref 1834

wrapper_descriptor 1760

builtin_function_or_method 1655

getset_descriptor 1235

type 1232

功能完成后的对象数量.

(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

我们可以看到对象的显着增加.

我还制作了一些图表.

我相信红色字体框假设被释放但它们不是.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值