Arduino Memoire Optimiser SRAM

De MCHobby - Wiki
Sauter à la navigation Sauter à la recherche


MCHobby investit du temps et de l'argent dans la réalisation de traduction et/ou documentation. C'est un travail long et fastidieux réalisé dans l'esprit Open-Source... donc gratuit et librement accessible.
SI vous aimez nos traductions et documentations ALORS aidez nous à en produire plus en achetant vos produits chez MCHobby.

Introduction

Cette partie du tutoriel s'attarde sur l'optimisation de la mémoire SRAM.

SRAM is the most precious memory commodity on the Arduino. Although SRAM shortages are probably the most common memory problems on the Arduino. They are also the hardest to diagnose. If your program is failing in an otherwise inexplicable fashion, the chances are good you have crashed the stack due to a SRAM shortage.

There are a number of things that you can do to reduce SRAM usage.

These are just a few guidelines here under to get you started.

Eliminer les variables inutilisées

If you are not sure whether a variable is being used or not, comment it out. If the sketch still compiles, get rid of it!

F() ces Strings!

(Stocker les char* dans le PROGMEM de l'architecture Harvard).
PROGMEM signifie "Mémoire Programme", la mémoire Flash allouée au stockage du programme

Le type String permet de stocker une chaine de caractère.

Literal strings are repeat memory offenders. First they take up space in the program image in Flash, then they are copied to SRAM at startup as static variables. This is a horrible waste of SRAM since we will never be writing to them.

Paul Stoffregen of PJRC and Teensyduino fame developed the F() macro as a super-simple solution to this problem. The F() macro tells the compiler to keep your strings in PROGMEM. All you have to do is to enclose the literal string in the F() macro.

For example, replacing this:

Serial.println("Sram sram sram sram. Lovely sram! Wonderful sram! Sram sra-a-a-a-a-am sram sra-a-a-a-a-am sram. Lovely sram! Lovely sram! Lovely sram! Lovely sram! Lovely sram! Sram sram sram sram!");
with this:
Serial.println(F("Sram sram sram sram. Lovely sram! Wonderful sram! Sram sra-a-a-a-a-am sram sra-a-a-a-a-am sram. Lovely sram! Lovely sram! Lovely sram! Lovely sram! Lovely sram! Sram sram sram sram!"));

Will save you 180 bytes of wonderful SRAM!

Réservez l'espace de vos Strings

The Arduino string library allows you to reserve buffer space for a string with the fonction reserve(). The idea is you can prevent String from fragmenting the heap by using reserve(num) to pre-allocate memory for a String that grows.

With the memory already allocated, String doesn't need to call realloc() if the string grows in length. In most usages, lots of other little String objects are used temporarily as you perform these operations, forcing the new string allocation to a new area of the heap and leaving a big hole where the previous one was (memory fragmentation). Usually all you need to do is use reserve() on any long-lived String objects that you know will be increasing in length as you process text.

You can do better with C strings, but if you just follow these guidelines for String objects, they work nearly as efficiently and using them is so much easier.

Placer vos données constantes en PROGMEM

Data items declared as PROGMEM do not get copied to SRAM at startup. They are a little less convenient to work with, but they can save significant amounts of SRAM. The basic Arduino reference for PROGMEM is here (Anglais, Arduino.CC). And there is a more detailed tutorial on the subject here (Anglais, AVRFreaks.Net).

Réduire la taille des Buffers

Allocation des Buffers et Array

If you allocate a buffer, make sure it is no bigger than it needs to be.

Buffers dans les librairies

Also be aware that some libraries allocate buffers behind the scenes that may be candidates for trimming as well.

Buffers bystèmes

Another buffer hidden deeply in the system is the 64 byte serial receive buffer. If your sketch is not receiving a lot of high-speed serial data, you can probably cut this buffer size in half - or maybe even less.

The Serial buffer size is defined in HardwareSerial.cpp. This file can be found in your Arduino install directory:

....\Arduino-1.x.x\hardware\arduino\cores\arduino\HardwareSerial.cpp

Look for the line:

#define SERIAL_BUFFER_SIZE 64

And change it to 32 or less.

Réduire les variables surdimensionnées

Don't use a float when an int will do. Don't use an int when a byte will do. Try to use the smallest data type capable of holding the information.

xxx

Pensez globalement. Allouez localement

Let's have another look at how SRAM is used (and abused):