I am not 100% sure why it freed up so much RAM, but YAY!
Code:
teensy_size: Memory Usage on Teensy 4.1:
teensy_size: FLASH: code:372928, data:55916, headers:9040 free for files:7688580
teensy_size: RAM1: variables:74208, code:243152, padding:18992 free for local variables:187936
teensy_size: RAM2: variables:47616 free for malloc/new:476672
I have a 15 byte big struct, that contains the intervals and a name of chords, I have 27 of them, I moved them from being const and directly initialized to static and being initialized in a method.
Old:
Code:
#define NUM_CHORDS 27
struct Chord {
char chord_name[8];
int8_t chord_intervals[NUM_CHORDS_INTERVALS];
Chord( const __FlashStringHelper* name, int8_t i1, int8_t i2, int8_t i3, int8_t i4, int8_t i5, int8_t i6, int8_t i7) : chord_intervals{i1, i2, i3, i4, i5, i6, i7} {memset( chord_name, 0, 8); strncpy( chord_name, (const char*)name, 7);}
};
const Chord MODIFIER_CHORDS[NUM_CHORDS] = {
{F("-"), 0, 100, 100, 100, 100, 100, 100},
{F("maj"), 0, 4, 7, 100, 100, 100, 100},
...
};
I already tried to make it smaller by passing FlashLocated Strings into the constructor, but if I remember correctly, it didn't help...
I changed it to:
Code:
#define NUM_CHORDS 27
struct Chord {
char chord_name[8];
int8_t chord_intervals[NUM_CHORDS_INTERVALS];
Chord( const __FlashStringHelper* name, int8_t i1, int8_t i2, int8_t i3, int8_t i4, int8_t i5, int8_t i6, int8_t i7) : chord_intervals{i1, i2, i3, i4, i5, i6, i7} {memset( chord_name, 0, 8); strncpy( chord_name, (const char*)name, 7);}
};
static Chord MODIFIER_CHORDS[NUM_CHORDS];
void prepareGlobalData() {
MODIFIER_CHORDS[0] = {F("-"), 0, 100, 100, 100, 100, 100, 100};
MODIFIER_CHORDS[1] = {F("maj"), 0, 4, 7, 100, 100, 100, 100};
...
MODIFIER_CHORDS[26] = {F("add11"), 0, 4, 7, 17, 100, 100, 100};
}
I call this method from my setup(). Memory usage moved from
RAM1: variables:130144, code:336144, padding:24304 free for local variables:33696
to
teensy_size: RAM1: variables:114592, code:243152, padding:18992 free for local variables:147552
Thats ~15kbyte variables and ~90kbyte code. Why? 27*(7+8) = 405bytes
I See, I have a hell lot to learn :P
By moving the Array to DMAMEM and the prepareGlobalData to FLASHMEM I ended with:
RAM1: variables:74208, code:243152, padding:18992 free for local variables:187936