Section 6.7

SRAM

Full name: Static random-access memory.

Definition

SRAM (static random-access memory) is working memory that holds each bit in a small circuit which keeps its value while powered. Random access means that any location can be read or written directly. SRAM is volatile: it loses its content without power. The word static separates it from dynamic random-access memory (DRAM), which stores each bit as an electric charge that must be rewritten periodically (refreshed). The working memory of a microcontroller §6.1 (a small computer on one chip) is SRAM. This memory holds the data that the running program changes.

The idea

A processor needs memory that it can change at full speed, any number of times, one byte at a time. Flash memory §6.6, which holds the program, cannot serve: it must be erased in blocks, and it wears out. SRAM can. The price is size. The usual SRAM cell needs six transistors for one bit, where a DRAM cell needs one transistor and a capacitor, so the same chip area holds far fewer bits. A microcontroller has kilobytes of SRAM where a laptop has gigabytes of DRAM.

A workbench is a fair picture: flash memory is the shelf where things are kept, and SRAM is the bench where the work is done. What counts is not how small the finished piece is, but whether everything that lies on the bench at the same moment fits. Replacing a part needs the old part, the new part and the tools side by side. The picture stops in one respect: a bench is cleared on purpose, but SRAM loses everything when power goes, and after a reset with power still on it may hold old content that nothing guarantees to be intact.

How it works

The firmware, a microcontroller’s stored program, uses SRAM in four ways.

Every object, meaning any piece of data that occupies memory, has an object lifetime §4.1: the span from the moment its memory is taken (allocation) to the moment the memory is given back (release). The memory in use at one instant is the sum of the sizes of all objects alive at that instant. These steps find the highest use during an operation.

  1. List the objects that the operation touches, and where each one is allocated and released.

  2. Find the instant at which the most memory is alive. In a replacement it is the instant when the old object, the new object and the transfer buffers all exist.

  3. Add what the source code does not show at that instant: the deepest stack use and the bookkeeping of the heap.

  4. Compare the sum with the capacity, and check that each single request can be met by one unbroken free block.

Worked example

The values in this example are invented. A device replaces a table of 20 KiB by a new version that arrives in pieces. During the replacement three objects are alive: the old table of 20 KiB, which is still in use; the new table of 20 KiB, which is being filled; and a receive buffer of 8 KiB. The use is 20+20+8=4820 + 20 + 8 = 48 KiB. After the switch, the old table and the buffer are released. The use falls by 20+8=2820 + 8 = 28 KiB to 48−28=2048 - 28 = 20 KiB. If 40 KiB are available, the final state fits, because 20 KiB is less than 40 KiB. The replacement fails, because 48 KiB exceeds 40 KiB by 8 KiB.

Unbroken space matters too. A heap has 20 KiB free, in two separate pieces of 10 KiB each. A request for one block of 16 KiB fails although 20 KiB are free, because neither piece is large enough. This effect is called heap fragmentation §4.9.

Stated precisely

Let object ii have size sis_i, counted with its bookkeeping bytes. Let it be alive from time aia_i, when it is allocated, up to but not including time rir_i, when it is released. The memory in use at time tt is U(t)=∑i:ai≤t<risi,U(t) = \sum_{i \,:\, a_i \le t < r_i} s_i , the sum over all objects that are alive at tt. The transient peak is the largest value that U(t)U(t) takes during the operation. The operation fits in a region of capacity CC only if the peak is at most CC and every request finds one free block at least as large as the request.

Two rules decide what counts. An object is alive until its last user has finished, not until it has been replaced: a buffer handed to a hardware device stays alive until the device reports completion. And a region is counted once: a block taken from a heap is part of the heap, not an addition to it.

Limits and common mistakes

Fitting at the end is not fitting on the way. The peak decides, not the final size.

Free bytes in total are not one free block. A heap can refuse a request while more than enough memory is free in pieces.

The sizes that the build tools report cover the static data and the regions reserved for stacks and heap. They do not show how deep a stack or how full the heap gets while the firmware runs.

A measured peak is a lower bound. A measurement that samples the use at intervals can miss a short spike between samples.

Static does not mean constant: the content of SRAM changes all the time. Static says only that a stored bit needs no refresh.

SRAM is not cleared for you: it loses its content without power, but a reset with the power on need not erase it. The product specification of the nRF52840 §6.4, a chip from Nordic Semiconductor, says that the RAM is never reset and that its content may be corrupted, depending on the cause of the reset. Start-up code must set every variable before the firmware uses it.

A low-power mode can switch SRAM off. On the nRF52840 the firmware sets for each section of the RAM whether that section keeps its content in the chip’s deepest sleep mode; a section that is not kept loses its content.

Questions answered

Which allocated objects coexist at the transient peak?

The objects that coexist at the transient peak are those whose lifetimes include the instant of highest memory use. The transient peak is that highest use, which lasts only a short time during an operation. For a replacement or an update the objects are usually these.

To find them, write down where every object is allocated and released, and look for the instant with the largest sum. With an old object of 20 KiB, a new object of 20 KiB and a buffer of 8 KiB, the peak is 48 KiB, although only 20 KiB remain at the end.

Related entries: Heap fragmentation (§4.9), Stack overflow (§4.10), Memory leak (§4.8), Peak versus retained memory (§11.37), Static, stack, and heap storage (§4.11), Object lifetime (§4.1), Flash memory (§6.6).