Heap memory issues

I am facing heap memory issues in my code. I am using dynamic memory allocation for saving microphone data into sd card. Along with this I am using wifi also.

CONFIG_HEAP_MEM_POOL_SIZE=153600 this configuration is already large.
I am attaching my code for reference.

Updated_home_with_wifi.zip
  • Before we dive into your code, Can you please describe what the issues are exactly using the heap? Are you getting heap memory corruption?

    Which companion chip with RAM are you using to control the nRF7002? seems like 153600 is huge chunk of allocation for heap. Have you checked in the memory map if you had that much free space in RAM?

  • So I am using MS12SF1 module (nrf5340+nrf7002). In my application I need to enable wifi as well as collect audio data through pdm microphone and save it into the sd card. When the wifi was not enabled the application was working smoothly but as the wifi configurations was enabled the memory was not getting allocated for microphone.
    Also I am using dynamic allocation because when I tried using static memory I need to take it large enough as the microphone data is coming with speed and is also very large. With small static array it was either facing loses or the generated audio was fast forwarded. Also on using static memory my ram gets increased to 88% but if I use dynamic it stays till 57% around but the issue is in allocation of memory during runtime

    Also I changed something in my code, previously I was allocating memory using k_malloc inside the audio_callback(), but later on I created a separate function for allocating memory by calling the function in main() but nothing changed

  • After checking the heap stats, the below logs were printed before network got connected

    [00:00:00.186,523] <inf> home_demo: INFO: Allocated Heap = 88452

    [00:00:00.186,523] <inf> home_demo: INFO: Free Heap = 64368

    [00:00:00.186,553] <inf> home_demo: INFO: Max Allocated Heap = 88452

    But when I called the heap stats after network got connected, I got

    [00:00:26.442,382] <inf> home_demo: INFO: Allocated Heap = 122120

    [00:00:26.442,382] <inf> home_demo: INFO: Free Heap = 30652

    [00:00:26.442,382] <inf> home_demo: INFO: Max Allocated Heap = 127404

  • There seems to be some additional heap allocated somehwhere after the connection. Try to enable CONFIG_SYS_HEAP_RUNTIME_STATS=in your prj.conf to get some dynamic heap stats to see if we get to understand where the difference in memory after connection is happening.

    Also is that the memory issue you are talking about, the difference in max allocated heap and allocated heap or are you seeing any other side effects of this in the functioning of your app?

  • After enabling the above configuration you mentioned and adding 

    void rtos_hal_print_heap_info(void)
    {
    sys_heap_runtime_stats_get(&_system_heap.heap, &stats);

    LOG_INF("\n");
    LOG_INF("INFO: Allocated Heap = %zu\n", stats.allocated_bytes);
    LOG_INF("INFO: Free Heap = %zu\n", stats.free_bytes);
    LOG_INF("INFO: Max Allocated Heap = %zu\n", stats.max_allocated_bytes);
    LOG_INF("\n");

    return;
    } in my code only I got the allocated heap and free heap 

    On running the code I used to get 
    LOG_ERR("Memory allocation for circular buffer failed\n"); this continuously and if I try to reduce the size of the k_malloc it executes for hardly 1sec and after that I get fault (have set 
    CONFIG_RESET_ON_FATAL_ERROR=n) otherwise before it was getting reset
Related