Page 1 of 1

Dynamic memory pool size

Posted: Thu Oct 27, 2016 12:00 pm
by Manveru
I am getting some strange behavior with the free memory return value when changing the STACK_SIZE value. I wonder what is the max size of the pool that guarantees that normal ram usage does not collide with the dynamic memory pool.

Thanks.

Re: Dynamic memory pool size

Posted: Thu Oct 27, 2016 1:48 pm
by Stef
What kind of strange behavior did you observed ?

Re: Dynamic memory pool size

Posted: Thu Oct 27, 2016 2:29 pm
by Manveru
For example, i have an amount of free memory returned with MEM_get_free, and after when i change the code to allocate some memory statically instead of dynamically, i get a lower amout of free memory. It is like something overwrites some of the pool zone and get_free reads it like a non-free blocks. That's why i wonder how much of memory can be allocated with STACK_SIZE without interfering with the rest of the RAM usage.

When i change the STACK_SIZE constant i have to change also something in md.ld like the "_bend" value, or something else?

Re: Dynamic memory pool size

Posted: Fri Oct 28, 2016 10:02 am
by Stef
Manveru wrote:For example, i have an amount of free memory returned with MEM_get_free, and after when i change the code to allocate some memory statically instead of dynamically, i get a lower amount of free memory.
Of course, that is expected. When you statically allocate an object in C, it eats some part of the available heap memory so the dynamic memory manager has less memory to work with.

For instance if you do that (static allocation) :

Code: Select all

int myArray[1000];

int main()
{
  int free = MEM_getFree();
  ...
}
'free' will be lower than if you do that (no allocation) :

Code: Select all

int main()
{
  int free = MEM_getFree();
  ...
}
But normally you should have almost same free value when you do that (static allocation) :

Code: Select all

int myArray[1000];

int main()
{
  int free = MEM_getFree();
  ...
}
and that (dynamic allocation) :

Code: Select all

int* myArray;
  
int main()
{
  myArray = MEM_alloc(1000 * sizeof(int));
  int free = MEM_getFree();
  ...
}
In both case, 'free' value should be very close.

Re: Dynamic memory pool size

Posted: Fri Oct 28, 2016 11:16 am
by Manveru
Oh i see. I understand better now how the memory works, bss and so. Thanks Stef.

Re: Dynamic memory pool size

Posted: Fri Oct 28, 2016 2:42 pm
by Stef
You're welcome !