日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Python?如何查看程序內存占用情況_python

作者:TheOldManAndTheSea ? 更新時間: 2022-07-09 編程語言

查看程序內存占用情況

  • flyfish
  • psutil 這里用在查看內存占用情況
  • memory_profiler輸出每一行代碼增減的內存

安裝

pip install memory_profiler

代碼

import numpy as np
import os
import psutil
import gc
from memory_profiler import profile

@profile
def test():
? ? a=np.full(shape=(600, 700), fill_value=99.0)
? ? return a

if __name__ == '__main__':

? ? a=test()

? ? print('A:%.2f MB' % (psutil.Process(os.getpid()).memory_info().rss / 1024 / 1024))
? ? del a
? ? gc.collect()
? ? print('B:%.2f MB' % (psutil.Process(os.getpid()).memory_info().rss / 1024 / 1024))

如果沒有from memory_profiler import profile這句代碼,執行終端命令如下

python -m memory_profiler test.py

結果

Line # ? ?Mem usage ? ?Increment ?Occurences ? Line Contents
============================================================
? ? 10 ? ? 53.8 MiB ? ? 53.8 MiB ? ? ? ? ? 1 ? @profile
? ? 11 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? def test():
? ? 12 ? ? 56.8 MiB ? ? ?3.0 MiB ? ? ? ? ? 1 ? ? ? a=np.full(shape=(600, 700), fill_value=99.0)
? ? 13 ? ? 56.8 MiB ? ? ?0.0 MiB ? ? ? ? ? 1 ? ? ? return a


A:56.83 MB
B:53.83 MB

python查看內存使用

在程序中使用python查看電腦內存,可以使用:

import psutil
import os

info = psutil.virtual_memory()
print(u'內存使用:',psutil.Process(os.getpid()).memory_info().rss)
print(u'總內存:',info.total)
print(u'內存占比:',info.percent)
print(u'cpu個數:',psutil.cpu_count())

原文鏈接:https://flyfish.blog.csdn.net/article/details/115214755

欄目分類
最近更新