Ernestina Juan
Ernestina Juan

Reputation: 967

Where does malloc return memory?

I am learning about malloc function in C. I know how to request memory with

p = malloc(number_of_bytes);

and to free it later when it's not used with

free(p);

But I want to know something a bit more theoretical than this: I know that with virtual memory, there are three possibilities:

  1. A virtual address can have no physical storage assigned (in pagefile or in physical memory)

  2. A virtual address can have physical storage on the pagefile

  3. A virtual address can have physical storage on physical memory

So, my question is:

Where does malloc returned memory is (I mean, is it on the pagefile, on the physical memory (heap maybe?))?

Upvotes: 1

Views: 1470

Answers (3)

It is implementation specific. On Linux with GNU libc or musl-libc, malloc is often calling mmap(2) system call to get more virtual memory from the kernel. free is generally marking a memory zone to be reusable by future calls to malloc but occasionally (for big memory zones) is releasing the memory with munmap

Use strace and study the relevant source code ... (Usually, malloc is implemented inside some free software standard C library on Linux). I believe that musl-libc has a nicely readable implementation of malloc

Read more about processes, virtual address spaces, virtual memory and Advanced Linux Programming.

See also proc(5) and think about the output of cat /proc/$$/maps and cat /proc/self/maps

On Linux specifically, read also about Linux memory overcommit. It is a feature that I dislike and that I am disabling ...

Be aware of ASLR. You can disable it (e.g. to have reproducible watchpoints under GDB).

For debugging malloc related issues (notably memory leaks) use valgrind (and, with recent GCC compilers, their -fsanitize= debugging options).

Upvotes: 4

Prophet Daniel
Prophet Daniel

Reputation: 327

Usually it will be allocated in the virtual memory if there's one. So let's assume it has one and the memory hardware is composed by a Hard Disk and RAM. If there's no space available in the physical memory (RAM), it will be allocated in the pagefile inside the Hard Disk.

Heap is the memory for objects and is allocated samewise. Just to clear up concepts here about the heap, imagine an embedded application without an operating system and without a virtual memory, if it's an object it will be allocated in the heap area in the physical memory (RAM), if it's not it will be allocated outside the heap area but still in the physical memory (RAM).

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 727137

is it on the pagefile, on the physical memory?

On operating systems with virtual memory management malloc has no idea of how the virtual memory that it allocates is mapped to physical memory, and that is entirely by design. malloc operates on a higher level than the virtual memory manager. To malloc all memory in the address space provided by the OS is the same.

Some pages may start with physical mapping, only to be swapped out into a file later on, and then back into memory as needed. However, operating system does all of this transparently to your program, so malloc is unaware of the physical memory.

Upvotes: 6

Related Questions