Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm a bit confused on some of the results I am seeing from "ps" and "free".

On my server, this is the result of "free -m"

[root@server ~]# free -m
             total       used       free     shared    buffers     cached
Mem:          2048       2033         14          0         73       1398
-/+ buffers/cache:        561       1486
Swap:         2047         11       2036

My understanding of how Linux manages memory, is that it will store disk usage in RAM, so that each subsequent access is quicker. I believe this is indicated by the "cached" columns. Additionally, various buffers are stored in RAM, indicated in the "buffers" column.

So if I understand correctly, the "actual" usage is supposed to be the "used" value of "-/+ buffers/cache", or 561 in this case.

So assuming all of that is correct, the part that throws me is the results of "ps aux".

My understanding of the "ps" results, is that the 6th column (RSS), represents the size in kilobytes the process uses for memory.

So when I run this command:

[root@server ~]# ps aux | awk '{sum+=$6} END {print sum / 1024}'
1475.52

Shouldn't the result be the "used" column of "-/+ buffers/cache" from "free -m"?

So, how can I properly determine the memory usage of a process in Linux? Apparently my logic is flawed.

share|improve this question
    
@Coren, Personally my reason for asking was more about why ps resulted in a different answer than free, not specifically how to interpret either. –  GoldenNewby Mar 23 '12 at 22:05
    
ok, I remove my comment then. –  Coren Mar 24 '12 at 14:50
    
Very well posed question, exactly what was on my mind, but much clearer than I could have put it :-) Thanks! –  Oliver May 2 '13 at 18:38

6 Answers 6

up vote 24 down vote accepted

This exact same question was asked on serverfault just the other day :-)

The linux virtual memory system isn't quite so simple. You can't just add up all the RSS fields and get the value reported used by free. There are many reasons for this, but I'll hit a couple of the biggest ones.

  • When a process forks, both the parent and the child will show with the same RSS. However linux employs a copy-on-write so that both processes are really using the same memory. Only when one of the processes modifies the memory will it actually be duplicated.
    So this will cause the free number to be smaller than the top RSS sum.

  • The RSS value doesnt include shared memory. Because shared memory isn't owned by any 1 process, top doesnt include it in RSS. So this will cause the free number to be larger than the top RSS sum.

share|improve this answer
    
This is the best answer I've gotten on any stack-exchange site to date. So specifically what I wanted to know. It's so especially accurate to my situation because I'm dealing with a program I wrote that forks of processes, but the majority of the footprint is in libraries they use. –  GoldenNewby Mar 23 '12 at 20:13
    
The problem with this answer is that calculating the sum of RSS and SHR often gives a lot less than used memory. For example on a VPS I have, used memory is 380MB while the sum of all RSS and SHR is 90MB. –  user239558 Jun 25 '13 at 20:34
2  
@user239558 As I mentioned in the answer, there are many reasons the numbers don't add up, I only listed 2 of them. There are a lot of other numbers; cache, slab, huge pages, etc. –  Patrick Jun 26 '13 at 0:20

A really good tool is pmap which list the current usage of memory for a certain process:

pmap -d PID

For more information about it see the man page man pmap and also have a look at 20 Linux System Monitoring Tools Every SysAdmin Should Know, which list great tools I always use to get information about my Linux box.

share|improve this answer
    
That's a pretty cool tool, but it doesn't really solve my problem. I'm trying to figure out how to effectively determine the "actual" memory usage on the server. –  GoldenNewby Mar 23 '12 at 6:47
3  
@GoldenNewby There is no such thing as the “actual” memory usage of a process. The actual memory usage of the system is what free tells you. –  Gilles Mar 23 '12 at 19:40
    
pmap -x PID also includes a RSS column which is often quite useful to get idea where the RSS sum of a process (as observed e.g. via top comes from). –  maxschlepzig Aug 5 at 21:29

Run top, hit h for help then f to add fields. you can add the following fields:

  • RSS amount of physical memory the application is using
  • CODE total amount of memory the process's executable code is using
  • DATA - total amount of memory (kb) dedicated to a process's data and stack

Between these 3 you should have pretty accurate results. You can also use more detailed replacements for top I recommend htop or atop.

Edit: Almost forgot if you want really detailed information. Find the PID and cat the following file.

PID=123

cat /proc/123/status

Edit 2: If you can find it or have it the book:

Optimizing Linux Performance: A Hands-On Guide to Linux Performance Tools

-has a section Chapter 5: Performance Tools: Process-Specific Memory -it has way more information than you would ever want.

share|improve this answer
    
Well top by default has the RSS size of the process. Top gives identical results as does "ps aux" in my example. My question is, how is it that the combined RSS of all processes is much higher than the "active" memory usage on the whole server? –  GoldenNewby Mar 23 '12 at 6:41

ps gives you the amount of memory used by each process. Some of that memory is mmapped files, which counts under cache. Some of that memory (especially code) is shared with other processes, so if you add up the RSS values it's counted multiple times.

There's no right answer to “how much memory does this process use?”, because it doesn't depend on the process alone, it also depends on the environment. There are many different values you might call the “memory usage” of the process, and they don't match or add up because they're counting different things.

share|improve this answer

Try this: it will give you the total RAM actually used by all the process running in MB

ps -eo size,pid,user,command --sort -size | awk '
  { hr=$1/1024 ; printf("%13.2f Mb ",hr) } 
  { for ( x=4 ; x<=NF ; x++ ) { printf("%s ",$x) } print "" }
  ' | awk '{total=total + $1} END {print total}'
share|improve this answer
    
The size reported by ps has little relation to actual memory usage. It is the virtual size of each process which isn't necessarily allocated memory. It also doesn't include some segments that are allocated. –  mtm May 9 at 11:35

It ll show you how much memory user by users..

#!/bin/bash
total_mem=0

printf "%-10s%-10s\n" User MemUsage

while read u m
do
        [[ $old_user != $u ]] && {  printf "%-10s%-0.1f\n" $old_user $total_mem;
                                    total_mem=0; }
        total_mem="$(echo $m + $total_mem | bc)"
        old_user=$u

done < <(ps --no-headers -eo user,%mem| sort -k1)

#EOF
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.