memory allocation

02 Jan 2012

Hi,

I'm trying to find the amount of available heap memory in my mbed LPC1768 device. I'm using malloc in a loop, starting with the memory block of 0x8000 bytes and decreasing it until malloc returns a pointer different from NULL. I guess this should be the available heap memory (23924 bytes in my case)... but then if I free the memory allocated in the first loop and in the second loop start to increase the desired amount of heap memory malloc function is returning non NULL pointers with the amounts of memory where it previously (in the first loop) returned NULL (in my case I can allocate 27932 bytes in the second loop).

I wonder why do I get the different values and why are they so different (4008 bytes in my case)?

#include "mbed.h"

int main() {
    
 void* p;
 int i=0x8000;
 while(i>0)
 {
    p=malloc(i);
        if (p != NULL)
        {
            free(p);
            printf("\rAmount of available memory when counting down: %d\n",i);
            break;
        }
    i=i-1;
 }
 
 while(i<0x8000)
 {
    i=i+1;
    p=malloc(i);
        if (p == NULL)
        {
             printf("\rAmount of available memory when counting up: %d\n",i-1);
             break;
        }
        else 
            free(p);  
 }           
}
02 Jan 2012

Why not just take the difference between the stack and start of heap:

unsigned long  heapSize()
{
   char   stackVariable;
   void   *heap;
   unsigned long result;
   heap  = malloc(4);
   result  = &stackVariable - heap;
   free(heap);
   return result;
}
02 Jan 2012

Thanks for the function - it's doing its job, but the thing is I'm actually trying to determine what is the maximal size of an array I can use to store some results. If I implement your function to my example I get the heapSize of 32136 bytes (32064 after I am "playing" with the malloc in a loop) - but still, the maximum amount of memory I can allocate is 23916 bytes (27924 bytes in the 2nd loop)... now I wonder where is the difference between the memory I can allocate with malloc and the amount of memory I get when using the heapSize() function? Can I not allocate (and use) all (or almost all - I would let some place for the stack to grow) of the heap memory size I get with the heapSize() function?

30 Mar 2015

Stack is used for static memory allocation and Heap for dynamic memory allocation, both stored in the computer's RAM .

Variables allocated on the stack are stored directly to the memory and access to this memory is very fast, and it's allocation is dealt with when the program is compiled. When a function or a method calls another function which in turns calls another function etc., the execution of all those functions remains suspended until the very last function returns its value. The stack is always reserved in a LIFO order, the most recently reserved block is always the next block to be freed. This makes it really simple to keep track of the stack, freeing a block from the stack is nothing more than adjusting one pointer.

More about......Difference between stack and heap

http://net-informations.com/faq/net/stack-heap.htm

Helsy

Please log in to post a reply.