Total memory used by Python process?

前端 未结 12 705
眼角桃花
眼角桃花 2020-11-22 04:28

Is there a way for a Python program to determine how much memory it\'s currently using? I\'ve seen discussions about memory usage for a single object, but what I need is tot

相关标签:
12条回答
  • 2020-11-22 04:49

    I like it, thank you for @bayer. I get a specific process count tool, now.

    # Megabyte.
    $ ps aux | grep python | awk '{sum=sum+$6}; END {print sum/1024 " MB"}'
    87.9492 MB
    
    # Byte.
    $ ps aux | grep python | awk '{sum=sum+$6}; END {print sum " KB"}'
    90064 KB
    

    Attach my process list.

    $ ps aux  | grep python
    root       943  0.0  0.1  53252  9524 ?        Ss   Aug19  52:01 /usr/bin/python /usr/local/bin/beaver -c /etc/beaver/beaver.conf -l /var/log/beaver.log -P /var/run/beaver.pid
    root       950  0.6  0.4 299680 34220 ?        Sl   Aug19 568:52 /usr/bin/python /usr/local/bin/beaver -c /etc/beaver/beaver.conf -l /var/log/beaver.log -P /var/run/beaver.pid
    root      3803  0.2  0.4 315692 36576 ?        S    12:43   0:54 /usr/bin/python /usr/local/bin/beaver -c /etc/beaver/beaver.conf -l /var/log/beaver.log -P /var/run/beaver.pid
    jonny    23325  0.0  0.1  47460  9076 pts/0    S+   17:40   0:00 python
    jonny    24651  0.0  0.0  13076   924 pts/4    S+   18:06   0:00 grep python
    

    Reference

    • memory - Linux: find out what process is using all the RAM? - Super User
    • Total memory used by Python process? - Stack Overflow
    • linux - ps aux output meaning - Super User
    0 讨论(0)
  • 2020-11-22 04:51

    For Unix systems command time (/usr/bin/time) gives you that info if you pass -v. See Maximum resident set size below, which is the maximum (peak) real (not virtual) memory that was used during program execution:

    $ /usr/bin/time -v ls /
    
        Command being timed: "ls /"
        User time (seconds): 0.00
        System time (seconds): 0.01
        Percent of CPU this job got: 250%
        Elapsed (wall clock) time (h:mm:ss or m:ss): 0:00.00
        Average shared text size (kbytes): 0
        Average unshared data size (kbytes): 0
        Average stack size (kbytes): 0
        Average total size (kbytes): 0
        Maximum resident set size (kbytes): 0
        Average resident set size (kbytes): 0
        Major (requiring I/O) page faults: 0
        Minor (reclaiming a frame) page faults: 315
        Voluntary context switches: 2
        Involuntary context switches: 0
        Swaps: 0
        File system inputs: 0
        File system outputs: 0
        Socket messages sent: 0
        Socket messages received: 0
        Signals delivered: 0
        Page size (bytes): 4096
        Exit status: 0
    
    0 讨论(0)
  • 2020-11-22 04:53

    Current memory usage of the current process on Linux, for Python 2, Python 3, and pypy, without any imports:

    def getCurrentMemoryUsage():
        ''' Memory usage in kB '''
    
        with open('/proc/self/status') as f:
            memusage = f.read().split('VmRSS:')[1].split('\n')[0][:-3]
    
        return int(memusage.strip())
    

    It reads the status file of the current process, takes everything after VmRSS:, then takes everything before the first newline (isolating the value of VmRSS), and finally cuts off the last 3 bytes which are a space and the unit (kB).
    To return, it strips any whitespace and returns it as a number.

    Tested on Linux 4.4 and 4.9, but even an early Linux version should work: looking in man proc and searching for the info on the /proc/$PID/status file, it mentions minimum versions for some fields (like Linux 2.6.10 for "VmPTE"), but the "VmRSS" field (which I use here) has no such mention. Therefore I assume it has been in there since an early version.

    0 讨论(0)
  • 2020-11-22 04:53

    Using sh and os to get into python bayer's answer.

    float(sh.awk(sh.ps('u','-p',os.getpid()),'{sum=sum+$6}; END {print sum/1024}'))
    

    Answer is in megabytes.

    0 讨论(0)
  • 2020-11-22 04:55

    For Python 3.6 and psutil 5.4.5 it is easier to use memory_percent() function listed here.

    import os
    import psutil
    process = psutil.Process(os.getpid())
    print(process.memory_percent())
    
    0 讨论(0)
  • 2020-11-22 04:56

    On unix, you can use the ps tool to monitor it:

    $ ps u -p 1347 | awk '{sum=sum+$6}; END {print sum/1024}'
    

    where 1347 is some process id. Also, the result is in MB.

    0 讨论(0)
提交回复
热议问题