網(wǎng)站首頁 編程語言 正文
查看程序內(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個數(shù):',psutil.cpu_count())
原文鏈接:https://flyfish.blog.csdn.net/article/details/115214755
相關(guān)推薦
- 2022-06-19 Visual?Studio創(chuàng)建WPF項目_實用技巧
- 2022-07-21 安裝MiniConda和Pytorch以及Cuda的筆記及心得
- 2022-12-08 React狀態(tài)更新的優(yōu)先級機制源碼解析_React
- 2022-11-22 Python?arrow模塊使用方法_python
- 2022-11-23 Python?threading模塊中l(wèi)ock與Rlock的使用詳細講解_python
- 2022-07-11 deepstream 問題
- 2022-05-12 Python繪制計算機CPU占有率變化的折線圖_python
- 2024-03-24 SpringBoot工具庫:解決SpringBoot2.*版本跨域問題
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支