How can I see the peak memory usage of a datastructure like alist in mojo?

I want to compare the memory usage of python and mojo. For python I use the memory profiler pip package. How can I get the accurate memory consumption of a list or other structure in mojo?
17 Replies
Darkmatter
Darkmatter2mo ago
For a List[T], assuming there are no pointers in T: sizeof[List[T]]() + sizeof[T]() * len(list). The runtime hasn't opened up the kind of memory profiling that is needed to do allocation tracking on that scale. The other option is to allocate a very big list and check the process memory usage.
Maxim
Maxim2mo ago
For Python memory profiling I would recommend Scalene https://github.com/plasma-umass/scalene as it provides data on Python and native memory usage.
GitHub
GitHub - plasma-umass/scalene: Scalene: a high-performance, high-pr...
Scalene: a high-performance, high-precision CPU, GPU, and memory profiler for Python with AI-powered optimization proposals - plasma-umass/scalene
Darkmatter
Darkmatter2mo ago
Scalene won’t work properly with Mojo if you’re on Linux due to some of the transparent hugepage and virtual memory shenanigans that tcmalloc does there.
Maxim
Maxim2mo ago
Right, I did not suggest to use Scalene for Mojo, but try it for Python memory profiling.
whatever
whateverOP2mo ago
I will do it on a macbook m1 16gb ram thanks for your suggestions What would you recommend for mojo? Alternatively I would use something like top to capture the memory consumption of the process
Darkmatter
Darkmatter2mo ago
First, can you set a server nickname? I didn’t look at your name last time and we don’t allow profanity in usernames. As for what I recommend, top might be good enough if you can make the process hand around for a while. Ideally you’d way to use a proper profiler but I have no idea what that is on MacOS, I’m only familiar with Linux and windows dev tooling.
whatever
whateverOP2mo ago
Sorry, set it up when discord made me change my old username and i was angry. No problem.
aurelian
aurelian2mo ago
activity monitor gives you process memory info for a list, isn't it just capacity * element size?
Darkmatter
Darkmatter2mo ago
In python, "element size" is a very interesting question to ask. int takes a variable amount of memory. So anything with an int is variable sized.
aurelian
aurelian2mo ago
python is a hellscape
Darkmatter
Darkmatter2mo ago
Including lists.
aurelian
aurelian2mo ago
now I see why the op was looking for a magic tool wait, ints are boxed??
Darkmatter
Darkmatter2mo ago
🙂
aurelian
aurelian2mo ago
o m g
whatever
whateverOP2mo ago
I have to write a "paper" for a college class and my idea is to compare the memory usage of python lists with differenrt objects in it and compare it to mojo. Maybe also compate it with numpy what would be more fair than python.
Darkmatter
Darkmatter2mo ago
numpy vs mojo is going to be a few bytes.
Want results from more Discord servers?
Add your server