How to calculate the stack size for a thread/worker_thread?

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Anonymous
Not applicable

Hi,

I'd like to know how to calculate the required stack size for a thread/worker_thread.

Also is there any method to know current memory usage? ( for debug memory leak )

BTW, the comment in http_server.c says /* printf requires 4K of stack */

is this true?

Doe that mean we should avoid using printf/sprintf in a thread/worker thread

if the stak size is less than 4K. (My test shows it seems ok to print someting even

the stack size is just 2K)

Thanks.

0 Likes
1 Reply
Anonymous
Not applicable

It depends on how deterministic your code is.  If you can exercise all of the paths and know that you have all paths covered, then you could set a pattern on a stack that is larger than you think that you need and after exercising, look at how much of the original pattern was not touched.  Beats calculating everything that may go on the stack by hand.

If your code can take many different paths based on various inputs and it is not deterministic, then things become much harder to add your limits... hope that you fit in the first category.

As for printf, it is a large user of memory.  If you are in an embedded piece of code, it is always best to avoid routines of this nature when you can.  I would guess that the 4K depends on the size of the output and the type of formatting that you are using.  As this is in a library, I am not sure that I would get a static view of what is used today and hope that this will never change at the risk of overflowing your stack.