Python sử dụng quá nhiều bộ nhớ

Quản lý bộ nhớ rất quan trọng trong bất kỳ logic lập trình nào nhưng điều này trở nên cần thiết đối với python. Vì python được sử dụng trong Ml và AI, nơi sử dụng dữ liệu lớn cần được quản lý. Rò rỉ bộ nhớ, tôi. e, chương trình hết bộ nhớ sau khi chạy vài giờ. Để quản lý những rò rỉ bộ nhớ này, giám sát bộ nhớ là điều cần thiết. Giám sát bộ nhớ còn được gọi là lập hồ sơ. Với tư cách là nhà phát triển, điều cần thiết là chúng tôi lập hồ sơ chương trình của mình và sử dụng ít cấp phát bộ nhớ nhất có thể

Phương pháp 1. Sử dụng Tracemalloc

Tracemalloc là một mô-đun thư viện theo dõi mọi khối bộ nhớ trong python. Truy tìm bắt đầu bằng cách sử dụng start[] trong thời gian chạy. Mô-đun thư viện này cũng có thể cung cấp thông tin về tổng kích thước, số lượng và kích thước trung bình của các khối bộ nhớ được phân bổ. Cho phép chức năng của nó với một ví dụ thích hợp-

Python3




# importing the module

import tracemalloc

 

# code or function for which memory

# has to be monitored

sudo pip install psutil [Linux]
pip install psutill [Windows]
0
sudo pip install psutil [Linux]
pip install psutill [Windows]
1

sudo pip install psutil [Linux]
pip install psutill [Windows]
2
sudo pip install psutil [Linux]
pip install psutill [Windows]
3_______0_______4
sudo pip install psutil [Linux]
pip install psutill [Windows]
5

sudo pip install psutil [Linux]
pip install psutill [Windows]
2
sudo pip install psutil [Linux]
pip install psutill [Windows]
7
sudo pip install psutil [Linux]
pip install psutill [Windows]
8
sudo pip install psutil [Linux]
pip install psutill [Windows]
9
func: consumed memory: 307,261,440
0
func: consumed memory: 307,261,440
1
func: consumed memory: 307,261,440
2
func: consumed memory: 307,261,440
3
func: consumed memory: 307,261,440
4
func: consumed memory: 307,261,440
5

func: consumed memory: 307,261,440
6_______10_______7

 

func: consumed memory: 307,261,440
8

func: consumed memory: 307,261,440
9

 

pip install -U memory_profiler
0

pip install -U memory_profiler
1

 

pip install -U memory_profiler
2

pip install -U memory_profiler
3
pip install -U memory_profiler
4

 

pip install -U memory_profiler
5

pip install -U memory_profiler
6

đầu ra

Đầu ra được đưa ra ở dạng [hiện tại, cực đại], tôi. e, bộ nhớ hiện tại là bộ nhớ mà mã hiện đang sử dụng và bộ nhớ cao nhất là dung lượng tối đa mà chương trình đã sử dụng trong khi thực thi

[0,3617252]

Phương pháp 2. Sử dụng Psutil

Psutil là một thư viện hệ thống python được sử dụng để theo dõi các tài nguyên khác nhau trong hệ thống và việc sử dụng chúng. Thư viện được sử dụng để lập hồ sơ, giới hạn và quản lý tài nguyên quy trình. Để cài đặt cái này, hãy làm như sau-

sudo pip install psutil [Linux]
pip install psutill [Windows]

Hãy xem điều này với một ví dụ. Tất cả những gì bạn phải làm là thêm chức năng trang trí và chức năng process_memory trong mã của bạn và điều này sẽ cung cấp cho bạn bộ nhớ được sử dụng bởi mã và trước và sau

Python3




pip install -U memory_profiler
7

import

pip install -U memory_profiler
9

import

Line #    Mem usage    Increment   Occurrences   Line Contents
============================================================
     2     30.5 MiB     30.5 MiB           1   @profile
     3                                         def my_func[]:
     4     30.6 MiB      0.1 MiB        1003       x = [x for x in range[0,1000]]
     5     30.7 MiB      0.1 MiB        1503       y = [y*100 for y in range[0,1500]]
     6     30.7 MiB      0.0 MiB           1       del x
     7     30.7 MiB      0.0 MiB           1       return y  
1

 

Line #    Mem usage    Increment   Occurrences   Line Contents
============================================================
     2     30.5 MiB     30.5 MiB           1   @profile
     3                                         def my_func[]:
     4     30.6 MiB      0.1 MiB        1003       x = [x for x in range[0,1000]]
     5     30.7 MiB      0.1 MiB        1503       y = [y*100 for y in range[0,1500]]
     6     30.7 MiB      0.0 MiB           1       del x
     7     30.7 MiB      0.0 MiB           1       return y  
2

