Take your own peak memory usage on Linux & Python

Conclusion

I searched a lot, but I couldn't find a better way. Since it uses the / proc file system, it only works on supported platforms (such as Linux).

import os
import re

def peak_memory():
    pid = os.getpid()
    with open(f'/proc/{pid}/status') as f:
        # "VmHWM:    862168 kB"Extract lines in the format of
        for line in f:
            if not line.startswith('VmHWM:'):
                continue
            return int(re.search('[0-9]+', line)[0])
    raise ValueError('Not Found')

Why peak usage?

Inside a certain function, a phenomenon occurred in which the memory usage temporarily increased.

If you want to profile the function and find out where the memory usage is rising, you should use memory-profiler. This time, I knew where it would increase, and I couldn't help it, but I wanted to find out how much it would increase, so I needed to know the peak usage.

What happens if you don't use peak usage?

You can get the current memory usage with psutil. See this.

However, it is not a peak, so for example, in the case of such a code

Take current memory usage


import psutil

def f():
    a = [0] * 20000000

print(psutil.Process().memory_full_info())
f()
print(psutil.Process().memory_full_info())

The result is as follows.

Not much different


pfullmem(rss=12406784, vms=18395136, shared=6369280, text=4096, lib=0, data=6225920, dirty=0, uss=8134656, pss=9319424, swap=0)
pfullmem(rss=12619776, vms=18354176, shared=6385664, text=4096, lib=0, data=6184960, dirty=0, uss=8253440, pss=9438208, swap=0)

I'm using a lot of memory inside f (), but I don't use it outside, so I can't tell from the outside.

Take it in f


import psutil

def f():
    a = [0] * 20000000
    print('inner: ', psutil.Process().memory_full_info())

print('before:', psutil.Process().memory_full_info())
f()
print('after: ', psutil.Process().memory_full_info())

In this case, it looks like this:

I use a lot only in the middle


before: pfullmem(rss=12476416, vms=18395136, shared=6443008, text=4096, lib=0, data=6225920, dirty=0, uss=8179712, pss=9407488, swap=0)
inner:  pfullmem(rss=172519424, vms=178356224, shared=6520832, text=4096, lib=0, data=166187008, dirty=0, uss=168300544, pss=169528320, swap=0)
after:  pfullmem(rss=12754944, vms=18354176, shared=6520832, text=4096, lib=0, data=6184960, dirty=0, uss=8298496, pss=9526272, swap=0)

If you could put code that pinpoints memory usage where you're likely to be using memory, that's fine, but I didn't want to.

What happened with this method?

Take peak usage


import os
import re

def peak_memory():
    pid = os.getpid()
    with open(f'/proc/{pid}/status') as f:
        for line in f:
            if not line.startswith('VmHWM:'):
                continue
            return int(re.search('[0-9]+', line)[0])
    raise ValueError('Not Found')

def f():
    a = [0] * 20000000

print('before:', peak_memory(), 'KB')
f()
print('after: ', peak_memory(), 'KB')

As a result, it became like this.

Is increasing after


before: 9072 KB
after:  165532 KB

I'm happy.

References

Measure the memory consumption of Linux programs

About the code

Feel free to use the code in this article for Python scripts that you or your company is developing.

Recommended Posts

Take your own peak memory usage on Linux & Python
Create your own Linux commands in Python
[Python] Register your own library on PyPI
Install Linux on your Chromebox
Monitor disk usage on Linux
Install Python Pillow on Amazon Linux
[Python] Make your own LINE bot
Make your own manual. [Linux] [man]
[Python] logging in your own module
Try implementing k-NN on your own
View disk usage on personal Linux
Introduce Python 3.5.2 environment on Amazon Linux
python> os.path.join ('data','checkpoint')>'data / checkpoint' on linux
Load_data self-made to run Python MNIST sample code on your own dataset
Install Python3 and Django on Amazon Linux (EC2) and run your web server
[LLDB] Create your own command in Python
Find files like find on linux in Python
Easily use your own functions in Python
Create a python environment on your Mac
[Python] Package and distribute your own modules
Note on building your own Miniconda environment
[C] [python] Read with AquesTalk on Linux
Basic usage of Btrfs on Arch Linux
When looking at memory usage in Python 3
Until you install your own Python library
5 reasons to install Linux on your laptop.
Import your own functions on AWS Glue
Dockerfile: Install Docker on your Linux server
Publish your own Python library with Homebrew
Install Python 3.8, Pip 3.8 on EC2 (Amazon Linux 2)
Get your own IP address in Python
[Python] Implement your own list-like class using collections.UserList
Linux C / C ++ Build your own library creation environment
Make your own module quickly with setuptools (python)
Build a Python development environment on your Mac
Easy way to load CPU / memory on Linux
Import your own modules in Grasshopper's Python development
python: Use your own class for numpy ndarray