網站首頁 編程語言 正文
查看程序內存占用情況
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
相關推薦
- 2022-02-28 ./node_modules/taro-ui/dist/weapp/index.ts Module
- 2022-08-03 使用Apache?Camel表達REST服務的方法_Linux
- 2022-05-22 LazyCaptcha自定義隨機驗證碼和字體的示例詳解_實用技巧
- 2023-07-24 el-table文字根據首字母排序
- 2022-09-01 Python?類方法和靜態方法之間的區別_python
- 2023-12-17 eclipse中設置自動補齊代碼
- 2022-06-06 數據分析數據庫ClickHouse在大數據領域應用實踐_數據庫其它
- 2022-02-27 Required String paramter ‘username‘ is not present
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支