Shared memory transport for ROS 2 images and point clouds
Maik13579 introduces ros2_shm_sensor_transport, a shared-memory library for ROS 2 that moves large sensor payloads without relying on DDS serialization. The tool supports Image, CompressedImage, and PointCloud2 types with C++ and Python APIs, offering benchmarks where C++ intra-process communication achieved 0.038 ms latency versus 0.417–0.857 ms for other paths under specific test conditions.
Article text · Original source
Hi everyone!
I’ve been working on ros2_shm_sensor_transport and wanted to share it here.
Part of the motivation is that I don’t particularly enjoy tuning DDS. I wanted a way to move large sensor payloads through shared memory without depending on a particular DDS implementation or its shared-memory settings. The payload path is independent of DDS, while the small metadata messages still use the normal ROS middleware.
The idea is pretty simple: keep the big image or point-cloud payload in shared memory and use ROS messages to tell subscribers where to find it. It works between separate processes on the same Linux machine, with both C++ and Python publishers/subscribers.
Each publisher creates its own POSIX shared-memory ring buffer. You choose how many slots it has and how much payload each slot can hold. Publishers on the same topic still own separate buffers.
When a new image or point cloud arrives, the publisher copies its data array into the next slot. It then publishes a small metadata message containing the usual header and sensor layout, plus the shared-memory name, slot, payload size, and sequence number. For /camera/image_raw, that metadata goes to /camera/image_raw/_shm.
The subscriber opens the buffer read-only and copies the advertised payload into its own memory. It checks the slot’s sequence number before and after the copy. If the slot was overwritten or changed during the copy, the frame is dropped. Once a frame has been copied successfully, the subscriber can keep it even after the publisher reuses the slot or shuts down.
The publisher doesn’t wait for readers, so slow subscribers can miss frames when the ring wraps around. There are still copies into and out of shared memory. The saving comes from keeping the large payload out of DDS serialization and transport, while ROS still handles metadata delivery and discovery.
It supports Image, CompressedImage, and PointCloud2, and comes with C++ and Python APIs, transport plugins, RViz displays, and relays. The shared-memory part lives in a separate library, ros2_shm_transport, so it can also be used for other data.
The sensor subscribers also have a built-in rate_limit_hz option. If your camera publishes at 60 Hz but you only want to run a Python machine learning model at 10 Hz, you can limit how often the processing callback runs directly on the subscriber. There’s no need for a separate throttle node, and other subscribers can keep processing at the full camera rate.
One part I find especially handy is the image_transport / point_cloud_transport integration. If your sensor driver already uses them, you can use the SHM path without changing its code.
For example, you can build and source these packages, run an ordinary usb_cam node, and open the stream with the SHM Image display in RViz. With the default transport plugin settings, there’s no extra relay or SHM configuration needed.
I also put together some benchmarks, comparing normal ROS subscriptions, direct SHM, a SHM relay, and C++ intra-process communication. The publisher is C++ in every case; subscribers are either C++ or Python.
For 256 KiB at 240 Hz, every path received all 1,200 frames:
| Subscriber | Normal ROS median latency | Direct SHM |
|---|---|---|
| Python | 0.857 ms | 0.666 ms |
| C++ | 0.417 ms | 0.392 ms |
C++ intra-process communication was still much faster at 0.038 ms
A bit of context for those numbers: I didn’t do any DDS or Linux buffer tuning. The larger-message cases had dropped frames on the normal subscription paths, and I haven’t pinned down why. UDP buffer limits and DDS settings can make a big difference, so please read these as results from this particular setup.
DDS has shared-memory options too, and the requirements depend on which mechanism you use. For example, Fast DDS data-sharing requires bounded message types, while its zero-copy path requires plain, bounded types. Standard ROS Image and PointCloud2 messages have unbounded strings and arrays, including header.frame_id, so they don’t meet those requirements. This also applies when your sensor always produces the same amount of data.
That restriction doesn’t apply to Fast DDS’s shared-memory transport, which can carry serialized variable-length messages too.
There’s also the newer rosidl::Buffer feature introduced with Lyrical. It lets uint8[] fields use different memory backends without changing the ROS message definition. A suitable backend can, for example, keep image data in GPU memory and exchange a descriptor instead of copying the whole payload through CPU memory. The actual transport and copy behaviour depend on the backend and middleware. My robots still run Humble and Jazzy, where this feature isn’t part of the standard releases, so I wanted something I could use with those setups too.
On my robots, I usually try to keep the sensor driver and perception components in the same process and use intra-process communication wherever I can. Sometimes I still need to get the data into another process, for example for a Python consumer or because I want process isolation. That’s where this has been useful for me.
There are also Nav2 and MoveIt integrations.
How do you handle sensor streams on your robots? Do you keep the driver and perception nodes in one process, use normal ROS topics between processes, or use another approach? I’d love to hear what works for you and where you’ve run into problems. Please share your setup in the comments!
And if you give this a try with your camera or LiDAR, let me know how it goes. Feedback, questions, and bug reports are welcome!