ROS 2 loaned messages give a publisher or subscription a way to use storage managed by the middleware instead of always allocating an ordinary application-owned message. That capability can remove copies in a suitable configuration. Calling a loan API, however, does not by itself prove that a camera frame or point cloud traveled from producer to consumer without a copy. The result depends on the message type, client library, selected RMW implementation, middleware transport, process topology, and the exact publish and take paths used at runtime.

The official ROS 2 zero-copy design document starts with memory ownership. A publisher may create and retain its own message, or it may borrow a message instance, populate it, and return ownership during publication. A subscription may provide its own storage or take loaned samples and release them later. Those are lifecycle contracts, not performance measurements.

Borrowing has a fallback path

The rclcpp LoanedMessage wrapper represents storage that may come from the middleware. The design also anticipates a fallback allocator when the middleware cannot loan memory. This distinction is critical: application code can retain the same high-level pattern while the underlying allocation source changes.

An integration test should therefore ask whether the publisher reports loan support in the current RMW configuration and should record which implementation is active. It should not turn the successful construction of a LoanedMessage into a claim that shared-memory transport is active. A fallback allocation can still produce correct data while failing the intended copy-elimination objective.

The publisher's ownership changes at publish time. Once a borrowed message is handed back for publication, the application must not continue treating that storage as ordinary mutable memory. A separate asynchronous loan use case, where hardware-owned memory remains immutable until the middleware signals completion, was explicitly outside the scope of the original design document. A driver cannot assume that a loaned publish automatically grants that hardware-buffer lifetime contract.

Subscriber loans are finite resources

On the receive side, a middleware loan must be released. The ROS 2 design warns that loan pools are finite and recommends returning loans as soon as feasible. Holding a sample while performing slow inference, writing a bag, or waiting on another service may exhaust available loan resources. The resulting effect can be dropped messages or stalled publications, depending on the implementation and conditions.

This changes the callback design. Extract the information needed for long-lived work or transfer ownership through a documented mechanism, then release the middleware loan promptly. A reference or pointer into the loaned sample must not escape beyond the loan lifetime. A successful callback return is weak evidence if another thread keeps an alias to storage that has already been released.

The message type matters

The original design is limited to plain-data message types. Dynamically allocated members introduce allocator and layout concerns. Strings, unbounded sequences, nested variable-length arrays, and custom allocators can prevent a simple fixed-size loan from representing the whole message without additional allocation or copying.

For a large sensor stream, define the message contract before optimizing. Fixed-size bounded fields are easier to place in middleware-managed pools than arbitrarily growing containers. This is not a reason to distort an interface merely to claim zero copy. It is a reason to measure the chosen type and document which fields, if any, still allocate.

Topology and transport matter too

Intra-process delivery, inter-process delivery on the same host, and network delivery are different paths. Middleware-backed shared memory may be available for one path and unavailable for another. Serialization can still be required for network transport, for incompatible endpoints, or for a path that leaves the shared-memory domain. A publisher-side loan does not erase those boundaries.

Likewise, one subscriber receiving a loan does not prove that every matched subscriber receives the same storage without copying. The middleware may need different representations for different endpoints. A monitoring tool, bridge, recorder, or remote consumer can change the route exercised by a test.

Record the graph used for evidence: publisher and subscriber processes, host placement, RMW implementation, middleware configuration, message type support, QoS profile, and every additional matched endpoint. Repeat the test with the production graph rather than a single-process demo alone.

Separate correctness from copy evidence

First verify correctness. Populate each borrowed message with a sequence number, source timestamp, payload length, and deterministic payload pattern. Confirm that the subscriber receives the expected values and releases the loan under normal, timeout, shutdown, and exception paths. Run with multiple outstanding samples to expose pool pressure. Check that a shutdown does not destroy a publisher or subscription while loans remain outstanding.

Then gather evidence about memory behavior. Record whether loaning is reported as supported, whether the high-level wrapper used middleware storage or allocator fallback, and whether the chosen transport announces a shared-memory or zero-copy mode. Use implementation-provided counters or traces where available. Profile allocations and copies around the actual publish and take calls. Comparing addresses inside one process can support a narrow observation, but it does not prove the path across processes or endpoints.

Create a negative control. Disable loaned-message behavior where the platform supports that configuration, or select an RMW that cannot provide the loan. The application should remain correct while the observed allocation or copy behavior changes. If the supposed zero-copy metric does not change, the metric probably does not measure the intended boundary.

Budget loan duration

Treat each borrowed sample as a pool resource with a deadline. Track loans outstanding, peak loan age, release failures, pool-exhaustion events, publish stalls, receive gaps, and callback duration. A throughput chart without loan-age data can hide a consumer that slowly captures the entire pool.

Set an ownership rule that is visible in code review: the borrower may write or read only within the loan's valid scope; publication transfers the publisher loan; receive-side processing releases the subscription loan; no alias survives release. Put slow or blocking work after a bounded copy of the required fields unless the middleware exposes a safe ownership transfer for the complete operation.

What can be claimed

A defensible result is specific: under one recorded ROS 2 distribution, RMW implementation, middleware configuration, message type, process topology, QoS profile, and endpoint graph, the publisher and subscriber reported loan support; the runtime used middleware-managed storage; traces showed the intended shared-memory path; and the measured boundary performed no observed payload copy. Even that statement applies to the tested boundary and configuration.

The phrase "we called loan_message" is much weaker. Loaned messages are an ownership mechanism that enables copy avoidance when the rest of the stack supports it. End-to-end zero copy is a property to verify across the entire data path, not a property conferred by one API call.