Python如何做内存监控-memory_profiler
先通过三个步骤
-
第一步下载安装你的memory_profiler
pip install git+https://github.com/pythonprofilers/memory_profiler.git
-
第二步在需要监控的python脚本里导入
#from memory_profiler import profile
- 第三步在你需要监控的函数上面加一个装饰器
@profile
def forward(self, sentence):
embeds = self.word_embeddings(sentence)x = embeds.view(sentence.size()[1], self.batch_size, -1) self.hidden= self.init_hidden() lstm_out, self.hidden = self.lstm(x, self.hidden) y = self.hidden2label(lstm_out[-1]) return y
给一个整体的例子哈, 见test.py
from memory_profiler import profile
@profile
def my_func():
a = [1] * (10 6)
b = [2] * (2 * 10 7)
del b
return a
if name == ‘main’:
my_func()