How to make tasks and determine stack size in freeRTOS?

Not speaking for him, but I read his words as

optimum stack size = (**the original stack size** - **highwatermakrk**) + **2000 bytes**

I'll give that a try. Cheers

Actually, just highwatermark+2000

You got it.

Topic reopened at the request of @Perehama

How many moderators does it take to open a topic ? :grinning:

Just in case there is any confusion, the high water mark returned by uxTaskGetStackHighWaterMark() is the lowest amount of free or unused space on the stack during the execution of a task.

This is taken from the IDE ESP32 example sketch ArduinoStackSize.ino :

// Print unused stack for the task that is running setup()
   Serial.printf("\nSetup() - Free Stack Space: %d", uxTaskGetStackHighWaterMark(NULL));

Did I? I don't think I did intentionally, or perhaps the wrong name was attributed?

You didn't, it was @6v6gt , I think the misattribution arose because 6v6gt flagged one of your replies in order to request for the topic to be opened.

So if the stack-space for the task is set at 10000, and the return value is 7200, that means the actual Stack space used (until that moment) is 2800.
Does the function keep track of the lowest value it self or does the programmer have to do this ?

The function returns the smallest available (free) stack space observed up until that moment. That means from the time the task was spooled up until the function was called. Hence the term "watermark" as in the highest water level observed on shores, riverbanks, or structures near water bodies.

So in that sense, the OS maintains the lowest value and returns it every time the function is called.

In addition to the "how much until it overflows" analogy, there's the eponymous stack of plates: last in, first out. Easy enough to test

#include <freertos/task.h>

void setup() {
  Serial.begin(115200);
  Serial.println("Hello, ESP32!");
}

unsigned fact(unsigned n) {
  if (n <= 1) {
    return 1;
  }
  n *= fact(n - 1);
  delay(10);  // defeat tail call optimization
  return n;
}

void loop() {
  static unsigned n = 2;
  auto f = fact(n);
  Serial.print(n);
  Serial.print("! = ");
  Serial.println(f);
  Serial.println(uxTaskGetStackHighWaterMark(nullptr), HEX);
  ++n;
}

prints (on Wokwi)

Hello, ESP32!
2! = 2
1C4C
3! = 6
1C4C
4! = 24
1C4C
5! = 120
1C4C
6! = 720
1C4C
7! = 5040
1C4C
8! = 40320
1C4C
9! = 362880
1C4C
10! = 3628800
1C4C
11! = 39916800
1C3C
12! = 479001600
1C1C
13! = 1932053504
1BFC
14! = 1278945280
1BDC
15! = 2004310016
1BBC
16! = 2004189184
1B9C
17! = 4006445056
1B7C
18! = 3396534272
1B5C
19! = 109641728
1B3C
20! = 2192834560
1B1C
21! = 3099852800
1AFC
22! = 3772252160
1ADC
23! = 862453760
1ABC
24! = 3519021056
1A9C
25! = 2076180480
1A7C
26! = 2441084928
1A5C
27! = 1484783616
1A3C
28! = 2919235584
1A1C
29! = 3053453312
19FC
30! = 1409286144
19DC
31! = 738197504
19BC
32! = 2147483648
199C
33! = 2147483648
197C
34! = 0
195C
35! = 0
193C

The HWM does not change until after 10!. This means that prior to the loop, the main task had made calls that deep. After that, further nested calls to the fact function "raise" the HighWaterMark, edging closer and closer to exceeding the available stack space. (The results go wrong and become consistently zero before then; exercise left for the reader.)