See the memory usage of a script

See the memory usage of a script See the memory usage of a script

View the memory usage of a scriptlink image 5

Disclaimer: This post has been translated to English using a machine translation model. Please, let me know if you find any mistakes.

If a script is consuming a lot of RAM, we can monitor it to see what is consuming it.

Installationlink image 6

To monitor it, we are going to install memory_profiler. We can install it with Conda.

conda install memory_profiler```
      
      or with pip
      ``` bash
      pip install memory_profiler```
      

Usagelink image 7

Decoratorlink image 8

We can put a decorator on the function we want to monitor.

from memory_profiler import profile
      @profiledef my_function():pass```
      
	
< > Input
Python
%%writefile memory_profiler_decorator.py
from memory_profiler import profile
@profile
def my_function():
a = [1] * (10 ** 6)
b = [2] * (2 * 10 ** 7)
del b
return a
if __name__ == '__main__':
my_function()
Copied
>_ Output
			
Overwriting memory_profiler_decorator.py

Now we run it using -m memory_profiles memory_profiler_decorator.py

	
< > Input
Python
!python -m memory_profiler memory_profiler_decorator.py
Copied
>_ Output
			
Filename: memory_profiler_decorator.py
Line # Mem usage Increment Occurrences Line Contents
=============================================================
3 49.5 MiB 49.5 MiB 1 @profile
4 def my_function():
5 57.0 MiB 7.5 MiB 1 a = [1] * (10 ** 6)
6 209.6 MiB 152.6 MiB 1 b = [2] * (2 * 10 ** 7)
7 57.2 MiB -152.4 MiB 1 del b
8 57.2 MiB 0.0 MiB 1 return a

As we can see, we get the memory usage of each line of the script.

Memory usage over timelink image 9

We can obtain a memory usage graph over time by doing

mprof run memory_profiler_decorator.pymprof plot```
      
	
< > Input
Python
!mprof run memory_profiler_decorator.py
Copied
>_ Output
			
mprof: Sampling memory every 0.1s
running new process
running as a Python program...
Filename: memory_profiler_decorator.py
Line # Mem usage Increment Occurrences Line Contents
=============================================================
3 49.5 MiB 49.5 MiB 1 @profile
4 def my_function():
5 57.0 MiB 7.5 MiB 1 a = [1] * (10 ** 6)
6 209.6 MiB 152.6 MiB 1 b = [2] * (2 * 10 ** 7)
7 57.2 MiB -152.4 MiB 1 del b
8 57.2 MiB 0.0 MiB 1 return a
	
< > Input
Python
!mprof plot
Copied
>_ Output
			
Using last profile data.
Figure(1260x540)

When running it, this graph is created memory_profiler plot

Continue reading

Last posts -->

Have you seen these projects?

Gymnasia

Gymnasia Gymnasia
React Native
Expo
TypeScript
FastAPI
Next.js
OpenAI
Anthropic

Mobile personal training app with AI assistant, exercise library, workout tracking, diet and body measurements

Horeca chatbot

Horeca chatbot Horeca chatbot
Python
LangChain
PostgreSQL
PGVector
React
Kubernetes
Docker
GitHub Actions

Chatbot conversational for cooks of hotels and restaurants. A cook, kitchen manager or room service of a hotel or restaurant can talk to the chatbot to get information about recipes and menus. But it also implements agents, with which it can edit or create new recipes or menus

View all projects -->
>_ Available for projects

Do you have an AI project?

Let's talk.

maximofn@gmail.com

Machine Learning and AI specialist. I develop solutions with generative AI, intelligent agents and custom models.

Do you want to watch any talk?

Last talks -->

Do you want to improve with these tips?

Last tips -->

Use this locally

Hugging Face spaces allow us to run models with very simple demos, but what if the demo breaks? Or if the user deletes it? That's why I've created docker containers with some interesting spaces, to be able to use them locally, whatever happens. In fact, if you click on any project view button, it may take you to a space that doesn't work.

Flow edit

Flow edit Flow edit

FLUX.1-RealismLora

FLUX.1-RealismLora FLUX.1-RealismLora
View all containers -->
>_ Available for projects

Do you have an AI project?

Let's talk.

maximofn@gmail.com

Machine Learning and AI specialist. I develop solutions with generative AI, intelligent agents and custom models.

Do you want to train your model with these datasets?

short-jokes-dataset

HuggingFace

Dataset with jokes in English

Use: Fine-tuning text generation models for humor

231K rows 2 columns 45 MB
View on HuggingFace →

opus100

HuggingFace

Dataset with translations from English to Spanish

Use: Training English-Spanish translation models

1M rows 2 columns 210 MB
View on HuggingFace →

netflix_titles

HuggingFace

Dataset with Netflix movies and series

Use: Netflix catalog analysis and recommendation systems

8.8K rows 12 columns 3.5 MB
View on HuggingFace →
View more datasets -->