</>EmbeddedInterview

Walk through what happens when malloc(4 KB) is called on ARM64 Linux.

MediumOperating System Concepts

Calling malloc(4096) on ARM64 Linux returns a userspace allocation if successful. It does not necessarily cause an immediate system call or allocate a fresh 4 KiB physical page.

1. Allocator

The C library allocator first checks existing free blocks and caches. If it needs additional virtual memory, it may use brk or mmap according to its implementation and configuration.

2. Virtual memory

The kernel establishes a virtual memory range with suitable permissions. Physical backing may be deferred until a page is accessed.

3. First touch

unsigned char *p = malloc(4096);
if (p) {
    p[0] = 42;
    free(p);
}

If the accessed page is not yet backed, a write may generate a demand fault. The kernel allocates or otherwise establishes a writable physical page, updates translation state and resumes the instruction.

4. Subsequent access

The MMU/TLB translates the virtual address; the data can be served by caches or memory. Free returns the allocation to the allocator, which may retain the virtual mapping for reuse.

5. Caveats

A 4096-byte request does not guarantee page alignment, exactly one page, a syscall or immediate physical commitment. Allocator metadata and actual kernel page size affect the result.

Interview takeaway: malloc manages userspace allocation, while Linux manages virtual mappings and physical backing. Do not confuse allocated virtual size with resident memory.