sudo pip install psutil [Linux]
pip install psutill [Windows]
0
Line #    Mem usage    Increment   Occurrences   Line Contents
============================================================
     2     30.5 MiB     30.5 MiB           1   @profile
     3                                         def my_func[]:
     4     30.6 MiB      0.1 MiB        1003       x = [x for x in range[0,1000]]
     5     30.7 MiB      0.1 MiB        1503       y = [y*100 for y in range[0,1500]]
     6     30.7 MiB      0.0 MiB           1       del x
     7     30.7 MiB      0.0 MiB           1       return y  
4

sudo pip install psutil [Linux]
pip install psutill [Windows]
2
Line #    Mem usage    Increment   Occurrences   Line Contents
============================================================
     2     30.5 MiB     30.5 MiB           1   @profile
     3                                         def my_func[]:
     4     30.6 MiB      0.1 MiB        1003       x = [x for x in range[0,1000]]
     5     30.7 MiB      0.1 MiB        1503       y = [y*100 for y in range[0,1500]]
     6     30.7 MiB      0.0 MiB           1       del x
     7     30.7 MiB      0.0 MiB           1       return y  
6
sudo pip install psutil [Linux]
pip install psutill [Windows]
4
Line #    Mem usage    Increment   Occurrences   Line Contents
============================================================
     2     30.5 MiB     30.5 MiB           1   @profile
     3                                         def my_func[]:
     4     30.6 MiB      0.1 MiB        1003       x = [x for x in range[0,1000]]
     5     30.7 MiB      0.1 MiB        1503       y = [y*100 for y in range[0,1500]]
     6     30.7 MiB      0.0 MiB           1       del x
     7     30.7 MiB      0.0 MiB           1       return y  
8

sudo pip install psutil [Linux]
pip install psutill [Windows]
2# importing the module0
sudo pip install psutil [Linux]
pip install psutill [Windows]
4 # importing the module2

sudo pip install psutil [Linux]
pip install psutill [Windows]
2# importing the module4 # importing the module5

 

# importing the module6

sudo pip install psutil [Linux]
pip install psutill [Windows]
0 # importing the module8

sudo pip install psutil [Linux]
pip install psutill [Windows]
2
sudo pip install psutil [Linux]
pip install psutill [Windows]
0 import1import2import3import2import2import6

 

func: consumed memory: 307,261,440
6_______144_______8
sudo pip install psutil [Linux]
pip install psutill [Windows]
4 tracemalloc0

func: consumed memory: 307,261,440
6_______145_______2
sudo pip install psutil [Linux]
pip install psutill [Windows]
4 tracemalloc4import2import3import2import2tracemalloc9

func: consumed memory: 307,261,440
6_______146_______1
sudo pip install psutil [Linux]
pip install psutill [Windows]
4 tracemalloc0

func: consumed memory: 307,261,440
6_______20_______3
func: consumed memory: 307,261,440
1# code or function for which memory7# code or function for which memory8# code or function for which memory9
func: consumed memory: 307,261,440
1

# has to be monitored1# has to be monitored2

# has to be monitored1# has to be monitored4# has to be monitored5 # has to be monitored6

 

func: consumed memory: 307,261,440
6_______143_______4 tracemalloc2

sudo pip install psutil [Linux]
pip install psutill [Windows]
2# importing the module4
sudo pip install psutil [Linux]
pip install psutill [Windows]
02

 

sudo pip install psutil [Linux]
pip install psutill [Windows]
03

sudo pip install psutil [Linux]
pip install psutill [Windows]
04

 

sudo pip install psutil [Linux]
pip install psutill [Windows]
05

sudo pip install psutil [Linux]
pip install psutill [Windows]
06

sudo pip install psutil [Linux]
pip install psutill [Windows]
0
sudo pip install psutil [Linux]
pip install psutill [Windows]
08

sudo pip install psutil [Linux]
pip install psutill [Windows]
2
sudo pip install psutil [Linux]
pip install psutill [Windows]
10_______0_______4
sudo pip install psutil [Linux]
pip install psutill [Windows]
12
sudo pip install psutil [Linux]
pip install psutill [Windows]
13
sudo pip install psutil [Linux]
pip install psutill [Windows]
14import2
func: consumed memory: 307,261,440
1_______0_______17 import2import2
sudo pip install psutil [Linux]
pip install psutill [Windows]
20
sudo pip install psutil [Linux]
pip install psutill [Windows]
21

sudo pip install psutil [Linux]
pip install psutill [Windows]
2
sudo pip install psutil [Linux]
pip install psutill [Windows]
23
sudo pip install psutil [Linux]
pip install psutill [Windows]
4
sudo pip install psutil [Linux]
pip install psutill [Windows]
12_______0_______26
sudo pip install psutil [Linux]
pip install psutill [Windows]
14import2
func: consumed memory: 307,261,440
1
sudo pip install psutil [Linux]
pip install psutill [Windows]
30 import2
sudo pip install psutil [Linux]
pip install psutill [Windows]
17 import2import2
sudo pip install psutil [Linux]
pip install psutill [Windows]
35
sudo pip install psutil [Linux]
pip install psutill [Windows]
21

