Memray: a memory profiler for Python
github.com
github.com
This sounds like it does peak memory, which is critical for batch jobs, since that's the bottleneck. Memory is fundamentally different than performance in that it's a limited resource, instead of cumulative cost; making any part of the program faster almost always helps speed up the program (at least a little, or at least reduces CPU load), but optimizing non-peak memory has no impact. You have to be able to identify the peak in order to reduce memory usage.
If you want peak memory profiling for Python that also runs on macOS, check out https://pythonspeed.com/fil/ (ARM support has some issues, but once I unpack my new Mac Mini I plan to fix it.)
Ways memray is better than Fil:
- Native callstacks.
- More kinds of reports, and ability to do custom post-processing of data.
- Much lower overhead (but not always, see reply).
- Subprocess support.
Fil I suspect has better flamegraphs: https://pythonspeed.com/articles/a-better-flamegraph/
And if you're running Python batch jobs, and want both peak memory and performance profiling in production, check out Sciagraph: https://pythonspeed.com/sciagraph/
(You can probably cobble together something like Sciagraph with py-spy + memray, but you won't e.g. get timeline reports designed with batch jobs in mind.)
>> - Much lower overhead, sounds like.
I actually think Fil has the potential to be faster in some situations because it seems that aggregates the flamegraph in memory. Memray needs to do a ton of I/O to disk holding a lock so if is under very heavy pressure it will be a bit slow.
Here are some non-very scientific tests running the "test_list" file from the CPython test suite:
* With pymalloc active (not a lot of heavy presure):
$ fil-profile run -m test test_list ... Total duration: 278 ms
$ memray3.10 run -m test test_list ... Total duration: 128 ms
* With pymalloc not active (heavy presure):
$ PYTHONMALLOC=malloc fil-profile run -m test test_list ... Total duration: 278 ms
$ PYTHONMALLOC=malloc memray3.10 run -m test test_list
Total duration: 344 ms
So as you can see Fil is 20% faster than memray in this scenario. This means that Fil is doing a fantastic job! We spent a lot of time optimizing memray and the fact that Fil can beat it is a testament to Fil's quality :)
In some sense this is only true if you're the end user of a platform, if you're trying to pack jobs onto machines then you actually do care about the utilization at any given time, since you can oversubscribe based on someone's max usage.
E.g., you can give everyone a limit based on their peak memory, but then bin pack based on their actual usage (and evict when you're wrong)
It also shows good engineering practices. I'd venture a guess that they are probably one of the better ones in the space.
Makes me want to work there! Maybe one day...
It would make more sense to say "financial data organization", which then makes it a bit more obvious why we'd have a whole lot of employees (6,500+) in Engineering compared to a typical "financial organization" making money primarily by participating in the public/private markets.
Not to discount how great it is that Bloomberg is contributing this to the community, but there are a lot of other interesting open source tools that have been open sourced by by finance firms.
Pandas, for one, was originally developed internally at the hedge fund AQR.
https://perspective.finos.org/ (JP Morgan)
https://github.com/jpmorganchase/regular-table (JP Morgan)
https://github.com/man-group/dtale (Man Numeric)
https://github.com/deshaw/pyflyby (DE Shaw)
My experience has been that the concurrent nature of coroutines can make it hard to reason about what's going on at a particular point in time. If you don't know how many things you're awaiting on at a specific moment (and what potential external stuff they may be interacting with), it's not exactly easy to identify memory usage of codepaths.
Disclosure: I work on this :-)
[0] https://app.datadoghq.com/profiling
[1] From https://docs.datadoghq.com/tracing/profiler/search_profiles/
> Heap Live Size
> Shows the amount of heap memory allocated by each function that has not been garbage collected (yet). This is useful for investigating the overall memory usage of your service and identifying potential memory leaks.You can use this to figure out what need to be optimized, but in your case optimization might not need to be easy, in above example you would have to re-implement the 3rd party library in memory efficient way or find an other one
Then, it's not a bad thing to optimize memory usage in any language. Memray could be a great tool for the authors of data science libraries, which Python has plenty of. Their average user probably doesn't have a beefy developer's laptop with 64GB of memory at hand.