skip to Main Content

I have 2 separate Python programs (for example myp1.py and myo2.py) must be running in Debian. It appears system memory consumes a lot. So I try to detect memory usage of each .py and decide to relaunch .py in a sh script that triggered by crontab.
Is it possible to have any hint for the commands used to grab memory usage of each .py in sh script please?

2

Answers


  1. In /proc/[pid]/status there is a lot of information about the process. You would be interested in the fields VmPeak and VmSize.

    You state that you have a lot of memory consumption. I don’t know where you got that value from, but you should look at the output of vmstat 5 5 if there is a memory problem on your system.

    Memory on Linux is used as much as possible. In the vmstst, you will see that quite a lot is used for cache. That makes the system faster. Do not look at the free column; it says little about the actual use of memory for processes.

    Memory becomes a problem when the columns si and so are high. On most modern systems, they are 0 or occasionally 1 or 2. These columns indicate how much memory is swapped in and out to disk.

    Login or Signup to reply.
  2. here is the bash code to run (make sure the .sh file is executable – use chmod-):

    ps -o pid,%mem,command ax | grep python | while read pid mem command; do echo "PID: $pid, Memory Usage: $mem%, Command: $command" 
    done
    

    save the file (FILENAME.sh), then execute sudo chmod 777 FILENAME.sh, then you can execute the following to see all PIDs that use python and see how much memory they are using:

    ./FILENAME.sh  
    

    In order to filter based on specific Python script names, you could use grep with the bash script to filter them out:

    ./FILENAME.sh | grep -E '(REGEX_PATTERN)'
    

    make sure to use the flag -E to use the regex engine and then pass the regex_pattern (the filename pattern) you are looking for.

    An example I used to test it:

    ./test.sh | grep -E '(server.py|lsp_server.py)'
    

    I got the following:

     PID: 333540, Memory Usage: 0.1%, Command:
     /home/XXXX/.pyenv/versions/XXXX/XXX/python
     /home/XXXX/.vscode/extensions/ms-python.isort-2022.8.0/bundled/tool/***server.py***
    
     PID: 333543, Memory Usage: 0.2%, Command:
     /home/XXXX/.pyenv/versions/XXXX/XXX/python
     /home/XXXX/.vscode/extensions/ms-python.autopep8-2023.4.0/bundled/tool/***lsp_server.py***
    

    I hope this helps!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search