Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with OpenID
Advertise on LowEndTalk.com

In this Discussion

Help me understand the memory usage

Help me understand the memory usage

vanarpvanarp Member
edited June 2012 in Help

Pardon my ignorance and please help me in interpreting this htop output. I am getting confused with some of the processes showing up multiple times. This vps is running a LAMP web server with one WP site.

bobo@vps:~$ htop

  1  [                                                                0.0%]     Tasks: 19, 16 thr; 1 running
  2  [|                                                               0.7%]     Load average: 0.02 0.01 0.00
  Mem[|||||||||||||||||||||||||||||||||||||                      273/512MB]     Uptime: 12 days, 06:54:40
  Swp[                                                               0/0MB]

  PID USER      PRI  NI  VIRT   RES   SHR S CPU% MEM%   TIME+  Command
16014 mysql      16   0 62740 27800  6252 S  0.0  5.3  0:00.00 /usr/sbin/mysqld
15884 mysql      18   0 62740 27800  6252 S  0.0  5.3  0:46.20 /usr/sbin/mysqld
15885 mysql      18   0 62740 27800  6252 S  0.0  5.3  0:50.93 /usr/sbin/mysqld
22257 mysql      18   0 62740 27800  6252 S  0.0  5.3  0:41.71 /usr/sbin/mysqld
16033 mysql      18   0 62740 27800  6252 S  0.0  5.3  0:00.64 /usr/sbin/mysqld
16004 mysql      15   0 62740 27800  6252 S  0.0  5.3  3:20.83 /usr/sbin/mysqld
14104 www-data   15   0  138M 10576  2292 S  0.0  2.0  0:00.22 /usr/sbin/apache2 -k start
14105 www-data   15   0  138M 10576  2292 S  0.0  2.0  0:00.27 /usr/sbin/apache2 -k start
14106 www-data   15   0  138M 10576  2292 S  0.0  2.0  0:00.23 /usr/sbin/apache2 -k start
14107 www-data   15   0  138M 10576  2292 S  0.0  2.0  0:00.34 /usr/sbin/apache2 -k start
14108 www-data   18   0  138M 10576  2292 S  0.0  2.0  0:00.34 /usr/sbin/apache2 -k start
14109 www-data   16   0  138M 10576  2292 S  0.0  2.0  0:00.26 /usr/sbin/apache2 -k start
14110 www-data   16   0  138M 10576  2292 S  0.0  2.0  0:00.23 /usr/sbin/apache2 -k start
14111 www-data   15   0  138M 10576  2292 S  0.0  2.0  0:00.25 /usr/sbin/apache2 -k start
14112 www-data   15   0  138M 10576  2292 S  0.0  2.0  0:00.32 /usr/sbin/apache2 -k start
14113 www-data   15   0  138M 10576  2292 S  0.0  2.0  0:00.28 /usr/sbin/apache2 -k start
14114 www-data   15   0  138M 10576  2292 S  0.0  2.0  0:00.35 /usr/sbin/apache2 -k start
14102 www-data   18   0  138M 10576  2292 S  0.0  2.0  0:03.24 /usr/sbin/apache2 -k start
 9242 root       15   0  9580  5160  2836 S  0.0  1.0  0:06.40 /usr/sbin/apache2 -k start
19740 root       15   0 85992  4880  1628 S  0.0  0.9  0:09.52 php-fpm: master process (/etc/php5/fpm/php-fpm.conf)
17646 bobo       18   0 85980  3996   740 S  0.0  0.8  0:00.00 php-fpm: pool bobo  
19741 www-data   18   0 85968  3996   740 S  0.0  0.8  0:00.00 php-fpm: pool www
17575 root       15   0  9576  3004  2388 S  0.0  0.6  0:00.08 sshd: bobo   [priv]
 3561 www-data   18   0  9076  2848   548 S  0.0  0.5  0:00.00 /usr/sbin/fcgi-pm -k start
 3560 www-data   15   0  9100  2836   528 S  0.0  0.5  0:00.00 /usr/sbin/apache2 -k start
16251 root       18   0  6612  2344  1916 S  0.0  0.4  0:00.07 /usr/sbin/sshd -D
17648 bobo       15   0  3464  1908  1164 R  0.0  0.4  0:00.05 htop
17592 bobo       16   0  3448  1788  1408 S  0.0  0.3  0:00.01 -bash
17591 bobo       15   0  9576  1644  1020 S  0.0  0.3  0:00.11 sshd: bobo  @pts/0
16329 postfix    15   0  4576  1560  1300 S  0.0  0.3  0:00.07 qmgr -l -t fifo -u
    1 root       15   0  3068  1532  1272 S  0.0  0.3  0:00.55 init
