6 years, 8 months ago.

C++ std::list memory problem using the ARM compiler, allocator question

I have the following C++ code which works fine with the GCC compiler but does needs 4624 bytes memory using the ARM online compiler for a single entry in the _myrecs list. Which is way too much for a single 36 byte record.

list sample code

#include "mbed.h"
#include <list>

struct TestRec {
    uint32_t lastTxSize;
    int tmp[32];
};

main {
    struct TestRec rc; 
    list<TestRec> _myrecs;
    _myrecs.push_back(rc);

    // __heapstats((__heapprt)mprintf, stdout);
    // __heapvalid((__heapprt)mprintf, stdout, 1);
    for(;;)
	;
}

I was running out of memory therefore I investigated into it using the heapstats feature. Any ideas what can be done. I have four different lists in my solution. I believe there is an default allocator which pre-allocates 100 entries or so.

Any help is appreciated, thanks Helmut

Do you know roughly how many objects are going to be pushed to this list? Also, I am curious to see if you see similar sizing results when explicitly creating a list of one element, std::list<TestRec> _myrecs(1, TestRec(), std::allocator<TestRec>());

posted by Michael Bartling 22 Aug 2017

Hi Michael, I will look into this and update this post.

posted by Helmut Tschemernjak 24 Aug 2017

Dear Michael, I found the source of the problem after debugging it within MDK for a while. The default _RWSTD_MINIMUM_NEW_CAPACITY is 32 (in _defs.h) causes the excessive memory usage, as I have few entries only I added the following defines before I include the mbed, etc. headers:

  1. define _RWSTD_MINIMUM_NEW_CAPACITY _RWSTD_C::size_t (1)
  2. define _RWSTD_INCREASE_CAPACITY(x) (x)

The resolves the problem.

posted by Helmut Tschemernjak 02 Sep 2017

Glad to hear the problem has been resolved!

posted by Michael Bartling 06 Sep 2017

1 Answer

6 years, 8 months ago.

Consider using either an array, or a linked list for example (https://developer.mbed.org/users/sam_grove/code/LinkedList/), or something else which meets your requirements.

Those standard C++ things like list and also vector take a ton of space. Apparently less with GCC, but probably still alot.

Hi Eric thank you for the advise, I use C++ lists and maps because they are efficient and part of the C++ language available on all platforms/compilers. They are also good documented. I have a project with a 20kB STM MCU and it works quite well, only the ARM compiler has a different allocation problem as described. Maybe some ARM engineers give advise on it. I can also open a case with Keil for by MDK which under support. PS: In code size of the ARM compiler is way better then gcc, however in this case gcc runtime lib is better.

posted by Helmut Tschemernjak 20 Aug 2017

See my comment above.

posted by Helmut Tschemernjak 02 Sep 2017