Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check the main memory usage by a linux command [closed]

Is there any command that I could use to check the memory usage for a single linux command? For instance I have a script file (test.sh) which will read and extract word from a 100mb text file. How could I know how much memory does this command (./test.sh input_file.txt) would take? Thanks for the advises there!!

like image 855
CheeHow Avatar asked Dec 20 '22 02:12

CheeHow


2 Answers

Use the free command to check the usage of RAM.

To check the size of the program in memory you can check the /proc/[pid]/statm file. For details of the format of this file read man proc

Fetch the PID of the script from the script using the $$ variable (in bash).

EDIT

Other solutions:

ps u $PID | tail -n1 | tr -s ' ' | cut -d ' ' -f 5,6 Gives you the VMZ and RSS of the process with $PID.

Or may want to like to see only the process memory using

watch -n0.5 ps u $PID

this will update the usage of the memory for your process every 0.5 secs. Adjust the value for updating as required.

like image 177
phoxis Avatar answered Dec 22 '22 14:12

phoxis


You can just use top to see that. When you execute your script, a shell process such as bash, will be create to execute the script for you. So, find the shell process in top and you can see how many memory it uses.

like image 28
nicky_zs Avatar answered Dec 22 '22 14:12

nicky_zs