How to Check Linux Shared Memory using ipcs Command

Last updated: July 25, 2022

Shared memory is the memory that may be accessed by multiple processes; i.e. a memory region that can be shared between different processes and a better way of passing data between two processes. Shared memory is the fastest form of Inter-Process Communication which is currently available.

Assume that the program will create a memory portion, which another process can access (if permitted). A shared segment can be attached multiple times by the same process. Whenever the memory is mapped into the address space of the process, i.e. sharing the common memory region, the kernel will not involve while passing data between the processes. Many applications, like Oracle SGA requires shared memory settings, use this feature.

Check shared memory in Linux

Let's check some IPCS command through some examples.

Print active shared memory segments using -m option.

# ipcs -m
------ Shared Memory Segments --------
key shmid owner perms bytes nattch status
0x00000000 65536 root 600 393216 2 dest
0x00000000 98305 root 600 393216 2 dest

where,

"dest" status means memory segment is marked to be destroyed
"nattach" field shows how many application pid's are still attached to the shared memory

The following command print information about active shared memory segments:

# ipcs -q
------ Message Queues --------
key msqid owner perms used-bytes messages

Print information about active shared memory queues:

# ipcs -s
------ Semaphore Arrays --------
key semid owner perms nsems

Print information about semaphores that is accessible semaphores. The ipcs -l shows limit of shared memory, semaphores and messages.

# ipcs -l
------ Shared Memory Limits --------
max number of segments = 4096
max seg size (kbytes) = 4194303
max total shared memory (kbytes) = 1073741824
min seg size (bytes) = 1

------ Semaphore Limits --------
max number of arrays = 128
max semaphores per array = 250
max semaphores system wide = 32000
max ops per semop call = 32
semaphore max value = 32767

------ Messages: Limits --------
max queues system wide = 16
max size of message (bytes) = 65536
default max size of queue (bytes) = 65536

The below command shows the maximum size of the single memory segment that Linux process can allocate in its virtual address space. You can limit the maximum size of the single memory segment by executing following command.

# cat /proc/sys/kernel/shmmax
4294967295

You can set shmmax value by echoing to the concerned /proc file as below. The following command will set maximum size (in terms of bytes) single memory segment is set to 8388698:

# echo 8388608 > /proc/sys/kernel/shmmax

In a similar fashion, you can set the maximum allowable size of any single message in a System V IPC message queue, in bytes.

# echo 8192 > /proc/sys/kernel/msgmax

You can check the current kernel parameter for semaphore with the following command.

# cat /proc/sys/kernel/sem
250 32000 32 128
where
max number of arrays = 128
max semaphores per array = 250
max semaphores system wide = 32000
max ops per semop call = 32
semaphore max value = 32767

Conclusion

In this tutorial, we learned Linux ipcs command to check shared memory information. I hope you enjoyed reading and please leave your suggestions in the below comment section.

About The Author

Bobbin Zachariah

Bobbin Zachariah

Bobbin started his career in IT in the year 2000 as a computer science instructor and worked as a Linux system engineer and senior analyst roles. Currently working in DevOps environments to increase efficiency and improve delivery time in AWS Cloud infrastructure. He is certified in RHEL, CCNA, and MCP and holds a Masters's in computer science. When in his free time love playing cricket, blogging, and listening to music.

SHARE

Comments

Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment