How to check if kernel overcommit is enabled in Linux

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to check the virtual memory overcommit setting in Linux. You will use the sysctl command to view the vm.overcommit_memory kernel parameter and understand its different values.

You will also explore the /proc filesystem, specifically the /proc/sys/vm directory, to see how kernel parameters are exposed as files. Finally, you will inspect the /etc/sysctl.conf file to see how these settings can be persistently configured. This lab will provide a hands-on understanding of how to verify and understand kernel overcommit settings in a Linux environment.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("Linux")) -.-> linux/BasicFileOperationsGroup(["Basic File Operations"]) linux(("Linux")) -.-> linux/UserandGroupManagementGroup(["User and Group Management"]) linux(("Linux")) -.-> linux/BasicSystemCommandsGroup(["Basic System Commands"]) linux/BasicSystemCommandsGroup -.-> linux/echo("Text Display") linux/BasicFileOperationsGroup -.-> linux/ls("Content Listing") linux/BasicFileOperationsGroup -.-> linux/cat("File Concatenating") linux/UserandGroupManagementGroup -.-> linux/env("Environment Managing") subgraph Lab Skills linux/echo -.-> lab-558796{{"How to check if kernel overcommit is enabled in Linux"}} linux/ls -.-> lab-558796{{"How to check if kernel overcommit is enabled in Linux"}} linux/cat -.-> lab-558796{{"How to check if kernel overcommit is enabled in Linux"}} linux/env -.-> lab-558796{{"How to check if kernel overcommit is enabled in Linux"}} end

Check overcommit with sysctl vm.overcommit

In this step, you will learn how to check the virtual memory overcommit setting in Linux using the sysctl command.

Virtual memory overcommit is a feature in Linux that allows the system to allocate more memory to processes than is physically available. This can be beneficial in some cases, but it can also lead to issues if processes actually try to use all the memory they've been allocated.

The sysctl command is used to view and modify kernel parameters at runtime. We will use it to check the value of the vm.overcommit_memory parameter.

Open the terminal if you haven't already. You can do this by clicking the Xfce Terminal icon on the left side of the desktop.

Now, type the following command into the terminal and press Enter:

sysctl vm.overcommit_memory

This command asks the kernel to show the current value of the vm.overcommit_memory parameter.

You should see output similar to this:

vm.overcommit_memory = 0

The value 0 indicates the default heuristic overcommit handling. Other possible values are 1 (always overcommit) and 2 (never overcommit). Understanding this setting is important for system performance and stability, especially in environments with high memory demands.

Click Continue to proceed to the next step.

Verify settings in /proc/sys/vm

In this step, you will explore the /proc filesystem, specifically the /proc/sys/vm directory, to see how kernel parameters related to virtual memory are exposed.

The /proc filesystem is a virtual filesystem that provides information about processes and other system information. It's a great place to inspect the kernel's current state. The /proc/sys directory within /proc contains files that correspond to kernel parameters that can be viewed and sometimes modified at runtime.

The /proc/sys/vm directory holds files related to the virtual memory subsystem. The vm.overcommit_memory parameter we checked in the previous step has a corresponding file in this directory.

Let's use the cat command to view the content of the file that represents vm.overcommit_memory. The cat command is used to display the content of files.

Type the following command into your terminal and press Enter:

cat /proc/sys/vm/overcommit_memory

This command will read the content of the overcommit_memory file located in /proc/sys/vm and print it to your terminal.

You should see the same value that you saw with the sysctl command in the previous step:

0

This demonstrates that sysctl is essentially reading from or writing to these files in the /proc/sys filesystem.

You can also list the files in the /proc/sys/vm directory to see other virtual memory related parameters. Use the ls command:

ls /proc/sys/vm/

You will see a list of files, each representing a different kernel parameter related to virtual memory.

