How are physical pages distributed and deallocated during malloc and free call?

Malloc allocates memory from one of the virtual memory areas of the Heap process. What is the initial heap size (immediately after the start of execution and before any malloc call)? Let's say if a heap starts with virtual address X and ends with virtual address Y, I want to know the difference between X and Y.

I read the answers to the duplicate question that was asked earlier.

How do malloc () and free () work?

All written answers are in the context of a virtual address, but I want to know how physical pages are distributed. I am not sure, but I think that this initial size (XY) would not have the corresponding entries in the page table in the operating system. Please correct me if I am wrong.

Now, let's say, there is a request to allocate (and use) 10 bytes of memory, a new page will be allocated. Then will all further memory requests be satisfied from this page, or every time a new page is selected? Who will solve this?

When the memory is freed (using free ()), at what time will this allocated physical page be freed and marked as available? I understand that the virtual address and physical page will not be freed immediately, as the amount of freed memory can be very less. Then, at what time will the corresponding link between the physical and virtual addresses be terminated?

I apologize if my questions may seem strange. I'm just a beginner and trying to understand the insides.

+4
source share
3 answers

You can usually think that physical pages are temporarily highlighted. If the memory used by your program is replaced with a disk, then at any time the connection between your virtual addresses and physical memory can be deleted, and physical memory is used for something else.

If the program later accesses this memory, the OS will assign a new physical page to this virtual page, copy the data from the page file to the physical memory, and terminate access to the memory.

So, to answer your question, a physical page can be marked as accessible when your program no longer uses the allocations allocated in it or earlier. Or after malloc is not always worried about freeing memory back to the OS. You really cannot predict this.

This all happens in the kernel, it is invisible from the point of view of C, just as caching memory from memory is invisible from C. Well, it is invisible until your program slows down massively due to exchange. Obviously, if you disable the swap file, everything will change a little: instead of slowing down your program due to an exchange, some program somewhere will not be able to allocate memory, or something will be killed by the OOM killer.

+4
source

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

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

+3
source

How the OS works, it depends on the OS. In most cases (if not all), the OS at least notes in its table that there is a distribution. You are probably confused by the fact that in some situations, some operating systems do not commit memory until it is available. (keyword: overcommit; if you want my opinion on this, it should be for each process, not global, and by default for fixing memory).

Now, to return the freed memory in the OS, which depends on the allocator. It cannot return anything but the page, therefore, as long as the page contains the allocated memory, it will not be returned. And depending on how it was allocated, there may be other restrictions; for example, when using sbreak() , as is usually done on Unix, you can only return the last selected pages (i.e., if you return the page, everything selected after it is returned). A more modern approach to Unix uses mmapped memory for large blocks, in accordance with the fact that mmapped memory can be returned as desired. For small distribution blocks, it is often considered not worth checking to see if pages in the middle can be returned, and therefore mmapped memory is not used.

+2
source

All Articles