All you know that microcontrollers have limited amount RAM and EEPROM. For instance ATMega128 has 4-Kbytes of RAM, 4-Kbyte of EEPROM. This may seem quite enough for some applications, but more complicated routines require more of RAM. And there is no compiler that can do better optimization on usage of ram than programmer.
The main aim of reaching this is reducing dependence on global data, stack and the heap.
So if your application running in microcontroller eats too much of RAM then you should develop some strategy of using its RAM.
Lets seeâ€¦ if you use constants as global values, why not to move them to EEPROM area? You can easily do this by compiler when you declare your variables by using const keyword:
const uint8_t a=10;
I compiled some code using this declaration and compiler (AVR-GCC) compiled .eep file which can be burned to microcontrollers EEPROM. In this way you can store your constants to your EEPROM memory.
Another factor eating microcontroller RAM is stacks. Try to use us much stack as your program require and leave some reserved space just in case. There are many techniques that can help define requirement of stack memory. One of them is to fill all stack with some specific data and after program execution check how much stack memory left untouched (never been overwritten). Different situation is with real time operation systems (RTOS) as there different task have its own stack. Here you can reduce number of tasks or use separate interrupt task to operate all interrupt service routines.
Using heaps in embedded systems is a pain, because heaps reduce performance increases code size. If it is possible try to use arrays instead.