Pages: [1]   Go Down
Author Topic: Any tips to keep memory usage as low as possible?  (Read 4145 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 14
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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  :smiley

So are there any advanced memory-saving tips?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49365
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Newbie
*
Karma: 0
Posts: 14
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

Global Moderator
Dallas
Online Online
Shannon Member
*****
Karma: 207
Posts: 12921
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
[glow]static [/glow]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...

Code:
[glow]extern [/glow]StupidClass foo;

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

Code:
StupidClass foo;
Logged

0
Offline Offline
God Member
*****
Karma: 0
Posts: 593
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
« Last Edit: January 15, 2011, 11:34:46 pm by frank26080115 » Logged

Freelance engineer, consultant, contractor. Graduated from UW in 2013.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 14
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you both! I didn't know that.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 14
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

I've put
Code:
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?
Logged

USA
Offline Offline
Full Member
***
Karma: 2
Posts: 213
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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);
      }
}
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 14
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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  smiley-sad
Logged

Global Moderator
Dallas
Online Online
Shannon Member
*****
Karma: 207
Posts: 12921
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


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

SF Bay Area (USA)
Offline Offline
Tesla Member
***
Karma: 133
Posts: 6755
Strongly opinionated, but not official!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Newbie
*
Karma: 0
Posts: 14
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

'round the world...
Offline Offline
Faraday Member
**
Karma: 42
Posts: 3262
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Program in Assembly. smiley
Logged

Eu não sou o teu criado. Se respondo no fórum é para ajudar todos mediante a minha disponibilidade e disposição. Responder por mensagem pessoal iria contra o propósito do fórum e por isso evito-o.
Se realmente pretendes que eu te ajude por mensagem pessoal, então podemos chegar a um acordo e contrato onde me pagas pela ajuda que eu fornecer e poderás então definir os termos de confidencialidade do meu serviço. De forma contrária toda e qualquer ajuda que eu der tem de ser visível a todos os participantes do fórum (será boa ideia, veres o significado da palavra fórum).
Nota também que eu não me responsabilizo por parvoíces escritas neste espaço pelo que se vais seguir algo dito por mim, entende que o farás por tua conta e risco.

Dito isto, mensagens pessoais só se forem pessoais, ou seja, se já interagimos de alguma forma no passado ou se me pretendes convidar para uma churrascada com cerveja (paga por ti, obviamente).

Pages: [1]   Go Up
Jump to: