How mem_malloc Eliminates Fragmentation in MCU RAM – A Complete Guide
This article introduces the open‑source mem_malloc module for microcontrollers, explains its fragmentation‑free allocation algorithm, shows how to integrate and compile it on an IOT development board, and provides detailed test code and results illustrating efficient RAM usage.
Overview
The mem_malloc library provides a lightweight memory manager for microcontrollers that lack an MMU and have very limited RAM. It replaces the standard malloc / free pair with a deterministic allocator that eliminates fragmentation by keeping all free space contiguous.
Algorithm
A fixed‑size byte array (default 128 bytes) is used as the heap. The low‑address region stores an array of mem_block structures that hold the pointer, size, and index of each allocated block. The high‑address region is used for user data and grows downward from the end of the array toward the middle. When a block is freed, the manager shifts the remaining high‑address blocks toward higher addresses, removing any gaps and thus preventing fragmentation.
Repository
https://github.com/chenqy2018/mem_malloc
Header (mem_malloc.h)
#ifndef __MEM_MALLOC_H__
#define __MEM_MALLOC_H__
#ifdef __cplusplus
extern "C" {
#endif
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#pragma pack(1)
typedef struct mem_block {
void *mem_ptr;
unsigned int mem_size;
unsigned int mem_index;
} mem_block;
#pragma pack()
#define MEM_SIZE 128
void print_mem_info(void);
void print_hex(char *data, int len);
void print_mem_hex(int size);
int mem_malloc(unsigned int msize);
int mem_realloc(int id, unsigned int msize);
void *mem_buffer(int id);
int mem_free(int id);
#ifdef __cplusplus
}
#endif
#endifSample Test Program
#include "mem_malloc.h"
char mem_id[10] = {0}; // up to 10 simultaneous blocks
void test_malloc(int i, int size) {
printf("------test_malloc-------
");
mem_id[i] = mem_malloc(size);
if (mem_id[i] == 0) {
printf("malloc --- fail
");
printf("size=%d
", size);
} else {
char *p = mem_buffer(mem_id[i]);
memset(p, i, size);
printf("p = 0x%x, i=%d, id=%d, size=%d
", (int)p, i, mem_id[i], size);
}
print_mem_hex(MEM_SIZE);
}
void test_buffer(int i, int size) {
printf("------test_buffer-------
");
printf("i=%d, id=%d, size=%d
", i, mem_id[i], size);
char *p = mem_buffer(mem_id[i]);
if (p != NULL) {
memset(p, 0xf0 + i, size);
print_mem_hex(MEM_SIZE);
} else {
printf("test_buffer---fail
");
}
}
void test_realloc(int i, int size) {
printf("------test_realloc-------
");
printf("i=%d, id=%d, size=%d
", i, mem_id[i], size);
int ret = mem_realloc(mem_id[i], size);
if (ret) {
char *p = mem_buffer(mem_id[i]);
memset(p, 0xa0 + i, size);
print_mem_hex(MEM_SIZE);
} else {
printf("test_realloc---fail
");
}
}
void test_free(int i) {
printf("------test_free-------
");
printf("i=%d, id=%d
", i, mem_id[i]);
if (mem_free(mem_id[i]))
print_mem_hex(MEM_SIZE);
}
int main(void) {
print_mem_info();
test_malloc(1, 10);
test_malloc(2, 8);
test_malloc(3, 20);
test_free(2);
test_malloc(4, 70);
test_free(1);
test_buffer(3, 20);
test_realloc(3, 10);
for (int i = 0; i < 10; i++)
test_free(i);
return 0;
}Compilation notes
The source compiles without warnings on GCC. Keil’s stricter type checking flags an error when pointer arithmetic is performed on a void* member ( mem_ptr). The fix is to cast the pointer to char* (or another concrete type) before adding or subtracting offsets.
Results
Running the test on a 128‑byte heap shows that:
Each allocated block is recorded in the low‑address metadata area (12 bytes for three 4‑byte fields per block).
Allocations start from the high end of the array and move toward the middle.
When a block is freed, the remaining blocks are shifted, leaving a single contiguous free region.
The following screenshots (from the original test) illustrate the heap layout before and after each operation, confirming that no fragmentation occurs.
Overall, mem_malloc offers a deterministic, fragmentation‑free memory allocation strategy suitable for resource‑constrained microcontroller projects.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
