I am currently working on a bare metal project with an arm processor using GCC arm compiler. If I call malloc, is the allocated memory tracked in the MMU? For some reason I had assumed an OS typically handled this but since the call to malloc compiles and I see it is defined in the libc/usr/include I am now thinking that the os implements another layer of memory management on top of the hardware management.
I guess i've got two questions: With full os (posix or windows) is the hardware tracking the allocated memory or is the os? With arm compiler is is the hardware tracking the allocated memory or is the os?


1 Answer 1


Memory allocation by malloc is mostly a software function.  Typically the blocks allocated by software are too small for the MMU to manage.  For example, an MMU typically manages pages of size 4k or larger.  In fact, the larger the pages, the more efficient the hardware can be.  Hardware also manages pages on page aligned boundaries (often like 4k), so, page sizes and alignments don't always correspond to software allocation sizes and alignment requirements.

TL;DR, software memory allocation is somewhat independent of hardware page management, as software carves (and releases) both small objects and larger arrays out of the available address space, while hardware manages the working set in terms of resident pages.

  • So if I understand, software allocation by os via malloc is a layer in between the application and the hardware. The hardware provides some management of fixed sizes but the software (presumably os) is responsible for creating a complete allocation API for the application. Nov 24, 2018 at 15:52
  • Yes. malloc lives almost entirely in the library of the application. When malloc is out of memory, it asks the os for a large chunk (say using the sbrk system cal), which will cause the operating system to expand the address space of the process, which implies modifications to the process' page tables, on a system using virtual memory. When malloc is not out of memory, it manages object allocation (and freeing) within the address space, while the hardware monitors page accesses, and the operating system swaps pages in & out as needed (e.g. on page faults).
    – Erik Eidt
    Nov 24, 2018 at 17:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.