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

Wi-Fi Combo Forum Discussions

chch_2120956
Level 3
Level 3
First like received First like given

Dear,

    I use MCU STM32F103RD(64KB SRAM),when I have used 60KB  I call malloc(10) will fail ,can't get space ?

Thanks a lot !!!

0 Likes
1 Solution
GregG_16
Employee
Employee
25 sign-ins 25 comments on KBA 10 comments on KBA

Hello,

Since MALLOC will allocate 4K blocks and runs in virtual memory, sometimes you can have fragments.  (Usually not an issue in systems with huge memory.)

Things like "scan results" and other functions rely on MALLOC, so it is best not to use every last bit or rely on it too heavily. 

Hope this helps.

View solution in original post

0 Likes
5 Replies
Anonymous
Not applicable

I assume by

I have used 60KB

you mean that when it compiles, that is how much Static RAM the compiler has told you it is using.  That means that you have ~4KB of RAM to dynamically allocate using malloc().  I've found at least some functions in the SDK that use dynamic memory allocation, so it is likely that they are taking up those 4 KB (and probably trying to use more), which is why your malloc() call is failing.

0 Likes

Thank you replying. complies use Staic Ram 36KB, SDK some functions use about 24KB ,then I call create thread that call malloc then will fail.

I add DbgPrintf which put debug info to temp buf  at pvPortMalloc function.

extern void DbgPrintf(const char *fmt,...);

void *pvPortMalloc( size_t xWantedSize )

{

     void *pvReturn;

     int a=(int)xWantedSize;

     vTaskSuspendAll();

     {

              pvReturn = malloc( xWantedSize );

             DbgPrintf("##size=%d,pv=0x%x\r\n",a,(unsigned int)pvReturn);

     }

     xTaskResumeAll();

     ...

  return pvReturn;

}

Then I get debug message

##### task name =cloud_thread,stack_size=2048,pri=2
##size=80,pv=0x2000e8c8
##size=2048,pv=0

Thank you


0x2000e8c8 +2048 -0x20000000=61460 >61440 =>get pv=0;

the if highlight (61460) <=61424 malloc will success.


0 Likes

Thank you replying. complies use Staic Ram 36KB, SDK some functions use about 24KB ,then I call create thread that call malloc then will fail.

I add DbgPrintf which put debug info to temp buf  at pvPortMalloc function.

extern void DbgPrintf(const char *fmt,...);

void *pvPortMalloc( size_t xWantedSize )

{

     void *pvReturn;

     int a=(int)xWantedSize;

     vTaskSuspendAll();

     {

              pvReturn = malloc( xWantedSize );

             DbgPrintf("##size=%d,pv=0x%x\r\n",a,(unsigned int)pvReturn);

     }

     xTaskResumeAll();

     ...

  return pvReturn;

}

Then I get debug message

##### task name =cloud_thread,stack_size=2048,pri=2
##size=80,pv=0x2000e8c8
##size=2048,pv=0

Thank you

0 Likes
GregG_16
Employee
Employee
25 sign-ins 25 comments on KBA 10 comments on KBA

Hello,

Since MALLOC will allocate 4K blocks and runs in virtual memory, sometimes you can have fragments.  (Usually not an issue in systems with huge memory.)

Things like "scan results" and other functions rely on MALLOC, so it is best not to use every last bit or rely on it too heavily. 

Hope this helps.

0 Likes