Deal with shm – The Shared Memory Segment – filesystem in RHEL-7
In Simple Terms, Shared memory is chunk of memory that can be shared by multiple processes. An application acquires shared memory by making a system call similar to what it would make to acquire conventional memory. The only difference is that each chunk of shared memory has key and it’s possible for another application to map the same shared memory, by referencing the key.
What is Shared Memory Segment?
In Technical terms, The Linux kernel provides a number of different ways for userspace to communicate with it. Shared Memory Segment i.e. /dev/shm is one of the that userspace uses to store shared memory segments, shared temporary files or sockets.
Just like any other API related file systems The shm file System mounted during very early boot-up of systemd and are generally not listed in /etc/fstab. As shm filesystem is important for kernel-to-userspace and userspace-to-userspace communication it is mounted automatically and without configuration or interference by the user. Even though the default settings of shm file system should normally be suitable for most setups, in some cases it might make sense to set the size of the shm filesystem using the mount options, in /etc/fstab file.
To get the current shared memory usage use the command:
$ ipcs -m
—— Shared Memory Segments ——–
key shmid owner perms bytes nattch status
0x00000000 0 root 644 80 2
0x00000000 32769 root 644 16384 2
0x00000000 65538 root 644 280 2
0x00000000 294916 user 600 393216 2 dest
0x00000000 327685 user 600 393216 2 dest
0x00000000 360454 user 600 393216 2 dest
0x00000000 393223 user 600 393216 2 dest
0x00000000 425992 user 600 393216 2 dest
0x00000000 458761 user 600 393216 2 dest
To get total current shared memory usage:
use the below command that sums up all the values from 5th column
# ipcs -m | awk ‘{ SUM += $5} END { print SUM }’
To know the Current Limits set for Shared Memory Usage:
run the command
$ sudo sysctl -a | grep shm
kernel.shmmax = 68719476736
kernel.shmall = 4294967296
kernel.shmmni = 4096
vm.hugetlb_shm_group = 0
Where
SHMMNI – This parameter sets the system wide maximum number of shared memory segments.
SHMALL – This parameter sets the total amount of shared memory pages that can be used system wide. Hence, SHMALL should always be at least ceiling(shmmax/PAGE_SIZE).
SHMMAX – This parameter defines the maximum size in bytes of a single shared memory segment that a Linux process can allocate in its virtual address space.
Please note that SHMALL is calculated in pages. In x86_64 architecture, a page of memory is 4 KiB in size. So, the total amount of shared memory that the system can allocate here is:
# echo “4294967296*4” | bc
17179869184# echo “17179869184/1024/1024” | bc
16384
You can see this calculated values using either of the following commands
# ipcs -lm
—— Shared Memory Limits ——–
max number of segments = 4096
max seg size (kbytes) = 67108864
max total shared memory (kbytes) = 17179869184
min seg size (bytes) = 1
or
# cat /proc/sys/kernel/shmall
4294967296
# cat /proc/sys/kernel/shmmax
68719476736
# cat /proc/sys/kernel/shmmni
4096
To Identifying the Processes using a Specific Shared Segment:
First , Check ID of Shared Segment
# ipcs -m
—— Shared Memory Segments ——–
key shmid owner perms bytes nattch status
0x0052e2c1 4194319 postgres 600 11083776 2
from the about output the second column gives the ID of shm. i.e. 4194319
Second, check the processes that are currently accessing the Shm id 4194319 using the command :
# lsof | egrep “4194319”
postmaste 15289 postgres DEL REG 0,9 4194319 /SYSV0052e2c1
postmaste 15293 postgres DEL REG 0,9 4194319 /SYSV0052e2c1
the output says the process ids 15289 and 15293 using the Shared Memory Segment 4194319
Finally, we can further look down for the process information using the command
#pgrep 15289
#pgrep 15293
Restrict the Size of SHM by adding the line to /etc/fstab
In order to alter settings of /dev/shm in Red Hat Enterprise Linux 7, add an entry into /etc/fstab specifying the setting and the value. During a fresh reboot, the system will apply the change. As an example, limiting the size of /dev/shm to 128M on a specific Red Hat Enterprise Linux 7 system, the following entry needs to be added to /etc/fstab
shmfs /dev/shm tmpfs defaults,size=128M 0 0
1 Response
[…] Deal with shm – The Shared Memory Segment – filesystem in RHEL-7 […]