build file size optimization

Hi,

I have an application project using nCS with following prj.conf file:

#C++ support configs
CONFIG_CPLUSPLUS=y
CONFIG_NEWLIB_LIBC=y
CONFIG_LIB_CPLUSPLUS=y
CONFIG_HEAP_MEM_POOL_SIZE=32768
CONFIG_NEWLIB_LIBC_FLOAT_PRINTF=y
#CONFIG_SYS_CLOCK_TICKS_PER_SEC=1000

#Analyse threads
#CONFIG_THREAD_ANALYZER=y
#CONFIG_THREAD_NAME=y

# General configuration
CONFIG_FLASH=y
CONFIG_FLASH_MAP=y
CONFIG_NVS=y
CONFIG_SETTINGS=y
CONFIG_HWINFO=y
CONFIG_DK_LIBRARY=y

#log
CONFIG_LOG=y
CONFIG_PRINTK=y
CONFIG_LOG_DEFAULT_LEVEL=4
CONFIG_SIZE_OPTIMIZATIONS=y



CONFIG_SOC_FLASH_NRF_EMULATE_ONE_BYTE_WRITE_ACCESS=y

CONFIG_DATA_LOGGING=y
#board init 
CONFIG_BOARD_INIT=y

#Enable PWM
CONFIG_PWM=n

#enable ADC
CONFIG_ADC=n

#enable  SPI
CONFIG_SPI=y

#enable I2C
CONFIG_I2C=y

#Enable UART
CONFIG_UART=y
CONFIG_UART_HW_DMA=y

# Bluetooth configuration

#################### Bluetooth configuration
CONFIG_BT=y
CONFIG_BT_SMP=y
CONFIG_BT_DEVICE_NAME="test"
CONFIG_BT_L2CAP_TX_MTU=69
CONFIG_BT_L2CAP_TX_BUF_COUNT=8
CONFIG_BT_OBSERVER=y
CONFIG_BT_PERIPHERAL=y
CONFIG_BT_SETTINGS=y

# Disable unused Bluetooth features
CONFIG_BT_CTLR_DUP_FILTER_LEN=0
CONFIG_BT_CTLR_LE_ENC=n
CONFIG_BT_DATA_LEN_UPDATE=n
CONFIG_BT_PHY_UPDATE=n
CONFIG_BT_CTLR_CHAN_SEL_2=n
CONFIG_BT_CTLR_PRIVACY=n

################## Bluetooth mesh configuration
CONFIG_BT_MESH=y
CONFIG_BT_MESH_RELAY=y
CONFIG_BT_MESH_FRIEND=y
CONFIG_BT_MESH_ADV_BUF_COUNT=13
CONFIG_BT_MESH_RX_SEG_MAX=32
CONFIG_BT_MESH_TX_SEG_MAX=32
CONFIG_BT_MESH_PB_GATT=y
CONFIG_BT_MESH_GATT_PROXY=y
CONFIG_BT_MESH_DK_PROV=y

################ Bluetooth mesh models
CONFIG_BT_MESH_LIGHT_CTRL_SRV=y
CONFIG_BT_MESH_SENSOR_SRV=y
CONFIG_BT_MESH_ONOFF_SRV=y

The issue is I am using nrf52833dk and the flash is only 512kb, my build file .bin size is 440kb, which I cannot afford.

Request your suggestions on how to bring build .bin file size below 300kb.

Thanks,

  • Hi,

    Generally, to reduce the memory usage you should disable any unused features, optimize for size, and in some cases select other smaller libraries instead of bigger ounces (like the minimal libc). Which of these changes you can do in your project is application specific though), so I cannot make any specific recommendations as I do not know exactly what you use. There are some advice under Memory footprint optimization.

    Without knowing more, I would consider these, though you will need more changes than these:

    • Do you need to print floats? If not, you could disable CONFIG_NEWLIB_LIBC_FLOAT_PRINTF
    • Can you disable logging or reduce the log level?
    • The C++ related libraries (including newlib libc) adds quite a bit to the size, but may be difficult to get around
    • Make sure you are not making a debug build (for instance via nRF Connect for VS Code, which adds a few configurations to improved debugging (most importantly CONFIG_DEBUG_OPTIMIZATIONS).
  • Hello ,

    Thanks for the suggest I will try out.

    Make sure you are not making a debug build

    I am working with release build which is generating .bin of 400kb.

    The C++ related libraries (including newlib libc) adds quite a bit to the size, but may be difficult to get around

    1. Can we have no workarounds to reduce size here..?

    • Do you need to print floats? If not, you could disable CONFIG_NEWLIB_LIBC_FLOAT_PRINTF
    • Can you disable logging or reduce the log level?

    This surely I shall disable

    other smaller libraries instead of bigger ounces (like the minimal libc).

    2. From my attached prj.conf file, can you please suggest what all are the alternative smaller libraries please..?

    Thanks,

  • I cannot really be much more specific without knowing what you need and actually use. Disable everything you don't need, is basically the advice. And if that is not enough, check if you can also stop using some of the things you thought you needed.

    Ubaid_M said:
    2. From my attached prj.conf file, can you please suggest what all are the alternative smaller libraries please..?

    libc was the main thing that came to mind, but the newlib libc is a requirement for CONFIG_LIB_CPLUSPLUS (or rather the requirement is to not use a minimal libc), so if you need that, then maybe not.

    You could also attack this in a different way, by looking at the memory reports (if you use nRF Connect for VS Code, just select "Memory report" in the ACTIONS pane. With that you can see in a good level of detail the size of different components, and perhaps that will make it clearer where you should try to optimize.

Related