espsss / reference / quick / nvs-limits

NVS and Preferences limits

The numbers that decide whether a value fits into NVS, and what the Arduino Preferences wrapper returns when it does not. Values from the ESP-IDF v5.5.5 NVS documentation and headers and the arduino-esp32 3.3.12 Preferences library.

In short

  • Keys and namespace names: at most 15 characters (16 bytes with the terminating zero).1
  • A string value is limited to 4000 bytes including the terminating zero, and it must fit into one page.2
  • A blob is limited to 508,000 bytes or 97.6% of the partition size minus 4000 bytes, whichever is lower; blobs are split into chunks across pages.2
  • No more than 254 namespaces in one NVS partition.2
  • Preferences reports failures through return values: begin() returns false, put…() returns 0, get…() returns the default value.3

Limits

LimitValueNotes
Key name15 charactersNVS_KEY_NAME_MAX_SIZE is 16 and includes the terminating zero. Keys are ASCII strings.21
Namespace name15 charactersSame rules as key names (NVS_NS_NAME_MAX_SIZE = NVS_KEY_NAME_MAX_SIZE).21
Namespaces per partition254nvs_open() returns ESP_ERR_NVS_NOT_ENOUGH_SPACE when there are too many.2
String value4000 bytesIncludes the terminating zero. Needs one page with enough contiguous free entries.2
Blob value508,000 bytesOr 97.6% of the partition size minus 4000 bytes, whichever is lower.2
Page4096 bytesOne page is one flash sector.2
Entry32 bytesA page holds a 32-byte header, a 32-byte entry state bitmap and entries 0…125.2

How many entries a value takes

Value typeEntriesNotes
Integer (1…8 bytes)1One entry holds the whole key-value pair.2
String1 + 1 per 32 bytesCounted with the terminating zero; all entries in the same page.2
Blob2 + 1 per 32 bytesChunks may be spread over several pages.2
Preferences Float / Double3As listed for freeEntries() in the Preferences docs.3

What Preferences returns on failure

The Preferences methods do not throw or return an error code; each failure path below returns a plain value. What gets logged differs per method, see the pitfalls.

MethodOn failureNotes
begin(name, readOnly, partition)falseNamespace name: at most 15 characters. In read-write mode a missing namespace is created; read-only begin() on a missing namespace returns false.3
putChar(), putUChar()0On success: 1, the number of bytes stored.3
putString()0On success: strlen() of the value. Also 0 if the namespace is not open or read-only.4
getChar() and other get…()defaultValue0 if no default was given. A missing key is a failure.3
getString(key, buf, maxLen)0Nothing is written; maxLen must cover the stored length including the terminator.3
getString(key, default)defaultValue or ""Arduino String version.3

Commit and wear

ESP-IDF: nvs_commit() is required

With the ESP-IDF API, set operations are not guaranteed to reach flash until nvs_commit() is called on the handle.2

Preferences commits on every put

In Preferences.cpp every put…() method ends with nvs_commit(), directly or through putInt(), putUChar() or putBytes() (Float and Double are stored with putBytes()), so every put is a flash write.4

Updates append, they do not overwrite

A new value is appended to the log and the old one is marked as erased. The docs give a factor of 126 between write and erase operations.2

Pitfalls

A long string can fail with free space left

A string needs contiguous free entries in one page, so a long string can fail on a fragmented partition even when nvs_get_stats reports enough free entries in total. Large data is better kept in a file system.2

Read-only begin() on a new namespace fails

begin(name, true) opens the namespace with NVS_READONLY, and nvs_open() then returns ESP_ERR_NVS_NOT_FOUND if the namespace does not exist yet, so begin() returns false (logged with log_e). Open it once in read-write mode first, or treat false as "nothing stored yet".4

Failed get…() calls are quiet

The integer get…() methods log a failure only with log_v, which is compiled in only when Core Debug Level is Verbose (CORE_DEBUG_LEVEL 5). Calls made before begin() or with a NULL key return the default without any message; so does begin() when the object is already started (it returns false).4

A 16-character key fails

Key names longer than 15 characters are rejected with ESP_ERR_NVS_KEY_TOO_LONG; with Preferences the put returns 0 and the reason goes to log_e.1

A changed partition table leaves stale NVS

If the NVS partition is truncated, for example by a new partition table, its contents should be erased; idf.py erase-flash erases the whole chip.2

Sources

Each number links here; open “What it confirms” for the exact quotes. Checked 2026-11-28.

  1. ESP-IDF v5.5.5: components/nvs_flash/include/nvs.h primary
    What it confirms (4)
    • NVS and Preferences limits: in short “#define NVS_KEY_NAME_MAX_SIZE 16 /*!< Maximum length of NVS key name (including null terminator) */”
    • NVS and Preferences limits: Key name “#define NVS_KEY_NAME_MAX_SIZE 16 /*!< Maximum length of NVS key name (including null terminator) */”
    • NVS and Preferences limits: Namespace name “#define NVS_NS_NAME_MAX_SIZE NVS_KEY_NAME_MAX_SIZE /*!< Maximum length of NVS namespace name (including null terminator) */”
    • NVS and Preferences limits: pitfall: A 16-character key fails “#define ESP_ERR_NVS_KEY_TOO_LONG (ESP_ERR_NVS_BASE + 0x09) /*!< Key name is too long */ … A message providing the reason for a failed call is sent to the arduino-esp32 ``log_e`` facility.”
  2. ESP-IDF v5.5.5 docs (ESP32-S3): Non-Volatile Storage Library primary
    What it confirms (17)
    • NVS and Preferences limits: in short “String values are currently limited to 4000 bytes. This includes the null terminator. … For strings, in case when a key-value pair spans multiple entries, all entries are stored in the same page.”
    • NVS and Preferences limits: in short “Blob values are limited to 508,000 bytes or 97.6% of the partition size - 4000 bytes, whichever is lower. … Blobs are allowed to span over multiple pages by dividing them into smaller chunks.”
    • NVS and Preferences limits: in short “there can be no more than 254 different namespaces in one NVS partition”
    • NVS and Preferences limits: Key name “Keys are ASCII strings; the maximum key length is currently 15 characters.”
    • NVS and Preferences limits: Namespace name “Namespace names follow the same rules as key names, i.e., the maximum length is 15 characters.”
    • NVS and Preferences limits: Namespaces per partition “ESP_ERR_NVS_NOT_ENOUGH_SPACE if there is no space for a new entry or there are too many different namespaces (maximum allowed different namespaces: 254)”
    • NVS and Preferences limits: String value “String values are currently limited to 4000 bytes. This includes the null terminator. … For the String value, at least one page capable of keeping the whole string in a contiguous row of free entries has to be available.”
    • NVS and Preferences limits: Blob value “Blob values are limited to 508,000 bytes or 97.6% of the partition size - 4000 bytes, whichever is lower.”
    • NVS and Preferences limits: Page “Logical page corresponds to one physical sector of flash memory. … flash sector size is 4096 bytes”
    • NVS and Preferences limits: Entry “the entry size is 32 bytes … Header (32) … Entry state bitmap (32) … Entry 125 (32)”
    • NVS and Preferences limits: Integer (1…8 bytes) “For values of primitive types (currently integers from 1 to 8 bytes long), entry holds one key-value pair.”
    • NVS and Preferences limits: String “Operation consumes 1 overhead entry and 1 entry per each 32 characters of new string including zero character to be set.”
    • NVS and Preferences limits: Blob “Function uses 2 overhead and 1 entry per each 32 bytes of new data from the pool of available entries.”
    • NVS and Preferences limits: ESP-IDF: nvs_commit() is required “After setting any values, nvs_commit() must be called to ensure changes are written to non-volatile storage.”
    • NVS and Preferences limits: Updates append, they do not overwrite “When a value of any given key has to be updated, a new key-value pair is added at the end of the log and the old key-value pair is marked as erased. … effectively reduces the frequency of flash erase to flash write operations by a factor of 126”
    • NVS and Preferences limits: pitfall: A long string can fail with free space left “Note that storage of long string values can fail due to fragmentation of nvs pages even if available_entries returned by nvs_get_stats suggests enough overall space available. … If you need to store large blobs or strings, consider using the facilities provided by the FAT filesystem on top of the wear levelling library.”
    • NVS and Preferences limits: pitfall: A changed partition table leaves stale NVS “if an NVS partition is truncated (for example, when the partition table layout is changed), its contents should be erased. ESP-IDF build system provides a idf.py erase-flash target to erase all contents of the flash chip.”
  3. arduino-esp32 3.3.12: docs/en/api/preferences.rst primary
    What it confirms (7)
    • NVS and Preferences limits: in short “``true`` if the namespace was opened successfully; ``false`` otherwise. … if the call is successful; ``0`` otherwise. … ``defaultValue``, if it is provided; ``0`` otherwise.”
    • NVS and Preferences limits: Preferences Float / Double “keys storing values of type ``Float`` and ``Double`` use three entries in the key table.”
    • NVS and Preferences limits: begin(name, readOnly, partition) “``true`` if the namespace was opened successfully; ``false`` otherwise. … Namespace name. Maximum length is 15 characters. … If the namespace does not exist within the partition, it is first created. … namespace doesn’t exist yet and mode is NVS_READONLY … err = nvs_open(name, readOnly ? NVS_READONLY : NVS_READWRITE, &_handle);”
    • NVS and Preferences limits: putChar(), putUChar() “* ``1`` (the number of bytes stored for these data types) if the call is successful; ``0`` otherwise.”
    • NVS and Preferences limits: getChar() and other get…() “``defaultValue``, if it is provided; ``0`` otherwise. … Attempting to retrieve value from a non existent key will fail.”
    • NVS and Preferences limits: getString(key, buf, maxLen) “if the method fails: nothing is written to the buffer and the method returns ``0``. … ``maxLen`` must be greater than or equal to the number of bytes stored against the key (including the null terminator) or the call will fail.”
    • NVS and Preferences limits: getString(key, default) “if the method fails: it returns ``defaultValue``, if provided; ``""`` (an empty String) otherwise.”
  4. arduino-esp32 3.3.12: libraries/Preferences/src/Preferences.cpp primary
    What it confirms (4)
    • NVS and Preferences limits: putString() “if (!_started || !key || !value || _readOnly) { … log_e("nvs_set_str fail: %s %s", key, nvs_error(err)); … return strlen(value);”
    • NVS and Preferences limits: Preferences commits on every put “err = nvs_commit(_handle); … return putBytes(key, (void *)&value, sizeof(float_t)); … return putUChar(key, (uint8_t)(value ? 1 : 0)); … return putInt(key, value);”
    • NVS and Preferences limits: pitfall: Read-only begin() on a new namespace fails “err = nvs_open(name, readOnly ? NVS_READONLY : NVS_READWRITE, &_handle); … log_e("nvs_open failed: %s", nvs_error(err)); … #define ESP_ERR_NVS_NOT_FOUND … namespace doesn’t exist yet and mode is NVS_READONLY”
    • NVS and Preferences limits: pitfall: Failed get…() calls are quiet “log_v("nvs_get_i8 fail: %s %s", key, nvs_error(err)); … if (!_started || !key) { … if (_started) { … #if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_VERBOSE … #define ARDUHAL_LOG_LEVEL CORE_DEBUG_LEVEL … -DCORE_DEBUG_LEVEL={build.code_debug} … #define ARDUHAL_LOG_LEVEL_VERBOSE (5) … esp32s3.menu.DebugLevel.verbose.build.code_debug=5”