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}'

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

7 Answers 7

up vote 26 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
@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
Probably years later after you answered this, I still have (at least) one confusion. You said that the RSS value doesn't include shared memory, but this answer said that "It does include memory from shared libraries as long as the pages from those libraries are actually in memory". Now I don't know which one to believe... Maybe I'm missing some subtle differences here... –  Naitree May 9 at 2:45

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
@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 '14 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.


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

If you're looking for memory numbers that add up have a look at smem:

smem is a tool that can give numerous reports on memory usage on Linux systems. Unlike existing tools, smem can report proportional set size (PSS), which is a more meaningful representation of the amount of memory used by libraries and applications in a virtual memory system.

Because large portions of physical memory are typically shared among multiple applications, the standard measure of memory usage known as resident set size (RSS) will significantly overestimate memory usage. PSS instead measures each application's "fair share" of each shared area to give a realistic measure.

For example here:

# smem -t
  PID User     Command                         Swap      USS      PSS      RSS
10593 root     /usr/lib/chromium-browser/c        0    22868    26439    49364 
11500 root     /usr/lib/chromium-browser/c        0    22612    26486    49732 
10474 browser  /usr/lib/chromium-browser/c        0    39232    43806    61560 
 7777 user     /usr/lib/thunderbird/thunde        0    89652    91118   102756 
  118 4                                       40364   594228   653873  1153092 

So PSS is the interesting column here because it takes shared memory into account.
Unlike RSS it's meaningful to add it up. We get 654Mb total for userland processes here.

System-wide output tells about the rest:

# smem -tw
Area                           Used      Cache   Noncache 
firmware/hardware                 0          0          0 
kernel image                      0          0          0 
kernel dynamic memory        345784     297092      48692 
userspace memory             654056     181076     472980 
free memory                   15828      15828          0 
                            1015668     493996     521672 

So 1Gb RAM total = 654Mb userland processes + 346Mb kernel mem + 16Mb free
(give or take a few Mb)

Overall about half of memory is used for cache (494Mb).

Bonus question: what is userland cache vs kernel cache here ?

btw for something visual try:

# smem  --pie=name

enter image description here

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. –  Matt May 9 '14 at 11:35

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


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

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

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

share|improve this answer

Your Answer


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.