0

I have an Arduino C++ program for ESP8266 that uses ESPAsyncWebServer to serve dynamic HTML pages. The static parts of HTML, CSS and JavaScript are stored in PROGMEM as string literals. The dynamic parts are supplied programmatically at runtime using the built in template engine. For example:

server.on("/index.html", HTTP_GET, [](AsyncWebServerRequest *request) {
  request->send_P(200, "text/html", index_html, processor);
});

String processor(const String &var) {
  if (var == "PLACE_HOLDER") {
    return String("this is the actual value for your place holder");
  }
  return String();
}

I understand the template engine used by ESPAsyncWebServer moves the content from PROGMEM to a String object in memory and performs String replacement operations. Each web page has several dynamic strings of length 100 to 200 bytes, whose value is known only at runtime. (For example, it scans for available WiFi networks and shows them on a web page. PROGMEM has only a place holder where the full list is inserted dynamically)

I am concerned that this will need frequent reallocation of memory to grow the string. Will this run the risk of heap fragmentation? If so, is there a work around?

4
  • The risk of fragmentation depends on many factors, the most prominent one being the available free ram space, the second being the sizes of the memory block being . allocated. Modern heaps do fairly well in house-keeping. Do you have another task running that allocates memory on the heap? How many concurrent connections are you expecting? Commented Jul 26 at 12:49
  • @MichaelRoy, I have 2 concurrent client connections that are permanent, plus one monitoring client app that runs about once in an hour. No other parts of my code allocates heap memory, but I am using a dozen libraries including ArduinoJson, FauxmoESP etc that may very well allocate on the heap. I am monitoring free heap on the serial console. It hovers around 10 to 12 KB on my ESP8266 Commented Aug 16 at 5:57
  • There's not much more you can do. You can try saturating your appliance with traffic to simulate the passage of time. If you are very concerned memory fragmentation might become an issue after a few weeks, or months of service, you can force a reboot when memory runs out, or send out an SOS email if an occasional reboot is not possible. Commented Aug 19 at 11:28
  • One thing to keep in mind: avoid using any of the memory allocated by the network tasks (strings, mostly) in the core control tasks. Then fragmentation should not be a problem, unless there is a memory leak in the network/text processing code you are using. Commented Aug 19 at 11:31

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.