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.
Static data: variables that exist for the whole run. Their total size is fixed when the firmware is built.
Stacks: one region for each thread, which is an independent sequence of instructions, and one for interrupt handlers §6.12, the routines that respond to hardware events. A stack holds the data of the functions currently running. The size of each region is fixed; how much of it is in use changes all the time.
Heap: a region from which the firmware requests blocks while it runs, and to which it returns them. Each block carries a few extra bytes of bookkeeping.
Buffers lent to hardware: memory that a hardware device of the chip reads or writes by itself during a transfer, which is called direct memory access §4.36.
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.
List the objects that the operation touches, and where each one is allocated and released.
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.
Add what the source code does not show at that instant: the deepest stack use and the bookkeeping of the heap.
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 KiB. After the switch, the old table and the buffer are released. The use falls by KiB to 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 have size , counted with its bookkeeping bytes. Let it be alive from time , when it is allocated, up to but not including time , when it is released. The memory in use at time is the sum over all objects that are alive at . The transient peak is the largest value that takes during the operation. The operation fits in a region of capacity only if the peak is at most 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.
The old object, which stays in use until the switch.
The new object, which is being built.
The buffers that carry the incoming data, including any buffer that a hardware device or a queue still holds.
Temporary working data of parsing and checking.
An old object that has been replaced but that some part of the program is still reading.
The stacks, the working areas of running code, at their deepest use at that instant, and the bookkeeping bytes of the heap, the region for memory requested at run time.
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).