3.5.3 How Can I Reduce RAM Usage?
Use the smallest data types possible (this also reduces code size as
less code is needed to access these). Note that a bit type and non-standard 24-bit
integer type (__int24
and __uint24
) exists for this
compiler. See 5.3 Supported Data Types and Variables for all data types and sizes. There are two sizes of floating-point
type that are discussed in the same section as well.
Consider using auto variables over global or static variables as there is the potential that these can share memory allocated to other auto variables that are not active at the same time. Memory allocation of auto variables is made on a compiled stack (described in 5.4.2.2 Automatic Storage Duration Objects).
Rather than pass large objects to or from functions, pass pointers that reference these objects. This is particularly true when larger structures are being passed, but there might be RAM savings to be made even when passing long variables.
Objects that do not need to change throughout the program can be located
in program memory using the const
qualifier (see 5.3.8.1 Const Type Qualifier and 5.4.2 Objects in Data Memory). This frees up
precious RAM, but slows execution.
Ensure that all optimizations are enabled (see 4.6.6 Options for Controlling Optimization). Be aware of which optimizations the compiler performs (see 5.13 Optimizations), so that you can take advantage of them and don’t waste your time manually performing optimizations in C code that the compiler already handles.