I'm writing a code about play gif from SDCard on TFT Screen, so I create a array to put the gif file. (Using Nodemcu-32s 4MB)
#include<TFT_eSPI.h>
#include<SPI.h>
#include<AnimatedGIF.h>
TFT_eSPI tft;
AnimatedGIF gif;
uint8_t *gifArray;
int gifArrayLen;
void setup(){
tft.init();
tft.setRotation(2);
tft.fillScreen(TFT_BLACK);
File fgif=SD.open("/test.gif",FILE_READ);
gifArrayLen=fgif.size();
gifArray=(uint8_t *)malloc(gifArrayLen);
for(int i=0;i<gifArrayLen;i ) gifArray[i]=fgif.read();
fgif.close();
}
void loop(){
tft.startWrite();
gif.open(gifArray,gifArrayLen*sizeof(uint8_t),GIFDraw);
while(gif.playFrame(true,NULL)) yield();
gif.close();
tft.endWrite();
}
But if gif size > 131KB, it will trigger fatal error like this.
Guru Meditation Error: Core 1 panic'ed (StoreProhibited). Exception was unhandled.
After malloc the array, when I set a value on it, it triggered.
I found some forum says it's because it over the FreeRTOS heap size.
Can I extend the heap size or use another storage method to replace it?
CodePudding user response:
The heap is the pool of memory available to your program to allocate storage.
You're using an ESP32, which is a small CPU that has a small amount of RAM, roughly 400KB.
You cannot extend the heap on the ESP32. Unlike Linux which has virtual memory, there's nothing to extend it with.
However, you can add additional storage in the form of external PSRAM - additional RAM that is accessed via SPI. PSRAM will be slower than internal RAM and will have some restrictions on its use (for instance, call stacks cannot live in PSRAM and certain DMA buffers cannot be located there).
The ESP32's manufacturer, Espressif, has documented how to use PSRAM with the ESP32.
You'll generally (maybe exclusively) find PSRAM available built onto ESP32 boards, so if you're using an ESP32 module that doesn't already have it you'll need a new ESP32 module that does.
And as other people have pointed out, you can artificially cause less memory to be available in the heap by over-allocating and freeing memory, fragmenting the heap so that there are no large pieces still available. This is called heap fragmentation and has been written about extensively here on Stack Overflow and other web sites.
CodePudding user response:
The "4MB" in NodeMCU refers to the size of flash, the size of RAM on ESP32 is fixed at 512KB, roughly 200KB of which is used by IRAM cache/code sections, leaving around 320KB for program memory, half of which is available for dynamic allocation.
From documentation Heap Memory - Available Heap:
Due to a technical limitation, the maximum statically allocated DRAM usage is 160KB. The remaining 160KB (for a total of 320KB of DRAM) can only be allocated at runtime as heap.
There is a way to connect more RAM via SPI, but it's going to be very slow. You might want to look into a larger SoC with more RAM instead.