Running out of memory on Orangutan SV-328

I am running out of memory on the Orangutan SV-328 and need to add a little more code to complete the project. I probably only need to save 1 or 2% to be able to finish up. By the way, I used all the default compiler settings for AVRStudio 4 - so the optimization setting is -Os.

When I compile I get the following message:

Device: atmega328p

Program: 32746 bytes (99.9% Full)
(.text + .data + .bootloader)

Data: 1683 bytes (82.2% Full)
(.data + .bss + .noinit)

EEPROM: 936 bytes (91.4% Full)

Build succeeded with 0 Warnings…

I found an article on line that mentioned that uninitialized variables go into .bss and the vast majority of the variables I use are global arrays of uninitialized structures - so that should not cause program memory to fill up.

I have done several things to save program memory like: put static strings in EEPROM and reading them as needed, using a single file wide static char array variable for use in place of char array variables declared in local functions. I have removed all the unused code I can find and tried to use the fewest variables and the simplest approach to accomplishing a given task in the code.

The Pololu AVR library reference mentions that printf() for the LCD uses memory but I am not sure what to do about it. A lot of my code is related to running a necessarily simple-minded yet extensive user interface to configure the project. The resulting configuration info is saved in the global arrays mentioned above and in EEPROM for future use. Naturally, the menu uses printf() a lot.

Can you offer any suggestions?

Hello, Burke.

You should get the linker to produce a map file so you can get an idea of what is taking up the space in your flash. It would list the addresses of every symbol in flash. You might find some functions that are taking up way more space than you would expect. I forget if AVR Studio 4 produces one by default but you should be able to enable it. Feel free to post your some of your source code here and I could scan it for things that are inefficient.

You should use printf_P(PSTR(“abcd”)) instead of prtinf(“abcd”) to save 5 bytes of RAM.

Are you using a lot of pointers? It saves code space if the compiler knows addresses at compile time instead of having to dereference pointers.

Are you doing floating point arithmetic? Try to use integers instead.

Always use the smallest possible integer:

char = 1 byte
int = 2 bytes
long = 4 bytes


Just replacing int with uint8_t or int8_t ended up saving about 9%. I do relatively few floating point calculations so I did not try using integer math - besides I need the accuracy of floating point values when I do use them.

I tried using printf_P(PSTR…) but it actually used more memory.

But the 9% will make all the difference in my project - way more than I ever expected.

Thank you very much!