Tips and tricks for saving RAM

For general Flowcode discussion that does not belong in the other sections.
Post Reply
Oscar_T
Posts: 89
http://meble-kuchenne.info.pl
Joined: Wed Dec 02, 2020 10:06 am
Location: Italy
Has thanked: 37 times
Been thanked: 10 times

Tips and tricks for saving RAM

Post 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?

mnfisher
Valued Contributor
Posts: 1628
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 142 times
Been thanked: 761 times

Re: Tips and tricks for saving RAM

Post 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

Oscar_T
Posts: 89
Joined: Wed Dec 02, 2020 10:06 am
Location: Italy
Has thanked: 37 times
Been thanked: 10 times

Re: Tips and tricks for saving RAM

Post by Oscar_T »

Hi Martin

many thanks for the explanation.

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

Oscar

mnfisher
Valued Contributor
Posts: 1628
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 142 times
Been thanked: 761 times

Re: Tips and tricks for saving RAM

Post 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

Post Reply