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.

dynamic memory allocation unbalance



Hello,

I'm using CCS  Version: 6.1.0.00104, TIRTOS: tirtos_tivac_2_14_04_31 with TM4C129.

i have an issue with dynamic memory allocation balance, such that when a task is created then deleted the amount of total heap is correct but the largest free is less than the total free, for example at some point i will get the following

Total Heap = 128000-Total free = 30600-Largest free = 20568

when i invoke the following function

Memory_getStats(STD_NULL, &stats);

print function ==> "Total Heap = %d-Total free = %d-Largest free = %d", stats.totalSize, stats.totalFreeSize, stats.largestFreeSize

Thanks,

Mohammed Fawzy

  • I'm assuming you are using HeapMem as the heap implementation for the system heap (it's the default unless you configure it otherwise in the .cfg file). HeapMem is a first-fit variable length memory manager implementations. We selected first-fit because it keeps allocations quick (e.g. it does not have to search the entire free block linked list).

    The largest free can easily be less than the total free. Let's look at the following example where the starting size of the heap is 2400 bytes.

    buf1 = Memory_alloc(NULL, 200, 0, &eb);  // afterwards...total free = 2200 bytes, largest free = 2200

    buf2 = Memory_alloc(NULL, 400, 0, &eb);  // afterwards...total free = 1800 bytes, largest free = 1800

    buf3 = Memory_alloc(NULL, 800, 0, &eb);  // afterwards...total free = 1000 bytes, largest free = 1000

    buf4 = Memory_alloc(NULL, 1000, 0, &eb); // afterwards...total free = 0 bytes, largest free = 0

    Memory_free(NULL, buf2, 400);            // afterwards...total free = 400 bytes, largest free = 400

    Memory_free(NULL, buf4, 1000);           // afterwards...total free = 1400 bytes, largest free = 1000       

    The largest free is smaller than the total free because there are two unused non-adjacent blocks in the heap.

    Memory_free(NULL, buf3, 800);            // afterwards...total free = 2200 bytes, largest free = 2200       

    Now since all the free blocks are adjacent, HeapMem will coalesce (combine) the free blocks into one block. You can look in RTOS Object Viewer's (ROV) HeapMem ->Freelist to see the current allocated and free block in the heap.

    Todd

  • your answer is great but it didn't answer my question as if at some point i have about 10K free heap and i can't access them, So is there a way that enable me to access this space

    Thanks,

    Mohammed Fawzy

  • I don't understand what you mean by "i can't access them".

    Does an allocation (with a requested size < largest free size) fail? This should not happen. Are you specifying an alignment that forces a bigger block to be used. For example let's assuming the following:, the largest block free is 128 bytes but it is not aligned on a 64 byte boundary. If you tried to allocate 128 bytes with an alignment of 64 bytes, the allocation will fail.

    Todd