Saving Memory on ESP32 using PlatformIO
How to save memory on ESP32, some tips
Started the 27th of October
In this article I will write the effective techniques I am using to save memory on ESP32. I will enumerate them when I am facing the problems.
1 – Modifying the partition table
So the memory of the ESP32 is by default divide like this : 20kb of nvs storage, 8kb of ota (over the air) configuration, 1280kb of app0, 1280kb of app1, 1408kb of SPIFFS and 64kb of coredump. (source)
I haven’t read about all the sections here but OTA is basically the capacity to update “Over the air” and SPIFFS is the file system that will store the web app. From this, I can make the statement that I won’t need OTA for Lesk. We can imagine that the user would need to dowload the last version of the system when needed and flash it directly.
Then, we can imagine that having 2 partition for the OTA is a bit useless. In that sense, what I did (after some research and help of chat GPT) was to modify the partition table by allocating different values to the portions previously described. That way, we can have both app0 and app1 storages rather than dividing it.
To do so, we need to have a .csv file in the same folder than the platfomio.ini file and we need to redefine the partition. Here is the new partition:
Also, we need to modify a build flag to take into account this partition :
I was at 97% of flash storage and I am now at less than 50%. This is a great improvement even if I haven’t had the opportunity to really optimize the code (I know I should do that but I am scared to break everything, so maybe later). This is for a next tip.
2 – Redefining the structures
(28th of October) I went upon a problem stating that I overflowed by 1000+ bytes of the dram0_0_seg which would mean that I have used too many static variables in the program. This is true, I know I have used way too many static statements in the program: For the effects, and for the variables. At one point this would need to be fixed by the help of more theory but for now, let’s do what we can. One step at the time.
The problem I am tackling now is the structure memory, beauty and curse of C or C++.
My first structure of effect was like this :
Meaning it would allocate memory for 11 settings, 11 settings Names, 11 minValues, 11 maxValues and 11 realNames. It’s easy to understand that this was too many empty spot when functions have in average 4/5 settings. Hence, I needed to find a way to tackle that using… pointers.
Here is the new structure that would point to the necessary number of effects/settings etc…
From the first trials, this worked as well, now the challenge is to adapt it to all the previous effect and see if we have the same error than before. I know that I will have to shorten the number of static variables but for now, let’s try this out… and it worked ! I managed to enter all the effects and I will push up until I get a new bug.
I know I will have to reduce the number of static variables I am using but for now, let’s stay here.
3 – Redefining the number of static variables
So, spoiler: this one doesn’t work. But here is some explanations.
I had around 70 static variables and a problem of dram0_0_seg overflowed by 500+ bytes. So what I did was to check the values that each variable was taking. I made some classes of variables (0-5, 0-10, 0-128, 0-255, 0-500, 0-1000, 0-3000) and I tried to simplified everything and…. This didn’t work. It has saved maybe 200 bytes. I have spent easily 2 hours on that but at least I know. Ok let’s move on.
4- Creating a class ColorObject
Creating a class ColorObject will help to do several things. First, it will store all the colors I potentially need for fast access in an object rather than in 3 different variables (which goes up to 9 variables for 3 colors). So 3 colors and eventually 3 palettes just to simplify things.
The goal of this color class is to refer to a single color object for every effect rather than defining a special one with variables.
It will as well allows to save some function as we can have repetitive effects where we use either the rainbow, either colors either palettes which made sense for the exploration when crafting the effect but not anymore when searching for saving power. Will it help saving memory? I think so, but let’s see…