6 years, 9 months ago.

Change thread stack size

Hello together,

I'm developing an application where a long textfile is transmitted over Ethernet to my mbed device.

The whole text is stored in my K64F for further operation. The problem is, that the (main) thread in combination with all buffers is so big that the Thread Stack is overflowing.

I thought it wouldn't ne any problem to just make the Thread Stack bigger, but I have no idea where and how to do that. I already changed MBED_CONF_LWIP_DEFAULT_THREAD_STACKSIZE and MBED_CONF_LWIP_PPP_THREAD_STACKSIZE in mbed_config.h OS_DYNAMIC_MEM_SIZE in RTX_config.h and MBED_CONF_APP_THREAD_STACK_SIZE in mbed_rtx_conf.h

All my modifications had no affect at all.

I'm triying for days and have no idea. I appreciate every help!

1 Answer

6 years, 9 months ago.

By far the easiest is to spin up a new thread with the stack size that you want. K64F should have plenty of available memory.

void other_thread() {
  // do large operations
}

int main() {
  Thread t(osPriorityNormal, 32 * 1024 /*32K stack size*/);
  t.start(&other_thread);

  wait(osWaitForever); // alt: let the main thread die, but make sure to move the thread to global scope in that case
}

Accepted Answer

Hello Jan,

thanks a lot for your quick reply! I did what you suggested and it works like a charm.

posted by Martin Matt 03 Aug 2017

This works if you want to increase your desired stack size... but if you want your stack size smaller than the default 4K (on LPC1768 anyway) this actually uses MORE memory.

If I do this:

void main_run() {
     // do main stuff here
}

int main() {
     Thread main_thread(osPriorityNormal, 3 * 1024); // 3K stack size
     main_thread.start(&main_run);
     wait(osWaitForever);
}

I show an additional 4K more RAM being used than if I simply do this:

int main() {
    // do main stuff here
}

I confirmed the memory usage by using the mbed_stats.h functions periodically while the program is running.

I'm using MBED OS 5.7

I cannot figure out a way to adjust the main thread stack size.. it always defaults to 4K no matter what I do.. except if I do the above... which will show that the main thread stack size is 3840 but.. the overall memory usage goes up by 4K for some reason and it never come back to being available... maybe when the main thread dies, the memory is actually not getting freed?

This is the contents of my mbed_app.json file, for reference:

{
    "macros": [
        "MBED_MEM_TRACING_ENABLED",
        "OS_MAINSTKSIZE=3840",
        "OS_PRIVSTKSIZE=3840",
        "STACK_SIZE=3840",
        "MBED_HEAP_STATS_ENABLED=1",
        "MBED_STACK_STATS_ENABLED=1"
    ]
    
}

As you can see I've tried several parameters and none of them are working. I have seen tickets about fixing this issue but interest fell away.. I guess there just isn't a big enough demand for memory adjustment at this level.

posted by Errol Burrow 07 Mar 2018

Move the thread declaration out of main, and then remove the `wait` call. This will let the main thread stop and the memory is reclaimed.

In addition you can override main stack size now in mbed_app.json, see https://os.mbed.com/docs/v5.7/reference/configuration.html

posted by Jan Jongboom 12 Mar 2018