Bloomberg Open-Sources Memray: A Memory Profiler That Can Track Memory Allocations In Python Code In Native Extension Modules And Python Interpreter
This Article Is Based On Github Repo 'bloomberg/memray'. All Credit For This Research Goes To The Researchers Of This tool 👏👏👏 ✍ Submit AI Related News/Story/PR Here Please Don't Forget To Join Our ML Subreddit
All developers will agree that memory is a critical resource when it comes to computing. Owing to its importance, it is necessary we are aware of where it is getting consumed and how to optimize its usage. However, if we attempt to adjust the program blindly without any investigation, it may further slow down our application and this is where memory profiling comes to the rescue. Memory profiling enables us to understand our application’s memory allocation, helping us detect memory leaks or figure out parts of the program that consume the most memory. Â
Memray is a memory profiler developed at Bloomberg, and it is also now open-sourced and can track memory allocation in Python code, be it native extensions or the interpreter itself. In contrast to sampling profilers such as py-spy, Memray has features that set it apart; some of them are:Â
- It can trace every function call so it can accurately represent the call stack, unlike sampling profilersÂ
- It can also handle native calls in C/C++ libraries so the entire call stack is presented in the results.
- It doesn’t slow the application while profiling interpreted code
- Native code profiling tends to be slower but needs to be explicitly enabled.Â
- It can generate various reports on the memory usage data and show it as flame graphs
- Works with both Python and native threads like C++ threads in C extensionÂ
Memray can help memory allocations in applications to help discover the cause of high memory usage, find memory leaks and find hotspots in code that cause a lot of allocations. It can generate memory usage reports as flame graphs that help identify the program’s bottlenecks. As of now Memray works only on Linux and cannot be installed on the platforms.Â
You can download the latest stable release from PyPI using the following pip command.
python3 -m pip install memray
You can use Memray in multiple ways. The easiest is using the CLI to run your application.
python3 -m memray run my_script.py
This generates a .bin file which can be analyzed as flame graphs using the memray flamegraph command.Â
memray flamegraph my_script.2369.bin
This outputs an HTML file with the flame graph that you can inspect with your web browser. Here is an example.Â
The best part is that this memory profiler is open source so you can contribute or modify it according to your needs. Memory profiling is very essential to proper resource utilization. It allows us to identify some of the basic bottlenecks in the code. Resolving these bottlenecks can lead to significant code optimization and, ultimately, cost reduction for the company.
Github: https://github.com/bloomberg/memray
Reference:
- https://www.infoq.com/news/2022/04/bloomberg-memray-python-profiler/
- https://towardsdatascience.com/why-continuous-profiling-can-improve-your-python-application-1c4ec2b238f7
Credit: Source link
Comments are closed.