56

Here is my htop output:

enter image description here

For example, I'm confused by this ruby script:

enter image description here

How much physical memory is it using? 3+1+8+51+51? 51? 51+51?

Panki
  • 153
  • 9
Lai Yu-Hsuan
  • 723
  • 1
  • 5
  • 6
  • 2
    Then the `RES` column is the one you want to look at. – David Schwartz Jun 21 '13 at 09:02
  • 2
    yes, I'm looking at it. But which one is the memory usage by my script? Why are there two `51`s? Does it consume 102 megabytes? Or only 51 megabytes? In the outer process RES contains the inner process RES , why the `3`, `1`, `8` are all fewer than `51`? – Lai Yu-Hsuan Jun 21 '13 at 09:03
  • 2
    There are two 51's because the process forked. It most likely doesn't consume 102 megabytes because the two processes are each using the same 51 megabytes, but you can't easily tell. The other process RES contains the inner process if that memory is resident in the other process as well and not if it doesn't. The RES fields tells you how much physical memory each process is using, but other processes may (or may not) be using that very same physical memory too. – David Schwartz Jun 21 '13 at 09:16
  • 1
    If you want to know if the two processes are using the same memory, use pmap -d – Sirch Jun 21 '13 at 09:28
  • But... as far as I know, when you `fork` a process, its pages will be copied and occupies some new memory space. How can the parent and child process share all 5992 kb memory? Am I wrong? – Lai Yu-Hsuan Jun 21 '13 at 09:39
  • 2
    @LaiYu-Hsuan: Yes, you're wrong. The pages aren't copied, they're shared. They're only copied if either process writes to the page, then they have to be unshared. (The is called "copy on write" or "CoW".) – David Schwartz Jun 21 '13 at 10:58
  • 1
    I got it. May be you should sum these up as an answer so I can accept it? – Lai Yu-Hsuan Jun 21 '13 at 11:51

3 Answers3

79

Hide user threads (shift + H) and close the process tree view (F5), then you can sort out the process of your interest by PID and read the RES column (sort by MEM% by pressing shift + M, or F3 to search in cmd line)

nrc
  • 1,071
  • 8
  • 8
8

Memory is a hard thing, you cannot calculate used physical memory by just running ps/htop/top. Memory can be shared between processes.

I recommend you to check usage with this script:

http://www.pixelbeat.org/scripts/ps_mem.py

GioMac
  • 4,444
  • 3
  • 24
  • 41
  • Your script doesn't seem to do the math for shared mem for multiple processes correctly. I run two identical java programs, each reported to be using 300+300=600MiB when doing `ps_mem -p pid`. When doing `ps_mem -p pid1,pid2` I get 600+600=1200MiB. Wouldn't the shared portion be shared by both processes, and shouldn't then the result for `ps_mem -p pid1,pid2` should be 600+300=900MiB? When I kill one of the two, 300MiB are freed according to `free`. When killing the other process, another 300MiB are freed. – Evgeniy Berezovsky Mar 12 '20 at 05:08
  • @EugeneBeresovsky no, it's not necessary shared between these two or these two only – GioMac Mar 13 '20 at 06:20
  • 1
    Isn't everything a 'hard thing' until its not? – IlliakaillI Jul 31 '20 at 19:13
0

Take a look at the Column RES and SHR.

RES - SHR = Total estimated memory usage by the process.