0
Offline
Newbie
Karma: 0
Posts: 14
Arduino rocks
|
 |
« on: January 15, 2011, 08:24:14 pm » |
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 :  So are there any advanced memory-saving tips?
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #1 on: January 15, 2011, 08:42:27 pm » |
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
Newbie
Karma: 0
Posts: 14
Arduino rocks
|
 |
« Reply #2 on: January 15, 2011, 10:41:19 pm » |
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
Offline
Shannon Member
Karma: 116
Posts: 10137
|
 |
« Reply #3 on: January 15, 2011, 11:31:22 pm » |
[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... [glow]extern [/glow]StupidClass foo; And define a single instance in one module (source file)... StupidClass foo;
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
God Member
Karma: 0
Posts: 592
|
 |
« Reply #4 on: January 15, 2011, 11:31:50 pm » |
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
|
I'm an electrical engineering student. I designed the USnooBie (V-USB dev kit) which is sold at Seeed Studio 
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 14
Arduino rocks
|
 |
« Reply #5 on: January 16, 2011, 11:20:24 am » |
Thank you both! I didn't know that.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 14
Arduino rocks
|
 |
« Reply #6 on: January 17, 2011, 11:09:07 am » |
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?
|
|
|
|
|
Logged
|
|
|
|
|
USA
Offline
Full Member
Karma: 2
Posts: 213
Arduino rocks
|
 |
« Reply #7 on: January 17, 2011, 11:27:53 am » |
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
Newbie
Karma: 0
Posts: 14
Arduino rocks
|
 |
« Reply #8 on: January 17, 2011, 12:06:32 pm » |
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 
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Dallas
Offline
Shannon Member
Karma: 116
Posts: 10137
|
 |
« Reply #9 on: January 17, 2011, 01:23:12 pm » |
|
|
|
|
|
Logged
|
|
|
|
|
SF Bay Area (USA)
Offline
Faraday Member
Karma: 78
Posts: 5453
Strongly opinionated, but not official!
|
 |
« Reply #10 on: January 18, 2011, 03:24:57 am » |
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
Newbie
Karma: 0
Posts: 14
Arduino rocks
|
 |
« Reply #11 on: January 18, 2011, 03:56:20 am » |
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
Edison Member
Karma: 19
Posts: 2307
|
 |
« Reply #12 on: January 18, 2011, 04:13:52 am » |
Program in Assembly. 
|
|
|
|
|
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).
|
|
|
|
|