Skip to content

Instantly share code, notes, and snippets.

@TysonRayJones
Last active December 15, 2021 02:29
Show Gist options
  • Save TysonRayJones/e722b26b153ff5842934a59cbbd44da7 to your computer and use it in GitHub Desktop.
Save TysonRayJones/e722b26b153ff5842934a59cbbd44da7 to your computer and use it in GitHub Desktop.
Guide for using the python linux process memory profiler

Measure memory of a Linux process in Python

The module (found here)

from memorymeasure import get_memory

provides a single function get_memory which returns the current and peak, real and virtual memories (in bytes, accurate to ~kilobytes) used by your Python code's Linux process, in a dictionary. The same module but in C can be found here.

Calling

get_memory()

returns a dictionary with keys VmHWM, VmPeak, VmRSS and VmSize, e.g.

{
    'VmHWM': 6436000, 
    'VmPeak': 122260000, 
    'VmRSS': 6436000, 
    'VmSize': 122244000
}

though will raise a FileNotFoundError if ran on a non-linux machine.


Real memory (resident set size) is the amount of physical RAM your process is using, and virtual memory is the size of the memory address space your process is using. Linux chooses what in your virtual memory gets to reside in RAM. Note that in addition to your program data, these memories include the space taken up by your code itself, and any libraries your code is using (which may be shared by other running processes, skewing your usage). A good explanation can be found here.

Peak memory is the maximum amount of memory your process has used over its lifetime so far.


This module works by reading /proc/self/status which linux populates with info about your running process. It uses fields

Output status Field Description
currRealMem VmRSS Resident set size
peakRealMem VmHWM Peak resident set size ("high water mark")
currVirtMem VmSize Virtual memory size
peakVirtMem VmPeak Peak virtual memory size

taken from under /proc/[pid]/status on the proc man page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment