Linux – Ubuntu Linux: Process swap memory and memory usage

linuxmemoryswapUbuntu

My Ubuntu eats more memory than the task manager is showing:

sudo ps -e --format rss  | awk 'BEGIN{c=0} {c+=$1} END{print c/1024}'
2750.29

free -m

             total       used       free     shared    buffers     cached
Mem:          3860       2765       1094          0          3        300
-/+ buffers/cache:       2461       1398
Swap:         2729       2374        354

That's strange. Can someone explain this difference?

But what is more important:
I'd like to know how much memory a process is really using. I don't want to know the virtual memory size, but rather the resident memory plus swap of a process.

I have also tried to output the format param "sz" of 'ps', but the sum of this is to high (16000 MB) (param 'size' gives 36700 MB). Are there any other options?

I really want to use this, to determine which programs/processes are eating to much memory (and swap), to kill them, because memory is valuable 🙂 This just really don't make sense, so I'm asking here.

Output of /proc/meminfo:

MemTotal:        3952812 kB                
MemFree:         1119192 kB
Buffers:            2676 kB
Cached:           290068 kB
SwapCached:       160980 kB
Active:          1805396 kB
Inactive:         731680 kB
Active(anon):    1745820 kB
Inactive(anon):   689184 kB
Active(file):      59576 kB
Inactive(file):    42496 kB
Unevictable:         148 kB
Mlocked:             148 kB
SwapTotal:       2795272 kB
SwapFree:         390900 kB
Dirty:              1984 kB
Writeback:             0 kB
AnonPages:       2085472 kB
Mapped:            67432 kB
Shmem:            190676 kB
Slab:              88012 kB
SReclaimable:      42704 kB
SUnreclaim:        45308 kB
KernelStack:        5496 kB
PageTables:        87860 kB
NFS_Unstable:          0 kB
Bounce:                0 kB
WritebackTmp:          0 kB
CommitLimit:     4771676 kB
Committed_AS:    9522364 kB
VmallocTotal:   34359738367 kB
VmallocUsed:      374404 kB
VmallocChunk:   34359330144 kB
HardwareCorrupted:     0 kB
AnonHugePages:         0 kB
HugePages_Total:       0
HugePages_Free:        0
HugePages_Rsvd:        0
HugePages_Surp:        0
Hugepagesize:       2048 kB
DirectMap4k:       61440 kB
DirectMap2M:     4030464 kB

Best Answer

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 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 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.
    This will cause the free number to be smaller than the top RSS sum.

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