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```
      
	
%%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()
Copy
	
Overwriting memory_profiler_decorator.py

Now we run it using -m memory_profiles memory_profiler_decorator.py

	
!python -m memory_profiler memory_profiler_decorator.py
Copy
	
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```
      
	
!mprof run memory_profiler_decorator.py
Copy
	
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
	
!mprof plot
Copy
	
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?

Subtify

Subtify Subtify

Subtitle generator for videos in the language you want. Also, it puts a different color subtitle to each person

View all projects -->

Do you want to apply AI in your project? Contact me!

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 -->

Do you want to apply AI in your project? Contact me!

Do you want to train your model with these datasets?

short-jokes-dataset

Dataset with jokes in English

opus100

Dataset with translations from English to Spanish

netflix_titles

Dataset with Netflix movies and series

View more datasets -->