C. K.
C. K.

Reputation: 71

What happens if you don't free allocated memory in a microcontroller?

I know that there are lots of questions on this topic but my question is specifically: What happens if I allocate memory to say a buffer, and then I will never allocate memory in my code again. Do I then need to free that memory?

My application of the code is going to be for embedded design, so a microcontroller. The code would look something like this:

#include "ringbuffer.h"

const int BUFFER_SIZE = 5;       

// create instance of ring buffer
ringbuffer buff1;
ringbuffer buff2;

int main(void)
{
    // initialize ring buffer (this allocates memory to buffers)
    ringbuffer_init(BUFFER_SIZE, &buff1);
    ringbuffer_init(BUFFER_SIZE, &buff2);

    while(true)
   {
       // receive data in buffer here
       // do stuff with data
       // run forever
   }

    // memory is never freed for the buffers
    return 0;
}

The allocate memory to the buffer happens inside the ringbuffer_init() function, and this function would be permitted to only run once in the start of main, before the loop.

My question is really. If I allocate this memory to the buffer, never free it, and I turn off and on my microcontroller, will it allocate the memory twice so that over time it builds up and crashes?

.c and .h files for the ringbuffer library on GitHub.

Upvotes: 0

Views: 1460

Answers (4)

toto-w
toto-w

Reputation: 49

If you power off and reboot your controller any memory should be lost. The startup code should initialize the whole memory with 0 and the OS should be able to allocate the same memory for the buffers again.

If it is in general a good idea to use malloc on an embedded system depends mainly on the used operating system. Most embedded OS' use definable numbers of different fixed sized mem blocks and allocate them depending on requested size as needed. They mostly supports some kind of statistics so you can watch the use of the blocks.

Don't create large buffers as automatic variables in functions. They can blast the stack. Be aware that declaring static vars as buffer may consume FLASH/ROM memory too for the init section. (Static vars were initialized per default.)

Upvotes: 0

uɐɪ
uɐɪ

Reputation: 2580

The normal rule on an embedded microcontroller is to never use dynamic memory allocation because in the limited memory space available to a microcontroller you can, over time, get memory space fragmentation and end up in a situation where the memory manager cannot find a large enough free block of memory to complete the memory allocation.

There are two cases that I can think of where allocating memory off the heap could be permitted

  • When all allocations are done at startup and are never freed (your situation)
  • When all allocations are made with the same block size.

In the first case any failures in allocation will happen before the main code starts and there is no possibility of an allocation failure happening after a long time running as there are never any more allocation attempts.

In the second case, assuming that the maximum number of allocations can be supported by the available memory, the memory cannot become fragmented as any new allocation will always fit into a block that has been freed.

In your case you will not have to free the memory as your application continues to run for as long as there is power available. When power is removed the memory will inevitably be freed.

Upvotes: 1

Lundin
Lundin

Reputation: 213678

What happens if I allocate memory to say a buffer, and then I will never allocate memory in my code again. Do I then need to free that memory?

  • On a hosted (PC-like) systems, you need to free the memory when you are done using it. The OS will reclaim it, but you should free it still. Partially because it's good practice to clean up your own mess, partially because when doing so, all heap-related bugs in your program will surface early on. When you get a program crash upon calling free(), the actual bug is elsewhere in the program.

  • On freestanding (microcontroller/RTOS) systems, you shouldn't use dynamic allocation in the first place, because it doesn't make any sense. If you for reasons unknown still insist on using it, you don't need to free() it because your program controls 100% of the available resources in the computer.

My application of the code is going to be for embedded design, so a microcontroller.

You shouldn't use that Github project in embedded systems. You need to get rid of malloc and replace it with a statically allocated buffer, one per instance of the ring buffer.

Upvotes: 3

Rishikesh Raje
Rishikesh Raje

Reputation: 8614

If I allocate this memory to the buffer, never free it, and I turn off and on my microcontroller, will it allocate the memory twice so that over time it builds up and crashes?

No, When you end the program the memory is generally reclaimed by the OS.

In your case, you have an embedded system and do not end the program. What will usually happen is that when you restart the system, the start up code will allocate the memory regions again and you will allocate the memory only once. There is no guarantee that the same address is allocated every time though.

That being said, It is a good practice to free the memory when you are done with it. This is because as your programs become bigger and you write code that goes into other systems, it becomes important to free the memory at the end of your part of the program.

Upvotes: 2

Related Questions