Memory footprint optimization

When developing an application, ROM and RAM footprint are important factors, especially when the firmware runs on the most resource-constrained devices like nRF52810 or nRF52811.

General recommendations

To reduce the memory footprint, ensure that your application uses the minimum required resources and tune the nRF Connect SDK configuration parameters. Complete the following actions to optimize your application:

  • Follow the guides for optimizing Zephyr. Also see the implementation of the Minimal footprint sample.

  • Analyze stack usage in each thread of your application by using the Thread analyzer. Reduce the stack sizes where possible.

  • Limit or disable debugging features, such as logging or asserts.

  • Go through each component and subsystem and turn off all peripherals and features that your application does not use.

The following subsections give more information on how to optimize specific subsystems.

Bluetooth

Besides applying General recommendations, you can also complete the following actions to optimize the Bluetooth® part of your application:

For reference, you can find minimal footprint configurations of the Bluetooth: Peripheral LBS sample in nrf/samples/bluetooth/peripheral_lbs/minimal.conf and the Bluetooth: Peripheral UART sample in nrf/samples/bluetooth/peripheral_uart/minimal.conf.

Bluetooth mesh

Besides applying General recommendations and Bluetooth optimization actions, there are some configuration options you can use to optimize the Bluetooth mesh part of your application. Changing any of these options will change the functional capabilities of the Bluetooth mesh device, and thereby result in changes to RAM and flash memory footprint.

Changing the values of the following options will affect the RAM footprint and the amount of space needed for persistent storage of the associated configuration data:

  • General node configuration:

    • BT_MESH_MODEL_KEY_COUNT

    • BT_MESH_MODEL_GROUP_COUNT

    • BT_MESH_SUBNET_COUNT

    • BT_MESH_APP_KEY_COUNT

    • BT_MESH_LABEL_COUNT

    • BT_MESH_CRPL

  • For Provisioner device, the following configuration database (CDB) options are available (like how many nodes it can provision, or maximum number of supported application keys):

    • BT_MESH_CDB_NODE_COUNT

    • BT_MESH_CDB_SUBNET_COUNT

    • BT_MESH_CDB_APP_KEY_COUNT

Changing the values of the following options will only affect the RAM footprint:

  • Configuration options for segmented messages (for example how many segmented messages a node can send or receive, and the number of segments per message):

    • BT_MESH_TX_SEG_MSG_COUNT

    • BT_MESH_RX_SEG_MSG_COUNT

    • BT_MESH_SEG_BUFS

    • BT_MESH_RX_SEG_MAX

    • BT_MESH_TX_SEG_MAX

  • Advertiser configuration:

    • BT_MESH_ADV_BUF_COUNT

  • Extended advertising configuration:

    • BT_MESH_ADV_EXT

    • BT_MESH_RELAY_ADV_SETS

    • BT_MESH_ADV_EXT_GATT_SEPARATE

  • Legacy advertising configuration:

    • BT_MESH_ADV_STACK_SIZE

  • If Friend feature is enabled, the following configuration options are relevant:

    • BT_MESH_FRIEND_QUEUE_SIZE

    • BT_MESH_FRIEND_SUB_LIST_SIZE

    • BT_MESH_FRIEND_LPN_COUNT

    • BT_MESH_FRIEND_SEG_RX

  • If Low Power Node (LPN) feature is enabled, the following configuration option is relevant:

    • BT_MESH_LPN_GROUPS

  • If the proxy server is enabled (option BT_MESH_GATT_PROXY), pay attention to the proxy server filter size:

    • BT_MESH_PROXY_FILTER_SIZE

  • Other device configuration:

    • BT_MESH_LOOPBACK_BUFS

    • BT_MESH_MSG_CACHE_SIZE

Model configuration options that affect stack size at runtime:

  • BT_MESH_SENSOR_SRV_SENSORS_MAX

  • BT_MESH_SENSOR_SRV_SETTINGS_MAX

  • BT_MESH_SCENES_MAX

  • BT_MESH_PROP_MAXSIZE

  • BT_MESH_PROP_MAXCOUNT

Gazell

To optimize the Gazell memory footprint, follow the General recommendations. Specifically, study the ISR stack size. If your application is in a pairing device, pay attention to the system workqueue stack size.

Do not enable features that your application does not use. The configuration options default to disabling optional features, such as:

  • Pairing

  • Pairing encryption

  • Pairing settings persistent storage

To reduce the logging level, set the CONFIG_GAZELL_LOG_LEVEL_CHOICE Kconfig option.

Matter

Besides applying General recommendations, you can also complete the following actions to optimize the Matter part of your application:

  • Make sure Zephyr’s Shell is disabled for your application. Related configuration options are listed in a dedicated section in each Matter sample’s prj.conf file.

  • Use prj_release.conf for building the application. The release configuration has a smaller memory footprint than the default, debug-enabled prj.conf.

  • If the logs in your application do not use the default log level, you can change the default log level of Zephyr modules from info to warning by setting CONFIG_LOG_DEFAULT_LEVEL to 2.

  • Change the log level of the Matter logs from debug to info by setting CONFIG_MATTER_LOG_LEVEL_INFO to y.

  • Reduce the verbosity of assert messages by setting CONFIG_ASSERT_VERBOSE to n.

  • Check Thread memory footprint optimization actions, as the Matter application layer uses OpenThread.

NFC

The Near Field Communication (NFC) protocol implementation in the nRF Connect SDK provides some options for optimizing memory footprint for both the tag and the poller roles.

Tag

To optimize your application that supports the NFC tag functionality, follow the General recommendations. The NFC Type 2 Tag and Type 4 Tag libraries do not provide configuration options that have an effect on memory usage in an application. However, there are a few Kconfig configuration options you can use to optimize memory usage related to NFC.

For an application that uses the Type 4 Tag library, you can set the following options:

Poller

To optimize an application that supports the NFC poller functionality using the NFC Reader ST25R3911B library, you can set the following options:

If the application supports the Tag NDEF Exchange Protocol (TNEP), you can set the following options:

If the application uses the NFC TNEP protocol for the Bluetooth LE out-of-band pairing procedure (NFC TNEP Connection Handover), regardless of the role (tag or poller), you can set the following options:

Thread

The current Thread memory requirements are listed on the OpenThread memory requirements page.

Besides applying General recommendations, you can also complete the following actions to optimize the Thread part of your application:

Zigbee

The current Zigbee memory requirements are listed on the Zigbee memory requirements page.

Apply General recommendations to optimize the Zigbee part of your application.