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,4400
func: consumed memory: 307,261,4401
func: consumed memory: 307,261,4402
func: consumed memory: 307,261,4403
func: consumed memory: 307,261,4404
func: consumed memory: 307,261,4405
func: consumed memory: 307,261,4406_______10_______7
func: consumed memory: 307,261,4408
func: consumed memory: 307,261,4409
pip install -U memory_profiler0
pip install -U memory_profiler1
pip install -U memory_profiler2
pip install -U memory_profiler3
pip install -U memory_profiler4
pip install -U memory_profiler5
pip install -U memory_profiler6
đầ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_profiler7
import
pip install -U memory_profiler9
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 y1
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 y2
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 y4
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 y6
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 y8
sudo pip install psutil [Linux] pip install psutill [Windows]2
# importing the module
0sudo pip install psutil [Linux] pip install psutill [Windows]4
# importing the module
2sudo pip install psutil [Linux] pip install psutill [Windows]2
# importing the module
4 # importing the module
5
# importing the module
6
sudo pip install psutil [Linux] pip install psutill [Windows]0
# importing the module
8sudo pip install psutil [Linux] pip install psutill [Windows]2
sudo pip install psutil [Linux] pip install psutill [Windows]0
import
1import
2import
3import
2import
2import
6
func: consumed memory: 307,261,4406_______144_______8
sudo pip install psutil [Linux] pip install psutill [Windows]4
tracemalloc
0func: consumed memory: 307,261,4406_______145_______2
sudo pip install psutil [Linux] pip install psutill [Windows]4
tracemalloc
4import
2import
3import
2import
2tracemalloc
9func: consumed memory: 307,261,4406_______146_______1
sudo pip install psutil [Linux] pip install psutill [Windows]4
tracemalloc
0func: consumed memory: 307,261,4406_______20_______3
func: consumed memory: 307,261,4401
# code or function for which memory
7# code or function for which memory
8# code or function for which memory
9func: consumed memory: 307,261,4401
# has to be monitored
1# has to be monitored
2
# has to be monitored
1# has to be monitored
4# has to be monitored
5 # has to be monitored
6
func: consumed memory: 307,261,4406_______143_______4
tracemalloc
2sudo pip install psutil [Linux] pip install psutill [Windows]2
# importing the module
4 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]14
import
2 func: consumed memory: 307,261,4401_______0_______17
import
2import
2 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]14
import
2 func: consumed memory: 307,261,4401
sudo pip install psutil [Linux] pip install psutill [Windows]30
import
2 sudo pip install psutil [Linux] pip install psutill [Windows]17
import
2import
2 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 module
4 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]46
import
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,4400
func: consumed memory: 307,261,4401
func: consumed memory: 307,261,4402
func: consumed memory: 307,261,4403
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,4400
func: consumed memory: 307,261,4401
func: consumed memory: 307,261,4402
func: consumed memory: 307,261,4403
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 module
4 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.