Steps to reduce memory usage varies from one project to another. Here are general rules of thumb for getting more efficient code:
Use appropriate data types based on your requirements.
Use the smallest data types possible as less code is needed to access these. (This also reduces RAM usage.) For example, a short integer type exists for this compiler.
There are two sizes of floating-point type: 32-bit and 64-bit. Using –fno-short-double makes double equivalent to long double. Math operations on floating-point type will require significantly more instructions than similar operations on integers. Replace floating-point variables with integer variables wherever possible to reduce the program memory and data memory usage.
If you have used absolute addressing in your code, reduce the usage of these absolute addressing directives.
Without absolute addressing, the compiler may place more data into the small data section, which can be accessed via the efficient GP-register addressing mode. Reduce the size or number of function parameters, for instance rather than passing a structure by value pass a pointer to the same instead.
Use compiler optimizations for generating efficient and compact code from the C/C++ source.
Increase optimization level from -O0 to -O1 or higher. The free mode compiler supports -O0, -O1. The standard mode supports -O0, -O1, -O2, and PRO mode of compiler supports -O0, -O1, -O2, -O3, and -Os.
If you are building under MPLAB X IDE, go to the Project Properties window, click on the compiler name (xc32-gcc for C language projects or xc32-g++ for C++ language projects) and select the Optimization option category to set optimization levels.
Use compressed ISA mode such as MIPS16 or microMIPS.
MIPS16 mode of instruction for PIC32MX devices or microMIPS mode of instructions for PIC32MZ devices. You can also use mips16 or micromips function attributes to change the mode at the function level. The –mips16 and –mmicromips options are available only in PRO mode.
Use the max value for –G.
The -G option puts global and static items less than or equal to num bytes into the small data or bss section instead of the normal data or bss section. This allows the data to be accessed using a single instruction. All modules should be compiled with the same -G num value. In general, larger -G values result in more efficient code. Note that gp-relative addressing is limited to 64-KB of small data.