SPY HILL Research
Spy-Hill.net

Poughkeepsie, New York [DIR] [UP]

Configuring Shared Memory on Mac OS X


The default configuration for shared memory on a Mac is best described as "minimal", and is the same on every Mac, regardless of the amount of total memory or the number of processors. These instructions tell you how to alter the shared memory configuation.

Last updated: 28 March 2010

Shared Memory

Shared memory is a form of Inter-Process Communication (IPC). Separate programs running on the same computer are able to communicate with each other by reading from and writing to a commonly allocated memory block.

A good example of how this can be useful is provided by BOINC, the Berkeley Open Infrastructure for Network Computing, which is the software used by distributed computing projects such as SETI@Home and Einstein@Home. The BOINC Core Client is the main program which manages the execution of multiple application programs for the various BOINC projects. When the Core Client starts an application process it provides configuration information and communicates commands to the application via shared memory.

On a multi-processor Mac where the BOINC Core Client is configured to keep suspended processes in memory it is possible that an application program will fail because it cannot allocate shared memory. This results in an error message that looks something like

   2006-05-22 04:46:08 [Pirates@Home] Can't create shared memory: system shmget
   2006-05-22 04:46:08 [Pirates@Home] Unrecoverable error for result wu_1148262635_451_0 (Couldn't start or resume: -144)
The amount of shared memory available on a Mac is configured at boot time. Once the shared memory system has been initiallized it is not possible to change the shared memory configuration[
1]. At present the same amount of shared memory is configured on any Mac (about 4MB), regardless of the number of processors or the amount of total memory available.

You can view the shared memory settings on your Mac by opening the Terminal application (it is in the Applications -> Utilities folder) and giving the command

  sysctl -A | grep shm
which should produce something like:
  kern.sysv.shmmax: 4194304
  kern.sysv.shmmin: 1
  kern.sysv.shmmni: 32
  kern.sysv.shmseg: 8
  kern.sysv.shmall: 1024

Configuring Shared Memory

As of Mac OS X 10.3.9 a relatively simple mechanism has existed for configuring shared memory at boot time. If the file /etc/sysctl.conf exists then the settings in this file are applied at boot time, before the default shared memory settings.

To change the shared memory settings you need to be comfortable with creating and editing files using a text editor such as Emacs, vi, or pico. If you are not familiar with a Unix text editor then you can learn to use Emacs from this "Introduction to the Emacs Editor", which includes simple instructions for taking the Emacs tutorial.

To change the shared memory settings you also need to be able to give commands as the "root" user on your Mac. If you like, you can follow these instructions on "How to Enable the root Account on Mac OS X". However, you can also simply log in as the "Admin" user, open up the Terminal application (it is in the Applications -> Utilities folder) and giving the command:

  sudo emacs /etc/sysctl.conf
It is likely this file doesn't exist on your system, in which case an empty file will be created. Edit this file so that it contains the lines:
kern.sysv.shmmax=16777216
kern.sysv.shmmin=1
kern.sysv.shmmni=128
kern.sysv.shmseg=32
kern.sysv.shmall=4096
These settings increase the amount of shared memory to four (4) times the usual default.

These shared memory settings will be applied the next time the computer boots. You can verify the settings after the reboot using the "sysctl -A" command demonstrated above.

Shared Memory Details

Here are what the particular shared memory kernel settings mean:
shmmax
Maximum size of a shared memory segment
shmmin
Minimum size of a shared memory segment
shmmni
Maximum number of separate shared memory id's
shmseg
Maximum number of shared memory segments per user
shmall
Maximum amount of shared memory (measured in pages). This is generally shmmax divided by 4096.

Allowing an arbitrary process to share memory with any other process on the machine would create a serious security problem. Restrictions can be imposed on which processes can use the shared memory segment, based on the userid and groupid of the processes, much as is done for Unix file permissions. See the shmget(1) man page for details [3].

References and Resources

  1. PostgreSQL: OS X shared memory documentation Besides being a very good summary, the links to the XNU source code were helpful.

  2. Enabling the root Account on Mac OS X

  3. man: shmget

  Copyright © 2010 by Spy Hill Research http://www.Spy-Hill.net/help/apple/SharedMemory.html (served by Islay.spy-hill.com) Last modified: 28 March 2010