Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
7 years, 8 months ago.
operator new[] out of memory
Hello,
I'm pretty new to mbed OS, so I ask you for patience.
When I dynamically allocate memory, it will throw an error : " Operator new[] out of memory "
My microcontroller has 512 KB RAM. Compiler output statistic shows 85016 bytes of RAM usage. I know that there is no dynamically allocated memory counted, but I don't believe that the rest of memory is used by new operator. Is there any way how to increase HEAP memory?
I tried to use runtime statistics according to this instructions https://docs.mbed.com/docs/mbed-os-handbook/en/5.2/advanced/runtime_stats/ but I'm still getting zeroes. That's probably caused because I appended -DMBED_HEAP_STATS_ENABLED=1 to "mbed compile" command. I know that's probably wrong but I couldn't figure anything else from that instructions.
Thank you!
3 Answers
6 years, 9 months ago.
Same Problem here!
It compiles and runs without a problem in the online compiler. But after exporting to GNU Arm Embedded Toolchain and compiling there, nothing works anymore after uploading the binary to the target, except the message in the serial terminal:
Operator new[] out of memory
We used the latest mbed OS5 (online compiler) and GNU Arm Embedded Toolchain Version 7-2017-q4-major Linux 64-bit
Frustrating like so many thing with arm mbed!
Aren't you simply running out of memory? The Keil compiler, which is also used in the online compiler, is quite efficient.
I don't thing it works for GNU, and I don't know if it works for OS5, and I also don't know if it is still relevant for OS2. So for everyone who tries it, use at your own risk. But in the past I know the compiler used an excessive margin between the heap and stack, which caused it to fail new assignments significantly before the memory was actually exhausted. Adding:
#pragma import __use_two_region_memory
disabled the check completely. So no more issues where it would run out of memory because of the safety margin it was taking, but also no warning that you run out of memory besides it completely crashing.
posted by 07 Feb 20187 years, 3 months ago.
Hi Juraj,
is your problem still existent? I have kind of the same error. Lots of RAM left, but "Operator new out of memory".
To use Thread Stack statistics you need to set OS_STACK_WATERMARK in the RTX_config.h file to 1. When you want to printf the statistics you need to include "rtx_os.h" To show Stack Size you must call osThreadGetStackSize(osThreadGetId()) To show remaining space in this Stack you must call osThreadGetStackSpace(osThreadGetId()).
When I make Thread Stack bigger, the new operator runs out of memory earlier. I have no explanation for that.
I hope that helped at least a bit.
Operator new out of memory is an indication that the *heap* is out of memory, so increasing thread stack will not help, only make the problem worse. You can add logging to mbed_retarget.cpp to see what allocations you're doing. Also if you attach a debugger and break on the line you should see the exact allocation that caused the failure. Also see this blog post.
posted by 03 Aug 20176 years, 9 months ago.
if you are using multiple threads and having this issue, then the solution is to define the memory allocation for each thread.
Thread threadSample(osPriorityNormal, 2000); allocate 2000 bytes or 2 kilobytes
link to answer is in the mbed OS 5 handbook: https://os.mbed.com/docs/v5.7/reference/configuration.html
For people finding this question: add "MBED_HEAP_STATS_ENABLED=1" and "MBED_STACK_STATS_ENABLED=1" to your macros array in mbed_app.json to enable heap and stack stats. For some info on how to debug these errors, see this blog post.
posted by Jan Jongboom 03 Aug 2017