sudo pip install psutil [Linux]
pip install psutill [Windows]
2
sudo pip install psutil [Linux]
pip install psutill [Windows]
38
sudo pip install psutil [Linux]
pip install psutill [Windows]
10

sudo pip install psutil [Linux]
pip install psutill [Windows]
2# importing the module4
sudo pip install psutil [Linux]
pip install psutill [Windows]
23

 

sudo pip install psutil [Linux]
pip install psutill [Windows]
43

đầu ra

func: consumed memory: 307,261,440

Phương pháp 3. Sử dụng trình biên dịch bộ nhớ cổ điển

Trình lược tả bộ nhớ từ PyPI là mô-đun thư viện python được sử dụng để theo dõi bộ nhớ tiến trình. Nó sử dụng mã psutil để tạo một trình trang trí và sau đó sử dụng nó để phân phối bộ nhớ. Với mô-đun pypi này bằng cách nhập, người ta có thể lưu các dòng và gọi trực tiếp trình trang trí. Để cài đặt sử dụng như sau-

pip install -U memory_profiler

Hãy xem điều này thông qua một đoạn mã-

Python3




sudo pip install psutil [Linux]
pip install psutill [Windows]
44

sudo pip install psutil [Linux]
pip install psutill [Windows]
45
sudo pip install psutil [Linux]
pip install psutill [Windows]
46import
sudo pip install psutil [Linux]
pip install psutill [Windows]
48

 

sudo pip install psutil [Linux]
pip install psutill [Windows]
49

sudo pip install psutil [Linux]
pip install psutill [Windows]
04

sudo pip install psutil [Linux]
pip install psutill [Windows]
51

sudo pip install psutil [Linux]
pip install psutill [Windows]
52

sudo pip install psutil [Linux]
pip install psutill [Windows]
0
sudo pip install psutil [Linux]
pip install psutill [Windows]
54

sudo pip install psutil [Linux]
pip install psutill [Windows]
2
sudo pip install psutil [Linux]
pip install psutill [Windows]
10_______0_______4
sudo pip install psutil [Linux]
pip install psutill [Windows]
58_______0_______7
sudo pip install psutil [Linux]
pip install psutill [Windows]
10
sudo pip install psutil [Linux]
pip install psutill [Windows]
9
func: consumed memory: 307,261,440
0
func: consumed memory: 307,261,440
1
func: consumed memory: 307,261,440
2
func: consumed memory: 307,261,440
3
sudo pip install psutil [Linux]
pip install psutill [Windows]
66
sudo pip install psutil [Linux]
pip install psutill [Windows]
67

sudo pip install psutil [Linux]
pip install psutill [Windows]
2
sudo pip install psutil [Linux]
pip install psutill [Windows]
23
sudo pip install psutil [Linux]
pip install psutill [Windows]
4
sudo pip install psutil [Linux]
pip install psutill [Windows]
71_______144_______2
sudo pip install psutil [Linux]
pip install psutill [Windows]
73
sudo pip install psutil [Linux]
pip install psutill [Windows]
7
sudo pip install psutil [Linux]
pip install psutill [Windows]
23
sudo pip install psutil [Linux]
pip install psutill [Windows]
9
func: consumed memory: 307,261,440
0
func: consumed memory: 307,261,440
1
func: consumed memory: 307,261,440
2
func: consumed memory: 307,261,440
3
sudo pip install psutil [Linux]
pip install psutill [Windows]
81
sudo pip install psutil [Linux]
pip install psutill [Windows]
67

sudo pip install psutil [Linux]
pip install psutill [Windows]
2
sudo pip install psutil [Linux]
pip install psutill [Windows]
38
sudo pip install psutil [Linux]
pip install psutill [Windows]
10

sudo pip install psutil [Linux]
pip install psutill [Windows]
2# importing the module4
sudo pip install psutil [Linux]
pip install psutill [Windows]
23

 

sudo pip install psutil [Linux]
pip install psutill [Windows]
89
sudo pip install psutil [Linux]
pip install psutill [Windows]
90_______0_______4_______0_______4
sudo pip install psutil [Linux]
pip install psutill [Windows]
93
sudo pip install psutil [Linux]
pip install psutill [Windows]
94

sudo pip install psutil [Linux]
pip install psutill [Windows]
2
sudo pip install psutil [Linux]
pip install psutill [Windows]
96

đầu ra

Đầu ra hiển thị bộ nhớ được sử dụng bởi mỗi dòng trong mã. Việc triển khai tìm mức tiêu thụ bộ nhớ rất dễ dàng bằng cách sử dụng trình lược tả bộ nhớ khi chúng ta gọi trực tiếp trình trang trí thay vì viết mã hoàn toàn mới.  

Chủ Đề