How to get the real cached/prefetched memory usage in Linux?

1

I do face the same hangs like this post: https://askubuntu.com/questions/41778/computer-freezing-on-almost-full-ram-possibly-disk-cache-problem

To sums up: looks like prefetched/cached/paged RAM reclaiming takes too long, so I live in constant fear that the disk makes insane disk readings to the point that the PC hangs and forces me to reboot.


So I've digged more into this problem and got that useful command :

sync && echo 3 | sudo tee /proc/sys/vm/drop_caches

Which do drop the prefetched RAM... up to a point since there's still some left, which seems to be a "static" part.

I've noticed that adding this to the prefetchless RAM usage matches top's used RAM.

However, this doesn't tell me how much "static" prefetched RAM I'm using. And knowing this will let me give enough time to Linux's paged cache offloading to do its job when I'll be about to reach the total RAM count.

E.g: Cache was ~1000 MiB, then got to ~700 MiB after doing "echo 3 | sudo tee /proc/sys/vm/drop_caches"; the ~700 MiB are the "static" part of the prefetched RAM that I need to know.

So my question is: How do you get the -real- "static" part of the Prefetched/Cached/Paged RAM usage ?


PS:

On the other hand, is there a way to only output the total RAM used by every processes -even root's ones- (like htop's mem value) ?

X.LINK

Posted 2018-01-12T17:17:04.620

Reputation: 1 935

Answers

2

You can find the RAM used by every process for all users with the following command:

ps -eo pid,command,rss,user

That gives, in order, the process id, command, the memory used, and the user of the process.

If you want the total amount of RAM being used by all of the processes, the the following command will work

free

If you want a SUM of the memory being used by every process, use the following code:

ps -eo rss | awk '{sum+=$1} END {print sum}'

That will give you a value which is the total amount of memory being used by all of the processes.

Nasir Riley

Posted 2018-01-12T17:17:04.620

Reputation: 886

Doh, sorry if I wasn't clear. By "all the RAM", I meant the total RAM used by every processes. – X.LINK – 2018-01-12T17:58:23.680

1See my edit above – Nasir Riley – 2018-01-12T19:25:05.730

It did showed the total amount, but there's other outputs too. I could grep that, but I need a single -and only- output so I can do some repeating math without using too much resources. What's more apriopriate would be how free get that value internally, but I unfortunately don't know how to code. So there must be another way to get such numbers. – X.LINK – 2018-01-12T21:10:20.140

1I have added another edit. – Nasir Riley – 2018-01-12T21:39:46.057

I'll be damned, by replacing "%mem" by "rss" you've got me on the right path for the whole answer. What the RSS total shows is the real memory usage without the "dynamic" part of prefetched RAM, and neither top's "used" or neither htop's RAM nonsense values ! Thanks ! – X.LINK – 2018-01-13T05:46:33.637

No problems. I've updated my answer with rss replacing %mem as it is more fitting your question. – Nasir Riley – 2018-01-13T13:42:42.660