Tôi bị nhầm lẫn với việc sử dụng bộ nhớ của python cho một hàm. Tôi đang chạy một hàm trong đó một khung dữ liệu gấu trúc đang được trả về (1161 X 240) và các đối số là (bamfile, pandas.Dataframe (1161 X 50)).Phân bổ bộ nhớ cho vòng lặp trong python
Bây giờ tôi sẽ cung cấp cho việc sử dụng bộ nhớ bằng cách 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
Tôi không hiểu tại sao trong dòng 133 nó increments 800MB (điên). Điều này đang ăn hết tất cả không gian trong trí nhớ của tôi. Tôi không biết đây có phải là lỗi của tôi không?
Tôi đã sử dụng biểu đồ đối tượng để tìm kiếm rò rỉ bộ nhớ. Số đối tượng trước khi chức năng bắt đầu:
(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
Số đối tượng sau khi chức năng kết thúc.
(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
Chúng tôi có thể thấy sự gia tăng đáng kể về các đối tượng. Tôi cũng sản xuất một số đồ thị.
Tôi tin rằng hộp chữ màu đỏ giả sử được giải phóng nhưng không.
Hoàn toàn có thể là 'overlap_df.iterrows()' tải hoàn toàn trong bộ nhớ trước khi bắt đầu lặp lại đầu tiên. – SuperBiasedMan
https://github.com/pydata/pandas/issues/7683 –
Đó có thể là vấn đề nhưng cách giải phóng bộ nhớ đó. –