16319 root       22   0  4508  1492  1252 S  0.0  0.3  0:00.71 /usr/lib/postfix/master
15995 postfix    15   0  4528  1356  1128 S  0.0  0.3  0:00.00 pickup -l -t fifo -u -c
15964 root       15   0  2548   920   736 S  0.0  0.2  0:01.39 cron
15969 root       15   0  2172   748   652 S  0.0  0.1  0:01.20 /usr/sbin/syslogd --no-forward

F1Help  F2Setup F3SearchF4FilterF5Tree  F6SortByF7Nice -F8Nice +F9Kill  F10Quit
Happy with ssd vps hosting. Now my sites load like they are hosted next door.

Tagged:

Comments

  • Because you have "child processes", a process can only respond to 1 request at a time, so you spawn more and you can deal with more request.

    The Original Daniel.

  • vanarpvanarp Member

    I am not getting what all I need to sum up to arrive at the memory consumed. In the above case how 273MB is occupied is a puzzle to me.

    Happy with ssd vps hosting. Now my sites load like they are hosted next door.

  • TazTaz Disabled

    Switch from Apache to nginx.

    Time is good and also bad. Life is short and that is sad. Dont worry be happy thats my style. No matter what happens i won't lose my smile!

  • vanarpvanarp Member

    @NinjaHawk said: Switch from Apache to nginx.

    I have planned that for near future. Mean while trying to understand where is all the memory going..

    Happy with ssd vps hosting. Now my sites load like they are hosted next door.

  • TazTaz Disabled

    Apache having some manly fun with your memory ( if you know what I mean).

    Time is good and also bad. Life is short and that is sad. Dont worry be happy thats my style. No matter what happens i won't lose my smile!

  • Most of it to Apache

    http://mitihost.com/ -> Great Shared Hosting for a good $$

    https://freevps.us/ -> Great P2H Forum to get free VPS's

  • netomxnetomx Member

    well, easily: MySQL.

    Referral links: DigitalOcean referral link | Get 500MB free with Dropbox | I sell domains with Google Apps, $1 p/ user
  • vanarpvanarp Member

    Wondering why nobody is telling me Apache is consuming xyz MB and MySQL is consuming xyz MB like that... :(

    Is it really difficult to pinpoint memory consumption by process/application on Linux..?

    Happy with ssd vps hosting. Now my sites load like they are hosted next door.

  • @vanarp said: Wondering why nobody is telling me Apache is consuming xyz MB and MySQL is consuming xyz MB like that... :(

    Is it really difficult to pinpoint memory consumption by process/application on Linux..?

    Just add it up the numbers under MEM%

  • netomxnetomx Member

    try "ps -aux" too

    Referral links: DigitalOcean referral link | Get 500MB free with Dropbox | I sell domains with Google Apps, $1 p/ user
  • yomeroyomero Member
    edited June 2012

    @vanarp said: Is it really difficult to pinpoint memory consumption by process/application on Linux..?

    Yes, it is. (And is worst in OpenVZ) Isn't just a sum, the SHR and other stuff is part of the accounting. How, I don't know.

    But since you are in OpenVZ, you can measure a little bit with the VIRT column. But some of the processes in your are just childs of a parent process, and its counters doesn't account to the memory used.

    Thanked by 1vanarp
  • vanarpvanarp Member

    After extensive Googling, I found a way to get the memory usage listed in MB.

    while read command percent rss; do if [[ "${command}" != "COMMAND" ]]; then rss="$(bc <<< "scale=2;${rss}/1024")"; fi; printf "%-26s%-8s%s\n" "${command}" "${percent}" "${rss}"; done < <(ps -A --sort -rss -o comm,pmem,rss | head -n 11)
    

    It is working perfectly fine on a system that does not have Apache installed. But on the above system it is not able to show me the culprit process :(

    bobo@vps:~$ free -m
                 total       used       free     shared    buffers     cached
    Mem:           512        280        231          0          0          0
    -/+ buffers/cache:        280        231
    Swap:            0          0          0
    
    bobo@vps:~$ while read command percent rss; do if [[ "${command}" != "COMMAND" ]]; then rss="$(bc <<< "scale=2;${rss}/1024")"; fi; printf "%-26s%-8s%s\n" "${command}" "${percent}" "${rss}"; done < <(ps -A --sort -rss -o comm,pmem,rss | head -n 50)
    COMMAND                   %MEM    RSS
    mysqld                    5.4     27.92
    php5-fpm                  4.2     21.51
    apache2                   0.9     5.03
    apache2                   0.9     4.82
    php5-fpm                  0.9     4.76
    php5-fpm                  0.7     3.90
    sshd                      0.5     2.92
    sshd                      0.5     2.92
    apache2                   0.5     2.78
    apache2                   0.5     2.76
    sshd                      0.4     2.28
    bash                      0.3     1.75
    bash                      0.3     1.75
    sshd                      0.3     1.58
    sshd                      0.3     1.58
    qmgr                      0.2     1.52
    init                      0.2     1.49
    master                    0.2     1.45
    pickup                    0.2     1.32
    cron                      0.1     .89
    ps                        0.1     .77
    bash                      0.1     .76
    syslogd                   0.1     .73
    

    those values do not sum up to 280 MB. there should be some way...

    Happy with ssd vps hosting. Now my sites load like they are hosted next door.

  • yomeroyomero Member
    edited June 2012

    @vanarp said: those values do not sum up to 280 MB

    OpenVZ... I told you Isn't about systems with apache installed or not. But if you ask me, kill apache and have fun. You will get, maybe 130MB free

  • vanarpvanarp Member

    @yomero said: OpenVZ... I told you

    Yeah, I understand the reason now. Thank you!

    So OpenVZ is not good (memory wise) to some specific applications or for all..?

    Happy with ssd vps hosting. Now my sites load like they are hosted next door.

  • TazTaz Disabled

    @vanarp said: So OpenVZ is not good (memory wise) to some specific applications or for all..? No, It is not open VZ. It is Apache. If you can optimize it, it will cut down on on resource. If you can not, use nginx.

    Time is good and also bad. Life is short and that is sad. Dont worry be happy thats my style. No matter what happens i won't lose my smile!

    Thanked by 1vanarp
  • yomeroyomero Member

    @vanarp said: So OpenVZ is not good (memory wise) to some specific applications or for all..?

    Yes, the problem is about applications allocating lots of virtual memory. And that is how the OpenVZ based in RHEL5 account the memory usage.

    The best example is Java. It will use the double or triple of memory than in a real computer (default configuration)

    Thanked by 1vanarp
  • vanarpvanarp Member
    edited June 2012

    Thank you @NinjaHawk and @yomero!

    bobo@vps:~$ free -m
                 total       used       free     shared    buffers     cached
    Mem:           512        153        358          0          0          0
    -/+ buffers/cache:        153        358
    Swap:            0          0          0
    

    Big thanks to Nginx! I felt much improvement in site load times as well :)

    Happy with ssd vps hosting. Now my sites load like they are hosted next door.

  • TazTaz Disabled

    As I said..virtualization has nothing to do with your issue.

    Time is good and also bad. Life is short and that is sad. Dont worry be happy thats my style. No matter what happens i won't lose my smile!

  • vanarpvanarp Member

    @NinjaHawk said: As I said..virtualization has nothing to do with your issue.

    But the math is still not working even though there is plenty of memory saved ;)

    Happy with ssd vps hosting. Now my sites load like they are hosted next door.

  • qjqqjq Member

    child processes share the bulk of their mem with parent processes and sometimes processes use the same shared libraries. If you want apache's usage just free -m, stop apache, then free -m again. Ovz's memory accounting is pretty broken anyways

  • vanarpvanarp Member
    edited June 2012

    @qjq said: Ovz's memory accounting is pretty broken anyways

    I read OVZ does not use own kernel and everything is shared without hard limits. But why the memory accounting is broken? Why not the OpenVZ creators bothered about it?

    Edit: Asking just for my own education.

    Happy with ssd vps hosting. Now my sites load like they are hosted next door.

  • yomeroyomero Member

    @vanarp said: But why the memory accounting is broken? Why not the OpenVZ creators bothered about it?

    Dunno But the new 2.6.32 kernels do accounting normally. Unfortunately most providers still doesn't implement it.

  • vanarpvanarp Member

    @yomero said: But the new 2.6.32 kernels do accounting normally. Unfortunately most providers still doesn't implement it.

    Are you sure? Because the vps I reported above shows this:

    bobo@vps:~$ uname -r
    2.6.32-pony6-3
    
    Happy with ssd vps hosting. Now my sites load like they are hosted next door.

  • yomeroyomero Member
    edited June 2012

    @vanarp said: 2.6.32-pony6-3

    Fake. (For compatibility)

  • NickMNickM Member

    @vanarp said: Are you sure? Because the vps I reported above shows this:

    They spoof the kernel version to make certain distros (like Debian) play nicely. You're actually on 2.6.18

  • KairusKairus Member

    @vanarp said: 2.6.32-pony6-3

    Is it sad my phone runs a newer kernel than OpenVZ servers?

    Thanked by 1tux
  • vanarpvanarp Member

    @yomero said: Fake.

    @NickM said: They spoof the kernel version to make certain distros (like Debian) play nicely. You're actually on 2.6.18

    Really?? What practically stops a host from upgrading the kernel on their nodes..?

    I see below kernel on a kvm vps. Could it be faked as well..?

    bobo@jupiter:~$ uname -r
    3.2.0-23-generic-pae
    
    Happy with ssd vps hosting. Now my sites load like they are hosted next door.

  • KairusKairus Member

    @vanarp said: Really?? What practically stops a host from upgrading the kernel on their nodes..?

    The latest OVZ kernel isn't very stable.

    @vanarp said: I see below kernel on a kvm vps. Could it be faked as well..?

    KVM instances run their own kernels, so no that's legit. OpenVZ containers use the kernel of the host node.

  • vanarpvanarp Member

    Quite surprising to me...

    So can we conclude that Xen/KVM kernels are up to date where as OpenVZ kernels are outdated (in general) ?

    Happy with ssd vps hosting. Now my sites load like they are hosted next door.

  • yomeroyomero Member
    edited June 2012

    @vanarp said: So can we conclude that Xen/KVM kernels

    KVM/Xen HVM can have any kernel that you want. Like a VMware/Virtualbox/etc instance. You install the OS and all the stuff.

    Xen PV can have it too, but for common configurations I haven't seen recent kernels, just 2.6.32 mostly. (Linode uses 3.4 btw o_O)

  • vanarpvanarp Member

    @yomero said: Linode uses 3.4 btw o_O

    Interesting....

    Happy with ssd vps hosting. Now my sites load like they are hosted next door.

  • tommytommy Member
    edited June 2012

    give ps_mem try

    slow motion

  • vanarpvanarp Member

    @tommy said: give ps_mem try

    Hmmm... it also does not solve the memory calculation issue. as can be seen below the actual memory in use and the script's result are greatly apart.

    On OpenVZ :

    root@vps:/home/bobo# ./ps_mem.py
     Private  +   Shared  =  RAM used       Program
    
    132.0 KiB +  61.5 KiB = 193.5 KiB       syslogd
    276.0 KiB +  63.0 KiB = 339.0 KiB       cron
    240.0 KiB + 198.5 KiB = 438.5 KiB       pickup
    312.0 KiB + 156.0 KiB = 468.0 KiB       su
    276.0 KiB + 198.5 KiB = 474.5 KiB       qmgr
    284.0 KiB + 205.0 KiB = 489.0 KiB       master
    732.0 KiB +  91.0 KiB = 823.0 KiB       init
    824.0 KiB + 827.0 KiB =   1.6 MiB       bash (2)
      1.0 MiB +   1.6 MiB =   2.6 MiB       sshd (3)
      1.7 MiB +   1.6 MiB =   3.3 MiB       nginx (2)
     19.8 MiB +   3.9 MiB =  23.7 MiB       php5-fpm (3)
     27.1 MiB + 137.5 KiB =  27.3 MiB       mysqld
    ---------------------------------
                             61.6 MiB
    =================================
    root@vps:/home/bobo# free -m
                 total       used       free     shared    buffers     cached
    Mem:           512        149        362          0          0          0
    -/+ buffers/cache:        149        362
    Swap:            0          0          0
    

    On KVM :

    bobo@jupiter:~$ sudo ./ps_mem.py
    [sudo] password for bobo:
     Private  +   Shared  =  RAM used       Program
    
     32.0 KiB +  28.0 KiB =  60.0 KiB       atd
      8.0 KiB +  54.0 KiB =  62.0 KiB       acpid
     64.0 KiB +  13.5 KiB =  77.5 KiB       dhclient3
      8.0 KiB + 102.5 KiB = 110.5 KiB       udevd (3)
    100.0 KiB +  42.0 KiB = 142.0 KiB       irqbalance
    104.0 KiB +  76.5 KiB = 180.5 KiB       cron
     96.0 KiB +  94.0 KiB = 190.0 KiB       upstart-socket-bridge [updated]
     96.0 KiB + 101.5 KiB = 197.5 KiB       upstart-udev-bridge [updated]
     24.0 KiB + 276.0 KiB = 300.0 KiB       getty (6)
    120.0 KiB + 180.0 KiB = 300.0 KiB       qmgr
    148.0 KiB + 184.5 KiB = 332.5 KiB       master
    232.0 KiB + 180.0 KiB = 412.0 KiB       pickup
    204.0 KiB + 261.5 KiB = 465.5 KiB       whoopsie
    508.0 KiB +  74.5 KiB = 582.5 KiB       rsyslogd
    600.0 KiB +  46.5 KiB = 646.5 KiB       dbus-daemon
    500.0 KiB + 158.0 KiB = 658.0 KiB       sudo
    644.0 KiB + 145.5 KiB = 789.5 KiB       init [updated]
      1.3 MiB + 955.5 KiB =   2.3 MiB       nginx (2)
    716.0 KiB +   1.6 MiB =   2.3 MiB       sshd (3)
      5.0 MiB +  80.5 KiB =   5.1 MiB       bash
      9.1 MiB + 110.5 KiB =   9.2 MiB       mysqld
     17.8 MiB +   5.7 MiB =  23.5 MiB       php5-fpm (4)
    ---------------------------------
                             47.8 MiB
    =================================
    bobo@jupiter:~$ free -m
                 total       used       free     shared    buffers     cached
    Mem:          1001        816        184          0         57        671
    -/+ buffers/cache:         86        914
    Swap:         1021          6       1015
    

    It makes me believe that there is no perfect program/script that can measure the exact memory in use by process on Linux.

    Only below script (ps command) takes us closer to reality but only on KVM.

    while read command percent rss; do if [[ "${command}" != "COMMAND" ]]; then rss="$(bc <<< "scale=2;${rss}/1024")"; fi; printf "%-26s%-8s%s\n" "${command}" "${percent}" "${rss}"; done < <(ps -A --sort -rss -o comm,pmem,rss | head -n 51)
    
    
    
    COMMAND                   %MEM    RSS
    php5-fpm                  2.1     21.25
    mysqld                    0.9     9.76
    php5-fpm                  0.5     5.82
    bash                      0.5     5.80
    php5-fpm                  0.4     4.78
    php5-fpm                  0.3     3.75
    sshd                      0.2     2.97
    nginx                     0.2     2.35
    sshd                      0.1     1.84
    nginx                     0.1     1.67
    init                      0.1     1.61
    sshd                      0.1     1.52
    master                    0.1     1.29
    pickup                    0.1     1.26
    qmgr                      0.1     1.14
    dbus-daemon               0.1     1.07
    whoopsie                  0.1     1.06
    bash                      0.1     1.05
    rsyslogd                  0.0     .88
    cron                      0.0     .76
    ps                        0.0     .68
    getty                     0.0     .66
    getty                     0.0     .66
    getty                     0.0     .66
    getty                     0.0     .66
    getty                     0.0     .66
    getty                     0.0     .66
    udevd                     0.0     .61
    bash                      0.0     .55
    irqbalance                0.0     .50
    acpid                     0.0     .47
    upstart-udev-br           0.0     .38
    upstart-socket-           0.0     .37
    atd                       0.0     .21
    bash                      0.0     .16
    dhclient3                 0.0     .08
    udevd                     0.0     .01
    udevd                     0.0     .01
    kthreadd                  0.0     0
    ksoftirqd/0               0.0     0
    kworker/u:0               0.0     0
    migration/0               0.0     0
    watchdog/0                0.0     0
    migration/1               0.0     0
    kworker/1:0               0.0     0
    ksoftirqd/1               0.0     0
    kworker/0:1               0.0     0
    watchdog/1                0.0     0
    cpuset                    0.0     0
    khelper                   0.0     0
    

    According to above output total memory in use is 79.62 MB which is closer to 86 MB reported by the free command.

    Happy with ssd vps hosting. Now my sites load like they are hosted next door.

  • yomeroyomero Member

    Nah, ps_mem.py does real memory accounting. Again, useless in these VPSs

  • DerekDerek Member

    You shouldn't run Apache and PHP FPM As Root.

    That's a problem..

  • yomeroyomero Member

    @Derek said: You shouldn't run Apache and PHP FPM As Root.

    Where is running as root? You mean the master processes? That's a common configuration.

  • qjqqjq Member

    ps_mem won't total up to free -m 's value cuz of stuff like disk cache

Sign In or Register to comment.