Reducing Memory Usage
In some cases, RAM rather than ROM is the limiting factor for your application. In these cases, you’ll want to reduce your dependence on global data, the stack, and the heap. These are all optimizations better made by the programmer than by the compiler.
Because ROM is usually cheaper than RAM (on a per-byte basis), one acceptable strategy for
reducing the amount of global data might be to move constant data into
ROM. This can be done automatically by the compiler if you declare all
of your constant data with the keyword const. Most C compilers place all of the
constant global data they encounter into a special data segment that is
recognizable to the locator as ROM-able. This technique is most valuable
if there are lots of strings or table-oriented data that will not change
at runtime.
If some of the data is fixed once the program is running but not necessarily constant, the constant data segment could be placed in a hybrid memory device such as flash or EEPROM. This memory device could then be updated over a network or by a technician assigned to make the change. An example of such data is the sales tax rate for each locale in which your product will be deployed. If a tax rate changes, the memory device can be updated, but additional RAM can be saved in the meantime.
Stack size reductions can also lower your program’s RAM requirement. One way to figure out approximately how much stack you need is to fill the entire memory area reserved for the stack with a ...