compact_hueristic
compact_memory
dirty_background_bytes
dirty_background_ratio
dirty_bytes
dirty_expire_centisecs
dirty_ratio
dirty_writeback_centisecs
drop_caches
extfrag_threshold
hugetlb_shm_group
laptop_mode
lowmem_reserve_ratio
min_free_bytes
min_slab_ratio
min_unmapped_ratio
mmap_min_addr
nr_hugepages
nr_hugepages_mempolicy
nr_overcommit_hugepages
numa_balancing
numa_balancing_scan_period_max_ms
numa_balancing_scan_period_min_ms
numa_balancing_scan_size_mb
numa_balancing_settle_count
numa_balancing_timeout
oom_dump_tasks
oom_kill_allocating_task
overcommit_memory
overcommit_ratio
page-cluster
panic_on_oom
percpu_pagelist_fraction
stat_interval
swappiness
user_reserve_kbytes
vfs_cache_pressure
watermark_boost_factor
watermark_scale_factor
zone_reclaim_mode

This gives you a glimpse into the many tunable parameters available in the Linux kernel's virtual memory subsystem.

Click Continue to move on.

Inspect config with cat /etc/sysctl.conf

In this step, you will learn about the /etc/sysctl.conf file, which is used to configure kernel parameters persistently across reboots.

While you can change kernel parameters at runtime using the sysctl command or by writing to files in /proc/sys, these changes are usually temporary and will be lost when the system restarts. To make kernel parameter changes permanent, you typically add them to the /etc/sysctl.conf file or files in the /etc/sysctl.d/ directory.

When the system boots up, it reads the settings from /etc/sysctl.conf and applies them.

Let's use the cat command again to view the content of the /etc/sysctl.conf file.

Type the following command into your terminal and press Enter:

cat /etc/sysctl.conf

This will display the contents of the main sysctl configuration file.

You might see lines starting with #, which are comments and are ignored. Other lines will be in the format parameter = value, similar to what you saw with the sysctl command output.

#
## /etc/sysctl.conf - Configuration file for setting system variables
## See /etc/sysctl.d/ .conf files for other system variables
#

#kernel.domainname = example.com

## Uncomment the following to stop low-level messages on console
#kernel.printk = 3 4 1 3

##############################################################3
## Functions previously found in netbase
#

## Uncomment the next two lines to enable Spoofing attacks protection
#net.ipv4.conf.default.rp_filter=1
#net.ipv4.conf.all.rp_filter=1

## Uncomment the next line to enable TCP/IP SYN cookies
#net.ipv4.tcp_syncookies=1

## Uncomment the next line to enable packet forwarding for IPv4
#net.ipv4.ip_forward=1

## Uncomment the next line to enable packet forwarding for IPv6
#net.ipv6.conf.all.forwarding=1

##############################################################3
## Blow away nobody in the kernel
#

#kernel.yama.ptrace_scope = 1

##############################################################3
## Magic system request Key
#

#kernel.sysrq = 176

##############################################################3
## Dynamic Shared Memory:
#

#kernel.shmmax = 4294967295
#kernel.shmall = 268435456

##############################################################3
## Other settings
#

#fs.file-max = 100000

#vm.swappiness = 60
#vm.vfs_cache_pressure = 50

#kernel.nmi_watchdog = 0

Notice that the vm.overcommit_memory parameter might not be explicitly set in this file. If a parameter is not set in /etc/sysctl.conf or /etc/sysctl.d/, the kernel uses its default value. The value you saw with the sysctl command and in /proc/sys/vm/overcommit_memory is the currently active value, which could be the default or set by another configuration file or script during boot.

Understanding /etc/sysctl.conf is crucial for persistent system tuning.

Click Continue to finish this lab.

Summary

In this lab, you learned how to check the virtual memory overcommit setting in Linux. You used the sysctl vm.overcommit_memory command to view the current value of this kernel parameter, understanding that a value of 0 indicates the default heuristic handling, 1 means always overcommit, and 2 means never overcommit.

You also explored the /proc filesystem, specifically the /proc/sys/vm directory, to see how kernel parameters like vm.overcommit_memory are exposed as files within this virtual filesystem, providing an alternative way to inspect the kernel's current state regarding virtual memory settings.