This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

RTOS: Dynamic Memory not released

Other Parts Discussed in Thread: CC2650

Tool/software: TI-RTOS

Hi there,

i'm using a Launchpad-XL in my project.

Basically i create a Dynamic array of 103 char elements with the TI-RTOS function:

Memory_calloc( NULL, (SizeT)103, 0, NULL );

After some operation I delete the dynamic array with:

Memory_free( NULL, memoryPointer, (SizeT) memorySize );


To be sure that the array was deleted, I print the the Heap information before Memory_calloc and after Memory_free.

I realise this function:

void OS_MemoryHeapInfoPrint( void )
{
Memory_Stats stats;

Memory_getStats(NULL, &stats);

/* totalSize — total size (in MADUs) of heap.
totalFreeSize — current size (in MADUs) of free memory in the heap
largestFreeSize — current largest contiguous free block (in MADUs) */

UART_PRINT( "[RTOS] Total:%d - Free:%d - Largest: %d\r\n", stats.totalSize, stats.totalFreeSize, stats.largestFreeSize );

}


On UART I see that heap free memory decrease continuously.

At first iteration on UART I see:

[RTOS] Total:32768 - Free:25800 - Largest: 25800
/* Allocation and Free */
[RTOS] Total:32768 - Free:25344 - Largest: 25344
After many iteration I see:

[RTOS] Total:32768 - Free:13032 - Largest: 13032
/* Allocation and Free */
[RTOS] Total:32768 - Free:12576 - Largest: 12576


Why the Heap size decrease?

Thanks for the help!

  • Hi Federico,
    You may have a memory leak in your code somewhere. Also when you allocate 103 bytes (not a multiple of 4), the actual buffer allocated will be slightly larger. For the BIOS HeapMem, the amount allocated will always be a multiple of the HeamMem header data structure, which is usually 8 bytes.
    You can also use ROV to monitor the heap usage.
    Best regards,
    Janet
  • Hi Janet,

    I create an instance of HeapMem with buffer size of 8192, alignment 0, Minimum Block Alignment 0.

    I modified my code with this two function:

    void* OS_MemoryAlloc( uint16_t memSize )
    {
      return HeapMem_alloc( heapMemSDK, (SizeT)memSize, 2, NULL );
    
    }
    
    void OS_MemoryFree( void* pMemAddr, uint16_t memSize )
    {
      HeapMem_free( heapMemSDK, (Ptr*)pMemAddr, (SizeT)memSize );
    }
    

    Is it correct to use alignment = 2? I read a post where a TI employee suggest to use 2 instead 0 because there is a bug into HeapMem Library?

    Furthermore, I use a library that uses the malloc and free standard function to allocate buffers and structure. Does HeapMem redirect automatically the malloc and free toward the HeapMem?

    Thanks for your time

  • Hi Fede,

    You can use any alignment as long as it's a power of 2.  I don't think there is any bug in HeapMem that would require using a non-zero alignment.  Can you post a link to this post you are referring to?

    BIOS will generate re-entrant versions of the RTS malloc(), free(), etc. functions if BIOS.heapSize is non-zero and xdc.runtime.HeapStd is not used.

    You can try adding assertions to your code by adding the following to your .cfg file:

    var Defaults  = xdc.useModule('xdc.runtime.Defaults');
    var Diags     = xdc.useModule('xdc.runtime.Diags');

    Defaults.common$.diags_ASSERT = Diags.ALWAYS_ON;

    Maybe that will help you track down the problem.

    Best regards,

    Janet

  • Hi Janet,

    thanks for the reply.

    After an analysis of my system I decided to use the SystemHeap because I use a external library (not mine) that uses the malloc/free/realloc and there isn't a way to substitute these functions with the TI-RTOS  dynamic allocation functions.

    Reading the SystemHeap I read that automatically TI-RTOS remap function like malloc/free/... to the system memory function. Is it true?

    Need I to add some modification to my code for example into app.cfg?

    I'm sorry but I don't remember which post talks about the memory alignment bug.

    Thanks for your time.

    Federico

  • Hi Federico,

    I'm not sure what the 'SystemHeap' is that you're referring to.  It is not part of the TI-RTOS kernel (SYS/BIOS).  It is true, that if you don't use HeapStd, SYS/BIOS will map malloc(), free(), etc, to xdc.runtime.Memory APIs.  You shouldn't need to add anything to your .cfg file to get this mapping.  The mapping functions will be generated, and you can find them in the generated .c file under the package/cfg file of your project (or under Debug/configPkg/package/cfg).

    Here's an example from my TI-RTOS PWM LED example.  The file pwmled_pem3.c in my workspace under

        pwmled_CC2650_LAUNCHXL_TI_CC2650F128/Debug/configPkg/package/cfg

    contains the generated malloc() function:

    /*
     *  ======== malloc ========
     */
    Void ATTRIBUTE *malloc(SizeT size)
    {
        Header *packet;
        xdc_runtime_Error_Block eb;

        xdc_runtime_Error_init(&eb);

        if (size == 0) {
            return (NULL);
        }

        packet = (Header *)xdc_runtime_Memory_alloc(NULL,
            (SizeT)(size + sizeof(Header)), 0, &eb);

        if (packet == NULL) {
            return (NULL);
        }

        packet->header.actualBuf = (Ptr)packet;
        packet->header.size = size + sizeof(Header);

        return (packet + 1);
    }

    So any calls to malloc() will go to this function, which calls Memory_alloc().  Memory_alloc() will use the BIOS heap, HeapMem, for the allocation.  Since HeapStd uses malloc(), we do not generate the malloc() call since that would lead to infinite recursion.

    Best regards,

    Janet

  • Hi Janet,
    thanks for the reply.
    Yes, with system Heap I refer to the HeapMem.
    In fact, if I open the malloc definition, I obtain the redefinition that uses the xdc_runtime_Memory_alloc (same code that you post above).

    Thanks for you time.