1. Overview

The memory usage of the Linux kernel is crucial as it directly affects the performance of our system. Specifically, we may need to monitor the kernel memory usage to troubleshoot slowdowns, optimize resource utilization, and ensure efficient multitasking.

In this tutorial, we’ll explore different methods for checking the RAM usage of our Linux kernel using the slabtop and dmesg commands and analyzing the /proc/meminfo and /proc/modules files.

These commands have been tested on Ubuntu 22.04 with the 6.2.0-36-generic kernel but can also be applied to other Linux distributions as well.

2. Using the slabtop Command

In Linux kernel memory management, slab refers to a cache that stores frequently used kernel data structures, whereas the slab allocator is the underlying memory allocation subsystem used by the kernel to efficiently manage these caches.

To explain further, we can use the slabtop command to monitor these kernel caches in real time and understand how memory is utilized by the kernel and its various components.

Let’s run the slabtop command in our Linux terminal:

$ sudo slabtop
Active / Total Objects (% used)    : 434936 / 471705 (92.2%)
Active / Total Slabs (% used)      : 17239 / 17239 (100.0%)
Active / Total Caches (% used)     : 118 / 168 (70.2%)
Active / Total Size (% used)       : 100779.97K / 106506.58K (94.6%)
Minimum / Average / Maximum Object : 0.01K / 0.23K / 8.00K
  OBJS ACTIVE  USE OBJ SIZE  SLABS OBJ/SLAB CACHE SIZE NAME                   
109356  86412  79%    0.10K   2804       39     11216K buffer_head
 53760  53760 100%    0.19K   2560       21     10240K dentry
 43180  43144  99%    0.05K    508       85      2032K shared_policy_node
...

In the above output:

  • Active / Total Size (% used) represents the total memory used by all active slabs in kilobytes, the total memory available for the slabs to use in kilobytes, and the percentage of available slab memory that is currently in use
  • Minimum / Average / Maximum Object provides information about the minimum, average, and maximum size of kernel objects in kilobytes across all of the slabs, respectively

Furthermore, the other columns in the slabtop output include details about the individual slab caches:

  • OBJS represents the total number of objects in the slab cache
  • OBJ SIZE refers to the size of each object in the cache in kilobytes
  • CACHE SIZE represents the total cache size in kilobytes

Now, let’s move on to the next method.

3. Analyzing the /proc/meminfo File

The /proc/meminfo file in Linux provides detailed information about memory usage, including physical and virtual memory statistics. Moreover, we can analyze this file to inspect some aspects of kernel memory utilization.

So, let’s view the /proc/meminfo file via cat:

$ cat /proc/meminfo
MemTotal: 2771620 kB
MemFree: 472840 kB
MemAvailable: 1725592 kB
Buffers: 95224 kB
Cached: 1305196 kB
SwapCached: 0 kB
Active: 755572 kB
Inactive: 1253728 kB
Active(anon): 1520 kB
Inactive(anon): 652136 kB
Active(file): 754052 kB
Inactive(file): 601592 kB
SwapTotal: 1190288 kB
SwapFree: 1190288 kB
Writeback: 0 kB
KReclaimable: 62936 kB
Slab: 152840 kB
SReclaimable: 62936 kB
SUnreclaim: 89904 kB
KernelStack: 7360 kB
PageTables: 15684 kB
...

As a result, we can see the detailed information related to memory usage.

In terms of kernel RAM usage specifically, Buffers and Cached represent the memory actively used by the kernel for buffering and caching tasks. Moreover, Slab represents the contiguous block of memory used by kernel data structure instances.

In addition, KernelStack, PageTables, SReclaimable, and SUnreclaim represent the memory used for kernel-related activities such as stack space for kernel threads, page tables, and reclaimable and unreclaimable slab memory, respectively.

4. Analyzing the /proc/modules File

In Linux, the /proc/modules file contains information about currently loaded kernel modules and their relevant memory sizes. However, its content may vary based on the configuration and use of our system.

To analyze the /proc/modules file and display its content in a more readable, tabulated format, we’ll use it with the column -t command:

$ column -t /proc/modules
binfmt_misc          24576   1   -                                  Live  0x0000000000000000  
snd_intel8x0         53248   2   -                                  Live  0x0000000000000000  
snd_ac97_codec       200704  1   snd_intel8x0,                      Live  0x0000000000000000  
...

The first column represents the name of the loaded kernel module, and the second column indicates the memory size of the respective module in bytes.

5. Using the dmesg Command

dmesg is a tool that displays the message buffer of the kernel. Moreover, with the help of the pipe (|) operator, we can redirect the output of dmesg to grep to display kernel messages related to memory only.

So, let’s run the dmesg command in our terminal and identify the kernel memory line:

$ sudo dmesg | grep Memory
[    0.091684] Memory: 2697736K/2876984K available (20480K kernel code, 4153K rwdata, 12732K rodata, 4768K init, 17536K bss, 178988K reserved, 0K cma-reserved)
...

Here, we can see a timestamp at the start of the line, which represents the time in seconds when the kernel started. After that, we see the total available memory on the system in kilobytes. Additionally, this memory usage is further divided into different categories such as kernel code, rwdata (read-write data), rodata (read-only data), init (initialization code), and more.

6. Conclusion

In this article, we’ve learned different methods to check the RAM usage of a standard Linux kernel. These methods included the use of the slabtop and dmesg commands, as well as analyzing the /proc/meminfo and /proc/modules files.

Naturally, we can use slabtop for dynamic and detailed observation of the kernel’s slab cache. Meanwhile, dmesg gives insights into the memory usage of the kernel at boot time. Furthermore, the /proc/meminfo file contains detailed system-wide memory data, including the kernel memory usage. Finally, the /proc/modules file lists the currently loaded kernel modules and their memory sizes.

Ultimately, we can choose to use any of these methods, depending on what details we want to determine about the RAM usage of our Linux kernel.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.