(XC32) How can I reduce memory usage?

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 requirement.

Use the smallest data types possible as less code is needed to access these. (This also reduces RAM usage.) For examples, a short integer type exists for this compiler.

There are two sizes of floating-point type, 32bit 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.

© 2017 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.