Les String utilisent l'allocation dynamique.
L'allocation dynamique aboutit forcément à de la fragmentation mémoire.
Premièrement ce phénomène ralentit la recherche d'un nouveau bloc de mémoire libre.
Deuxièmement il peut aboutir à des erreurs d'allocation sur des blocs de taille importante.
En exécutant le code suivant, on constate bien que l'allocateur parvient à allouer au départ des blocs de plus de 100 octets, ensuite il plafonne à 89.
void setup() {
Serial.begin(115200);
}
String generateRandomString() {
String result;
int l, len;
len = l = random(10, 128);
while (len--) result += '?';
if (result.length() != l) {
Serial.print(result.length());
Serial.print(" / ");
Serial.print(l);
Serial.print(" : ");
Serial.println("ERREUR D'ALLOCATION");
}
return result;
}
String strings[10];
void loop() {
static float fragmentation, lastFragmentation;
for (String &s : strings) {
s = generateRandomString();
Serial.print(s.length());
Serial.print(" : ");
Serial.println(s);
}
delay(100);
}
61 : ?????????????????????????????????????????????????????????????
15 : ???????????????
105 : ?????????????????????????????????????????????????????????????????????????????????????????????????????????
100 : ????????????????????????????????????????????????????????????????????????????????????????????????????
20 : ????????????????????
..............
..............
115 : ???????????????????????????????????????????????????????????????????????????????????????????????????????????????????
24 : ????????????????????????
110 : ??????????????????????????????????????????????????????????????????????????????????????????????????????????????
43 : ???????????????????????????????????????????
57 : ?????????????????????????????????????????????????????????
42 : ??????????????????????????????????????????
50 : ??????????????????????????????????????????????????
36 : ????????????????????????????????????
88 / 92 : ERREUR D'ALLOCATION
88 : ????????????????????????????????????????????????????????????????????????????????????????
113 : ?????????????????????????????????????????????????????????????????????????????????????????????????????????????????
104 : ????????????????????????????????????????????????????????????????????????????????????????????????????????
58 : ??????????????????????????????????????????????????????????
75 : ???????????????????????????????????????????????????????????????????????????
95 : ???????????????????????????????????????????????????????????????????????????????????????????????
123 : ???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
54 : ??????????????????????????????????????????????????????
11 : ???????????
89 / 114 : ERREUR D'ALLOCATION
89 : ?????????????????????????????????????????????????????????????????????????????????????????
13 : ?????????????
89 / 97 : ERREUR D'ALLOCATION
89 : ?????????????????????????????????????????????????????????????????????????????????????????
24 : ????????????????????????
86 : ??????????????????????????????????????????????????????????????????????????????????????
89 / 105 : ERREUR D'ALLOCATION
89 : ?????????????????????????????????????????????????????????????????????????????????????????
57 : ?????????????????????????????????????????????????????????
33 : ?????????????????????????????????
89 / 111 : ERREUR D'ALLOCATION
89 : ?????????????????????????????????????????????????????????????????????????????????????????
89 / 125 : ERREUR D'ALLOCATION
89 : ?????????????????????????????????????????????????????????????????????????????????????????
88 : ????????????????????????????????????????????????????????????????????????????????????????
54 : ??????????????????????????????????????????????????????
89 / 103 : ERREUR D'ALLOCATION
@+