Chat freely about anything...

User avatar
By diraniyoussef
#77904 AFAIK the stack is 4 KB in size for ESP8266.
This API system_get_free_heap_size() is useful to get the free heap.
And this API system_print_meminfo() is informative in terms of data, rodata, bss, and heap.
But I need an API (or way) to get the available stack at runtime.
Any idea?

P.S: I'm using NodeMCU and use Arduino IDE
User avatar
By eriksl
#78129 There is none, unfortunately.

See this post for some information about it: http://bbs.espressif.com/viewtopic.php? ... ead#unread

In a NON-OS SDK I generally see this:

> stack bottom: 3ffffffc
> stack top: 3fffeb30
> value of initial stack pointer: 3ffffab0 (339 bytes)
> value of current stack pointer: 3ffffde0 (135 bytes)
> stack painted: 3968 bytes
> stack not painted: 1356 bytes
> stack size: 5324 bytes
> stack used: 2668 bytes
> stack free: 2656 bytes

So it seems there is some 5 kbytes to be used for stack. If the stack grows beyond that, the ESP will crash. Beyond that point there is another small area free to be used, that's not part of the heap (not confirmed by Espressif, but seems to work), 0x3fffe000 - 0x3fffeb2c, I am using it for a 2.5 kb log buffer.

So it seems like the stack area is quite big, it's not really something to take into consideration that much.
User avatar
By pidloop
#82835 I approximate stack usage as follows:

Code: Select all// initial stack
char *stack_start;

void setup()
{
    // init record of stack
    char stack;
    stack_start = &stack;

    // blah blah
}


Then any time I want to know the current stack size I call this:

Code: Select allvoid printStackSize()
{
    char stack;
    Serial.print (F("stack size "));
    Serial.println (stack_start - &stack);
}


Seems to be close. Size tends to be 1-2 kB, I make sure it never gets anywhere the max of 4 kB.

Hope this helps.
User avatar
By quackmore
#82839 @pidloop

I check the stack boundaries using the same method you have posted about

for debug and log purposes I check them inside every function call, keeping track of the highest and lowest value

so far I found a nasty bug and subsequent stack overflow into the heap (due to interrupt mismanagement) with that method