scripts: memory usage per process(es) info differs, but why?

New features which should be implemented in Porteus; suggestions are welcome. All questions or problems with testing releases (alpha, beta, or rc) should go in their relevant thread here, rather than the Bug Reports section.
Post Reply
User avatar
Posts: 1573
Joined: 11 Jan 2011, 02:46
Distribution: Porteus 3.1.0 x86-64 XFCe
Location: Germany

scripts: memory usage per process(es) info differs, but why?

Post#1 by Rava » 16 Apr 2017, 23:12

I found code on the net to create two scripts to tell me how much memory a process (or all processes) by "name" uses.
Now, the way they do it differs. Also, one [psmem] lists also the amount of processes and the memory usage per process (that info seems quite useless, since that can only be an average, since when more processes of "name" are running, it sure means they all have different memory consumption, and the "Memory usage per process" can therefore only be an average, and what use it in that?
[For now I kept that info in the script, though.]

Here are the scripts:

Code: Select all

ps -C $1 -O rss | gawk '{ count ++; sum += $2 }; END {count --; print "Number of processes =",count; print "Memory usage per process =",sum/1024/count, "MB"; print "Total memory usage =", sum/1024, "MB" ;};'

Code: Select all

# Written by Alexander Löhner
# The Linux Counter Project
# Updated on Sep. 8th 2015 by Mike Hay to remove dependency on 'bc'
# Updated on 10th Sep. 2015 by Rava: added " MB" and name of process/program
if [[ "${1}" = "" ]]; then
    echo "please add the name of the process as a parameter"
        exit 1

for i in $(pidof $1)
do #echo DEBUG: /proc/$i/smaps >&2
echo $(awk '/Private/ {sum+=$2} END {print sum}' /proc/$i/smaps)
done | awk '{sum+=$1} END {printf("%.2f", sum/1024)}' && echo " MB $1"
Prior me testing that the PC ran out of memory and was unresponsive, I tried to exit a full screen viewnior via pressing the "escape key", to no prevails, also I tried changing into virtual console 1 via pressing Ctrl+Alt+F1, also to no prevail.

When I got back to that machine, palemoon exited itself (aka "crashed"), and the viewnior was still running fullscreen. But now I could easily exit viewnior's full screen via the "Esc" key. And the PC no longer ran out of memory and was responsive once again.

Code: Select all

rava@porteus:/usr/local/bin$ l *mem*
-rwxr-xr-x 1 root 515 2017-03-17 14:28 memused
-rwxr-xr-x 1 root 220 2016-12-07 08:29 psmem

rava@porteus:/usr/local/bin$ psmem gnome-mplayer
Number of processes = 3
Memory usage per process = 14.9753 MB
Total memory usage = 44.9258 MB
rava@porteus:/usr/local/bin$ memused gnome-mplayer
35.86 MB gnome-mplayer

rava@porteus:/usr/local/bin$ psmem mplayer
Number of processes = 3
Memory usage per process = 23.0404 MB
Total memory usage = 69.1211 MB
rava@porteus:/usr/local/bin$ memused mplayer
67.75 MB mplayer

rava@porteus:/usr/local/bin$ psmem viewnior 
Number of processes = 21
Memory usage per process = 35.0526 MB
Total memory usage = 736.105 MB
rava@porteus:/usr/local/bin$ memused viewnior 
694.16 MB viewnior

rava@porteus:/usr/local/bin$ psmem mousepad
Number of processes = 1
Memory usage per process = 39.8086 MB
Total memory usage = 39.8086 MB
rava@porteus:/usr/local/bin$ memused mousepad
33.93 MB mousepad

rava@porteus:/usr/local/bin$ psmem xfce4-terminal
Number of processes = 1
Memory usage per process = 16.707 MB
Total memory usage = 16.707 MB
rava@porteus:/usr/local/bin$ memused xfce4-terminal
8.46 MB xfce4-terminal
The example of "xfce4-terminal", it is almost half (or double, when seen from the other side) the memory usage size of the other script.
While other examples are almost the same, e.g. the "mplayer" one (not the "gnome-mplayer" one)

Now, is someone able to explain to silly me the reasons for the discrepancies between the output of psmem and memused?
Yours Rava

Post Reply