Page 1 of 1

Tips and tricks for saving RAM

Posted: Sat Jan 30, 2021 4:03 pm
by Oscar_T
Hi all

For a new project I am using a mcu (stm32f031) with a small amount of ram (4 KB).

Used to mcu with> = 32KB of ram, in my projects I create many local variables that are destroyed after the macro is terminated.To make the same macros work I had to transform local variables into global variables.

Are there some compiler settings to allow you to optimize the use of the ram, perhaps at the expense of general performance?

Re: Tips and tricks for saving RAM

Posted: Sat Jan 30, 2021 8:08 pm
by mnfisher
Hi Oscar,

The compiler settings just control the size of the code - the RAM use depends on the number of variables and also depth of procedure calls.
So the things you can do - use the smallest possible variable size for data types.
Use loops with your own variable as the index. Flowcode will generate a variable for any for loop (whereas you can reuse 'i'),
Be careful with recursion.
Limit the size of arrays and strings...
Minimize the number of arguments to macros (which also go on the stack) - arrays and strings also pass a size.


Personally, I'd still use locals - i in one macro effectively uses the same memory as i in another and reduces the risk of 'side effects' and hard to find bugs..

Can you offload any data to (i2c /spi) eeprom or sd card etc..

Martin

Re: Tips and tricks for saving RAM

Posted: Sun Jan 31, 2021 10:08 am
by Oscar_T
Hi Martin

many thanks for the explanation.

I'll try to optimize the code following your advice.

Oscar

Re: Tips and tricks for saving RAM

Posted: Sun Jan 31, 2021 5:59 pm
by mnfisher
Good luck.

The other thing to watch is components used - some (displays/sd for example) can use a fair bit of memory for buffers.

Martin