Skip to main content
Associate II
Jun 6, 2024 4:24 PM UTC
Solved

Compiled code size and flashed code size are different in STM32CUBEIDE v1.15.1

Hi everyone, I created an HID bootloader for the STM32G4xxx, it works and now I am trying to optimize the size. After compiling I can read this on the Build Analyzer a flash memory usage of 17.85 kB.Screenshot 2024-06-06 181018.png 

After compiling I get this:

Screenshot 2024-06-06 180859.png

 

When I flash this compiled code and read the flash memory I canclearly see that the memory usage is around 38 kB.  

Screenshot 2024-06-06 180946.png

I also used the STM32CubeProgrammer to look at the actual memory (I didn't manage to use this function directly on STM32CubeIDE) and I can see that more than 38 kB are used in the memory

Can somebody explain why? Am I missing something about the Build Analyzer function?

 

    This topic has been closed for replies.
    Simo_SappoBest answer
    Associate II
    Aug 8, 2024 9:18 AM UTC

    I found the problem, somehow even if I set the Release mode as active (with the size optimization) it was still flashing the Debug version of the code which is not optimized for size. I discovered this only after deleting the Debug and Release folders and staring the flashing process again. Thank you everyone for your help

    14 replies

    gbm
    Super User
    Jun 6, 2024 7:46 PM UTC

    SREC file is a text file. It's size is a little over 2 times the programmed Flash footprint.

    My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice
    Associate II
    Jun 7, 2024 10:55 AM UTC

    Thank you very much, is there a way to reduce its size or to flash into memory a different file that woul dbe smaller?

    Andrew Neil
    Super User
    Jun 7, 2024 11:25 AM UTC

    What's programmed into Flash is more than just the code - there's also all the data initialisation values.

    The Build Analyser shows you exactly what's going into the Flash, and where:

    AndrewNeil_2-1717759397633.png

     

    AndrewNeil_3-1717759518617.png

     

    Everything in the 'Load address' column is stored in Flash

    .text is your executable code; some of the other things are mentioned here:

    https://stackoverflow.com/questions/15265295/understanding-the-libc-init-array 

     

     

    A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
    Associate II
    Jun 7, 2024 12:32 PM UTC

    My problem is that the code I am actually flashing in the microcontroller is 38.37 kB, which I confirmed by also opening the flash memory with STM32CubeProgrammer. I don't understand why I am flashing a file that is double the size of my actual code.


    Screenshot 2024-06-06 180946.png

    Andrew Neil
    Super User
    Jun 7, 2024 12:37 PM UTC

    But, as @gbm already said, that's the size of  the SREC file on your hard drive - not the size of the data which gets programmed into the Flash?

    A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
    Associate II
    Jun 7, 2024 12:44 PM UTC

    Yes, but as I said I looked in the flash memory with  the STM32 Programmer, and as you can also see I have code until 0x08009974, which is page 19 of the Flash memory. So the actual occupied memory size is more than 38 kB (each page is 2 kB in dual bank mode). 

    Screenshot 2024-06-07 144125.png

     

    Edit: I said single bank mode but I meant dual bank mode)

    Associate II
    Jun 7, 2024 12:56 PM UTC

    Nope, until 0x08009974 there are no gaps, no "FFFF" is written before the one you see in the picture. I checked

    Simo_SappoAuthorBest answer
    Associate II
    Aug 8, 2024 9:18 AM UTC

    I found the problem, somehow even if I set the Release mode as active (with the size optimization) it was still flashing the Debug version of the code which is not optimized for size. I discovered this only after deleting the Debug and Release folders and staring the flashing process again. Thank you everyone for your help

    Andrew Neil
    Super User
    Jul 7, 2025 5:02 PM UTC

    Switching between Release and Debug (or other) Configurations does not change the Debug Configuration.

    Same issue here:

    https://community.st.com/t5/stm32cubeide-mcus/build-analyzer-underestimates-by-30-flash-memory-usage/m-p/819739/highlight/true#M37233

     

     

    A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.