Monitoring Python CPU and GPU Memory Usage with memory_profiler and Pytorch‑Memory‑Utils
This article introduces the Python libraries memory_profiler and Pytorch‑Memory‑Utils, demonstrates how to measure line‑by‑line CPU memory consumption and GPU memory usage in notebooks and scripts, and explains the additional overhead introduced by PyTorch during model loading.
Understanding the memory consumption of Python code is essential for data processing with pandas and for GPU‑accelerated training, especially when using platforms like Kaggle or Colab.
The article first presents memory_profiler , a tool that can be installed via pip install memory_profiler and loaded in a notebook with %load_ext memory_profiler . After importing profile , the magic command %memit can be used to obtain the peak memory usage and incremental memory of a single line of code.
%memit x = 10 + 5
# Output
peak memory: 54.01 MiB, increment: 0.27 MiBTo profile an entire function, prepend the function call with %memit or use the @profile decorator (which requires the function to be defined in a separate module). The article shows how to create a temporary module using %%file demo.py , decorate the function with @profile , and then run it to obtain a line‑by‑line memory report.
%%file demo.py
from memory_profiler import profile
@profile
def addition():
a = [1] * (10 ** 1)
b = [2] * (3 * 10 ** 2)
sum = a + b
return sumFor full‑script profiling, the article explains that the notebook magic cannot be used; instead, a script should be created and executed from the command line with mprof run script.py and visualized with mprof plot . This yields a graph of memory usage over time.
Next, the article introduces Pytorch‑Memory‑Utils , which allows insertion of memory‑tracking calls within PyTorch code to monitor GPU memory at each line. After importing MemTracker and creating a tracker object, the track() method is called before and after model operations to report the total used GPU memory.
import torch, inspect
from gpu_mem_track import MemTracker
device = torch.device('cuda:0')
frame = inspect.currentframe()
gpu_tracker = MemTracker(frame)
gpu_tracker.track()
cnn = models.vgg19(pretrained=True).to(device)
gpu_tracker.track()The generated report shows the memory occupied by each parameter tensor, revealing that PyTorch incurs additional GPU memory overhead beyond the model weights themselves (e.g., total used memory jumps from 472.2 MiB to 1387.5 MiB). The article notes that this overhead is expected and can be reclaimed after execution, even though it does not appear in nvidia‑smi output.
Overall, the guide provides practical code snippets and explanations for developers who need to profile and optimize both CPU and GPU memory usage in Python data‑science and deep‑learning workflows.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.