-->
Page 1 of 2

Any tips on troubleshooting heap problems?

PostPosted: Mon Feb 01, 2016 11:12 pm
by marklein
I'm new to C programming so managing memory is not something I do second nature. I'm using a NodeMCU v1 board (4M).

I've created a web server where I'm storing a fair amount of data in EEPROM. Starting it using EEPROM(1024) (is that kosher? I can just pick any random number under 4096?). First phase went well, so I added another block of data to the mix and now it QUICKLY runs out of heap just trying to serve up the index page once.

So including stupid newbie mistakes, what are some tips for managing memory on this guy so I can get back on track? Am I addressing EEPROM in an allowed manner? THX

Re: Any tips on troubleshooting heap problems?

PostPosted: Tue Feb 02, 2016 1:18 am
by WereCatf
marklein wrote:I've created a web server where I'm storing a fair amount of data in EEPROM. Starting it using EEPROM(1024) (is that kosher? I can just pick any random number under 4096?). First phase went well, so I added another block of data to the mix and now it QUICKLY runs out of heap just trying to serve up the index page once.

So including stupid newbie mistakes, what are some tips for managing memory on this guy so I can get back on track? Am I addressing EEPROM in an allowed manner? THX


Yes, you can use up to EEPROM.begin(4096), but you're saving your index-page in there? Why not use SPIFFS? The EEPROM is only 4 KB, whereas SPIFFS is 1MB or 3MB, depending on what you've chosen in the IDE, so you could store a lot larger files there and/or more than just the index-page.

As for your problem with running out of memory: using EEPROM in and of itself shouldn't cause you to run out of memory, you are doing something else that is causing it to happen. Without seeing your code there's nothing more I can say.

Re: Any tips on troubleshooting heap problems?

PostPosted: Tue Feb 02, 2016 11:44 am
by marklein
Sorry, no. The web pages are just being stored as character constants in "normal" memory (whatever that would be). EEPROM is being used for user data like password, IP settings. It was running great with that. Then I added a list of 20 new Strings to EEPROM, each 32 bytes, and that's when things turned unusable.

I'd post my code but it's around 1400 lines right now (probably half being html) and I don't think it makes good bathroom reading, if you know what I mean. ;)

Compiling the current code reports "Sketch uses 271,778 bytes (26%) of program storage space. Maximum is 1,044,464 bytes.
Global variables use 51,748 bytes (63%) of dynamic memory, leaving 30,172 bytes for local variables. Maximum is 81,920 bytes."

Heap is around 20,000 at boot, but serving just one mostly static page fails and if I can catch the heap before it panics it will be around 5000. Maybe it's not really a heap problem, but that's just a symptom of a greater issue?

I'm hoping that there might be some "best practices" type advise for managing memory. For example, use XX for this sort of thing and use YY for this other sort of thing. What the heck PROGMEM is good for. Newbie mistakes... stuff like that? I've never programmed anything as low level as this before, my experience is in web languages like PHP, easy stuff.

Thanks.

Re: Any tips on troubleshooting heap problems?

PostPosted: Tue Feb 02, 2016 1:47 pm
by WereCatf
marklein wrote:Heap is around 20,000 at boot, but serving just one mostly static page fails and if I can catch the heap before it panics it will be around 5000. Maybe it's not really a heap problem, but that's just a symptom of a greater issue?

I'm hoping that there might be some "best practices" type advise for managing memory. For example, use XX for this sort of thing and use YY for this other sort of thing. What the heck PROGMEM is good for. Newbie mistakes... stuff like that? I've never programmed anything as low level as this before, my experience is in web languages like PHP, easy stuff.


Yes, manual memory-management is quite a different beast to get used to, but.. well, there is no "best practices" type of thing other than "always remember to free any memory you allocate." PROGMEM is used for storing constant variable that do not change in a way that doesn't take up heap -- when a variable is defined as PROGMEM you cannot change its contents.

Still, it sounds like you are allocating a lot of memory when you're serving the page, but you're not freeing the memory afterwards -- a very common rookie-mistake. You're most likely also allocating much more than you'd actually need as that is also another common rookie-mistake. Without seeing the code I just do not know how else I can help you :/