Python 如何查看程序內(nèi)存占用情況
查看程序內(nèi)存占用情況
flyfish
psutil
這里用在查看內(nèi)存占用情況memory_profiler
輸出每一行代碼增減的內(nèi)存
安裝
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這句代碼,執(zhí)行終端命令如下
python -m memory_profiler test.py
結(jié)果
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查看內(nèi)存使用
在程序中使用python查看電腦內(nèi)存,可以使用:
import psutil import os info = psutil.virtual_memory() print(u'內(nèi)存使用:',psutil.Process(os.getpid()).memory_info().rss) print(u'總內(nèi)存:',info.total) print(u'內(nèi)存占比:',info.percent) print(u'cpu個(gè)數(shù):',psutil.cpu_count())
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
使用實(shí)現(xiàn)pandas讀取csv文件指定的前幾行
下面小編就為大家分享一篇使用實(shí)現(xiàn)pandas讀取csv文件指定的前幾行,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-04-04Python自動化測試ConfigParser模塊讀寫配置文件
本文主要介紹Python自動化測試,這里詳細(xì)說明了ConfigParser模塊讀寫配置文件,有興趣的小伙伴可以參考下2016-08-08pytorch實(shí)現(xiàn)mnist手寫彩色數(shù)字識別
這篇文章主要介紹了pytorch-實(shí)現(xiàn)mnist手寫彩色數(shù)字識別,文章圍繞主題展開詳細(xì)的內(nèi)容姐介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09python ImageDraw類實(shí)現(xiàn)幾何圖形的繪制與文字的繪制
這篇文章主要介紹了python ImageDraw類實(shí)現(xiàn)幾何圖形的繪制與文字的繪制,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02