7

I need to find the RAM usage of kernel space and user space memory used. On seeing

cat /proc/meminfo

I couldn't able to find the exact details. Is there any command line to find the RAM usage of kernel space and user space memory used.

fasil
  • 175
  • 2
  • 2
  • 9

2 Answers2

11

Try:

sudo slabtop

or

sudo cat /proc/slabinfo

These should give you enough information to estimate the total kernel memory consumption.

You can read more info about kernel and userspace memory here.

Avio
  • 2,700
  • 6
  • 30
  • 50
1

Could you elaborate a bit what do you mean by "kernel" in this context and why would you want to separate that from the rest of the system?

You can use

cat /proc/meminfo

or

free -m

to get some idea about memory usage in general. In general "available memory" will be the maximum amount of RAM that can be acquired in near future by processes and if more is requested, the system will slow down. Also note that using all "available memory" also requires sacrifying all of the disk cache which will make future disk access slower.

The memory used for buffers/cache (nowadays a same thing but historically Linux had separate memory areas for those needs) can be examined with sudo slabtop -sc – that displays buffer/cache ("slab cache") usage and active use percentage (you can think this as cache hit rate). If the items that take most RAM have high "USE" percentage, your kernel is working fine.

If you really want to speak about "kernel memory usage" you have to decide if kernel modules, page tables, TCP/IP receive buffers, disk cache, etc. are part of the memory you're interested in. Personally, I really don't care if some piece is technically kernel process or user mode process - if it's required for the working system, it needs to stay anyway.

One interpretation for kernel usage:

grep Memory: /var/log/dmesg | \
  grep -E -o '[0-9]+K (kernel code|data|rwdata|rodata|init)' &&
awk '{print $2/1024 "K " $1 }' /proc/modules | sort -hr

Example output:

8198K kernel code
1290K rwdata
3940K rodata
1428K init
1764K i915
1192K xfs
1068K btrfs
572K kvm
...

See also:

Mikko Rantalainen
  • 14,132
  • 10
  • 74
  • 112
  • The kernel's memory usage would be the quantity of pages times page size for pages allocated by or occupied by machine code executing in ring 0 plus any carve-outs such as for memory-mapped registers of various ICs. Userspace memory usage would be the quantity of pages times page size for pages allocated by or occupied by machine code executing in ring 1. – Andreas ZUERCHER Dec 07 '18 at 14:16
  • @optikos: How about RAM buffers between two user mode processed connected via a pipe? How about RAM cache used to store the data on the fly from user mode to storage? The difference between ring 0 and ring 3 is literally a single value inside the CPU which changes how the RAM is *interpreted*. As a result, the same physical RAM block can be considered as either kernel or user mode memory depending on exact time of the processing. – Mikko Rantalainen Dec 18 '18 at 10:22
  • Also note that kernel will e.g. manage all page tables. You could count those as kernel usage, too, but those are strictly required for user mode process only. If you kill all processes except init, those resources will be released; would that count as "kernel memory"? – Mikko Rantalainen Dec 30 '20 at 11:29
  • 1
    All of @MikkoRantalainen's examples are kernel memory usage due to being allocated by the kernel instead of being allocated in userspace (e.g., as inter-process shared memory). All structures needed by the kernel to administer a ring-3 process is kernel memory, not user memory due to who actually allocated it and who releases it (not who is the headwater of some cascading-ramification river of domino-falling events). Black & white without any shade of grey. – Andreas ZUERCHER Mar 25 '21 at 18:38
  • 1
    I really depends *why* you need to know how much "kernel" is using as opposed to "user mode processes". If you want to know how much will be freed if a given user mode process is killed, the amount of freed memory will include also part of the kernel reserved memory. Yes, it's reserved by the kernel on behalf of user mode process. The "why" defines if you want to include it into total sum or not. – Mikko Rantalainen Mar 31 '21 at 07:28
  • 1
    good point. Your most-recent comment clearly justifies both definitions as valid and that both should be able to be queried. Different horses for different courses. – Andreas ZUERCHER Apr 01 '21 at 01:23