Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does physical pages are allocated and freed during the malloc and free call?

Malloc allocates memory from one of the virtual memory regions of the process called Heap. What is the initial size of the Heap (just after the execution begins and prior to any malloc call)? Say, if Heap starts from X virtual address and ends at Y virtual address I want to know the difference between X and Y.

I have read the answers to the duplicate question which was asked earlier.

How do malloc() and free() work?

The answers written are all in the context of virtual address but I want to know how the physical pages are allocated. I am not sure but I think that this initial size (X-Y) would not have the corresponding page table entries in the operating system. Please correct me if I am wrong.

Now, say there is a request for allocating (and using) 10 bytes of memory, a new page would be allocated. Then, all the further requests for memory would be satisfied from this page or every time a new page would be allocated? Who would decide this?

When the memory would be freed (using free()) then at what time this allocated physical page would be freed and marked as available? I understand that the virtual address and physical page would not be freed immediately as the amount of memory freed could be very less. Then at what time the corresponding association between the physical and virtual address would be terminated?

I am sorry if my questions may sound strange. I am just a newbie and trying to understand the internals.

like image 272
Ashish Avatar asked Nov 01 '10 13:11

Ashish


2 Answers

Normally you can think of physical pages as being allocated temporarily. If the memory that your program is using is swapped to disk, then at any time the association between your virtual addresses and physical RAM can be dropped, and that physical RAM used for something else.

If the program later accesses that memory, the OS will assign a new physical page to that virtual page, copy the data back from the page file into the physical memory, and complete the memory access.

So, to answer your question, the physical page might be marked as available when your program is no longer using the allocations that were put in it, or before. Or after, since malloc doesn't always bother freeing memory back to the OS. You don't really get to predict this stuff.

This all happens in the kernel, it's invisible from the point of view of C, just as CPU caching of memory is invisible from C. Well, invisible until your program slows down massively due to swapping. Obviously if you disable the swap file then things change a bit: instead of your program slowing down due to swapping, some program somewhere will fail to allocate memory, or something will be killed by the OOM killer.

like image 80
Steve Jessop Avatar answered Oct 02 '22 16:10

Steve Jessop


How pages are allocated is different in each os, Linux, Mac, Windows, etc. In most/all implementations there is a kernel mechanism that defines how it is allocated.

http://www.linuxjournal.com/article/1133

like image 33
grmartin Avatar answered Oct 02 '22 14:10

grmartin