Any tips to keep memory usage as low as possible?

Hi,

Are there any tips or good practices for keeping the memory usage as low as possible?
I'm using 2 libraries plus my own classes right now and kinda hit the limit of the Unos ram ::slight_smile:

So are there any advanced memory-saving tips?

In general, keep arrays as small as possible. Store constant strings in PROGMEM to reduce SRAM usage.

To help you with your specific problem, we'd need to see your code.

Tanks very much.
I will try to use progmem and see if it works for my sketch!

But I discovered something else i hope anyone here can explain to me:

My Sketch has a rather large collection of classes i use to control LED Displays, IR Emitter,...
So i made an "System.h" header file where I store some options, as well as static objects from my classes (since I will only need one instance from each anyway)

It looks like this:

#ifndef SYSTEM_H
#define SYSTEM_H

#define OPTION 1
#define SOMEOTHEROPTION 2

static StupidClass foo;

#endif

The point of this was, that I am able to reach my options and objects from anywhere in my Code by including the System.h. And to modify my options in one single place.

The problem is, I discovered that including my Header file consumes about 100 bytes every time!

static StupidClass foo;

By making foo static, an instance is created for each module (source file). If you want only one foo (a singleton) declare it extern in the header file...

[glow]extern [/glow]StupidClass foo;

And define a single instance in one module (source file)...

StupidClass foo;

alex.c includes system.h
mike.c includes system.h

compile alex.c into alex.o, alex.o now contains one StupidClass
compile mike.c into mike.o, mike.o now contains one StupidClass

link alex.o and mike.o together, now you have two StupidClass, hence why the memory increased

the correct way is probably to use "extern" in system.h, and actually declare it in a source file (c, cpp, etc)

system.h has "extern StupidClass foo;"
josh.c has "static StupidClass foo;"
alex.c includes system.h, mike.c includes system.h

compile josh.c into josh.o, josh.o contains foo
compile alex.c into alex.o, alex.o knows that it's supposed to use a StupidClass called "foo", but doesn't have it
compile mike.c into mike.o, mike.o knows that it's supposed to use a StupidClass called "foo", but doesn't have it

link josh.o with alex.o and mike.o, now alex.o and mike.o knows where foo is

edit: coding badly beat me by 28 seconds

Thank you both! I didn't know that.

I tried using PROGMEM to store my strings, but I get "multiple definition of" error messages.

I've put prog_char SOUND_TEST_P[] PROGMEM = "Test.wav"; in my "System.h" header file wich is included in many other classes. I presume the later is causing the problem.

But how can I use PROGMEM to store my strings and still be able to use it globally in my Sketch?

you can use strcpy_P, to copy from FLASH to ram. I also wrote my on print functions that print directly from program memory.

//************************************************************************
void MYprint_P(prog_char *flashMemStr)
{
char theChar;
int ii;

ii = 0;
#if (FLASHEND > 0x10000)
while (theChar = pgm_read_byte_far(flashMemStr + ii++))
#else
while (theChar = pgm_read_byte_near(flashMemStr + ii++))
#endif
{
print(theChar);
}
}

Tanks, but i don't understand your code ;D

What I tried now:
I put

extern prog_char SOUND_P[];

in my often included System.h header file, and put
prog_char SOUND_P[] PROGMEM = "Sound.wav";

In one source file.

So it compiles fine, but when I call
char buffer[13];
strcpy_P(buffer, (char *)pgm_read_word(&SOUND_P));

My Arduino just restarts :frowning:

Give this a try...
http://arduiniana.org/libraries/flash/

In general, you should try to avoid putting commands that actually generate code (or data) in common .h files (for exactly the reasons that you are seeing.)
Put all your error messages in some .c or .cpp or .pde file, and put "extern" declarations in your .h files.

Tanks, I tried the lib Coding Badly suggested and it Works flawless!
Need to test out how much memory I saved this way, though.

Program in Assembly. :slight_smile: