Shared Memory: The Fastest Way to Exchange Data on the Same PC — and Its Real Costs

ayumaxPluginsObjectDeliverer

Shared Memory: The Fastest Way to Exchange Data on the Same PC — and Its Real Costs

When two applications on the same PC need to exchange data, the instinct is to reach for a network protocol: open a port, connect over localhost, send packets. It works, but it is worth knowing there is a faster option that skips the network entirely — shared memory.

Shared memory is an area of memory that two processes can both read and write. One app writes a value; the other app reads it. Strictly speaking it is not communication at all: there is no socket, no packet, no network stack in between. That is exactly why it is fast, and also why it behaves very differently from the protocols you may be used to. This article covers both sides — the speed is real, and so are the limits.

How it works

The operating system sets aside a named region of memory. Any process that opens that name sees the same bytes. A writer copies data in; a reader copies data out. That is all shared memory itself provides — a shared area of bytes, with no message structure, no notification, and no bookkeeping built in.

Keep that in mind while reading the figure below. It shows the layout as ObjectDeliverer implements it (created by giving a name and a size in bytes; Windows only). The data area is what shared memory gives you. The small counter area at the head is not part of shared memory — it is something ObjectDeliverer adds, and the distinction matters, as the next sections explain.

Figure: One memory region shared by two processes; the counter at the head is ObjectDeliverer's addition

The honest downsides

The size is fixed up front. The region is allocated before communication starts, and you cannot write more than it holds. If your data grows, you reallocate and restart the exchange. There is no elastic buffer quietly growing behind the scenes.

Values can be skipped — and plain shared memory cannot even tell you. The reader gets no signal when new data arrives; it has to poll the memory to check. If the writer writes twice between two polls, the first of those values is overwritten before anyone saw it. It is gone — shared memory has no queue and no retransmission. Worse, nothing in the mechanism records that anything was lost. If you build on raw shared memory yourself, detecting skips is your own problem to design for.

Varied data fits badly. One region naturally carries one kind of data with one shape. If you want to send several different kinds of messages, you end up splitting the region into areas and managing the layout yourself. It can be done, but at that point the simplicity that made shared memory attractive is mostly gone. A protocol with messages and framing (see TCP Has No Message Boundaries) is usually the better tool for mixed data.

What ObjectDeliverer adds on top

A library cannot remove those limits — they come from the mechanism itself. What it can do is handle the bookkeeping for you, and it is worth being precise about which parts are the library’s work rather than shared memory’s:

  • Polling is automatic. ObjectDeliverer checks the memory for new writes on its own, so your code simply receives data as it arrives.
  • A write counter detects skips. The writer also writes a counter in the head area, incremented on every write. The reader compares it with the previous value; if it jumped by two, one value was skipped. You cannot recover the lost value, but you can know it was lost — which is the difference between a monitoring display that silently drops a frame and one that can log, count, or flag the gap.
  • Framing still applies. Messages inside the region follow a PacketRule, the same framing agreement as on other protocols.

If you ever work with shared memory created by another program, expect none of this to be there unless that program added it. The counter, in particular, is ObjectDeliverer’s own convention — not a feature of shared memory, and not something other tools will write or read.

What it is actually good for

Put the downsides together and a clear profile emerges. Shared memory fits data that is:

  • Fixed-length (or at least fixed maximum size)
  • Produced continuously, as a stream of snapshots
  • Loss-tolerant — the newest value matters, and an occasional skip is acceptable, especially when you can detect it

Two examples match that profile well:

  • Video frames. An app captures an image every frame and hands it to another app for display or analysis. Frames are large, fixed in size for a given resolution, and if one frame is skipped the next one arrives a moment later anyway. This is also where the speed matters most — large data, many times per second, with no network stack in the way.
  • Fixed-format sensor data. A monitoring app reads the latest measurements from a device-facing tool in a fixed layout: temperatures, positions, voltages. Each snapshot replaces the previous one, and a write counter like ObjectDeliverer’s tells you if any snapshot was missed along the way.

And the mirror image: if every value must arrive — commands, events, transactions — shared memory is the wrong tool, however fast it is. Use TCP and accept the overhead. Speed never compensates for silently losing the one message that mattered.

The short version

Shared memory is less a communication method than a shared notebook with exactly one page. Writing on the page is nearly free, and anyone watching the page sees the update immediately. But the page has a fixed size, there is no history, and if you write twice before your partner looks, the middle note is gone. For streams of fixed-size snapshots on the same PC — video frames, sensor readings — that trade is often exactly right.

Follow @AyumaxSoft on X to get the latest updates from AyumaxSoft as soon as they are available.

Related Posts