Memory Management

Here is a high-level overview of the types of memory management that are going on “behind the scenes.”

SNAP Buffers

The SNAP Protocol Stack uses a pool of “packet buffers”, each 123 bytes long.

When you send a packet, receive a packet, print, etc., you are temporarily using up one of these packet buffers, which will later be returned to the global pool.

As a concrete example, when your script makes a call to the rpc() function, the actual RPC packet gets encoded into a buffer and enqueued to the radio code for transmission. Once the radio has actually sent the packet (possibly after a Mesh Route Discovery has first taken place), the packet will be returned to the pool.

Buffer Budgets

The “buffer pool” is shared among the various data sources, but no single source is allowed to use up all of the buffers. These “budget” numbers refer to how many buffers an individual data source is allowed to request.

Note

An individual budget number represents the maximum number of buffers that could get allocated to that particular function at one time. Because the buffer pool is “over-subscribed”, there may be fewer than the “max budget” buffers available.

As a concrete example, on the RF100 there are only 2 buffers allocated to STDOUT (“print” statements). If your script printed more than 246 characters in one burst, then the excess characters would be dropped (not printed). If your device was busy processing a lot of inbound and outbound RPC calls, there might only be one buffer available to perform print statements, resulting in characters being dropped after the first 123.

Note

Use of the HOOK_STDOUT and HOOK_RPC_SENT events can help you make more efficient use of your packet buffers.

Dynamic Strings and Byte Lists

Four pools of buffers are used to service both the string and byte list operations. See RF Modules for the maximum size and quantity of the tiny, small, medium, and large pools.

As a concrete example, the following line of SNAPpy will use up one dynamic string buffer:

message = 'Hello, ' + nameStr

Note

The default value of stdinMode() is line mode which reserves one string buffer of the largest size available on the platform. To reclaim this string buffer for other use, set stdinMode() to character mode from within your SNAPpy script.