Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A simple solution for Linux and other systems with <code>/proc/self/status</code> is the following code, which I use in a project of mine:</p> <pre><code>def memory_usage(): """Memory usage of the current process in kilobytes.""" status = None result = {'peak': 0, 'rss': 0} try: # This will only work on systems with a /proc file system # (like Linux). status = open('/proc/self/status') for line in status: parts = line.split() key = parts[0][2:-1].lower() if key in result: result[key] = int(parts[1]) finally: if status is not None: status.close() return result </code></pre> <p>It returns the current and peak resident memory size (which is probably what people mean when they talk about how much RAM an application is using). It is easy to extend it to grab other pieces of information from the <code>/proc/self/status</code> file.</p> <p>For the curious: the full output of <code>cat /proc/self/status</code> looks like this:</p> <pre><code>% cat /proc/self/status Name: cat State: R (running) Tgid: 4145 Pid: 4145 PPid: 4103 TracerPid: 0 Uid: 1000 1000 1000 1000 Gid: 1000 1000 1000 1000 FDSize: 32 Groups: 20 24 25 29 40 44 46 100 1000 VmPeak: 3580 kB VmSize: 3580 kB VmLck: 0 kB VmHWM: 472 kB VmRSS: 472 kB VmData: 160 kB VmStk: 84 kB VmExe: 44 kB VmLib: 1496 kB VmPTE: 16 kB Threads: 1 SigQ: 0/16382 SigPnd: 0000000000000000 ShdPnd: 0000000000000000 SigBlk: 0000000000000000 SigIgn: 0000000000000000 SigCgt: 0000000000000000 CapInh: 0000000000000000 CapPrm: 0000000000000000 CapEff: 0000000000000000 CapBnd: ffffffffffffffff Cpus_allowed: 03 Cpus_allowed_list: 0-1 Mems_allowed: 1 Mems_allowed_list: 0 voluntary_ctxt_switches: 0 nonvoluntary_ctxt_switches: 0 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload