Reputation: 3423
I read the following lines regarding memory allocation to processes:
One of the important considerations in main memory management is: how should an
OS allocate a chunk of main memory required by a process. One simple approach
would be to somehow create partitions and then different processes could
reside in different partitions.
Note that this paragraph came before the concept of paging and was talking about memory allocation to whole process at once. My Question is:
Why should we create partitions? We can just keep track of holes in the memory
and keep pointers to the beginning and end of the holes. When we allocate a
process some memory, we can associate the pointer to the beginning and end of
the process with the process and end pointer of the process serves as the
pointer to the beginning of a new hole.
Upvotes: 0
Views: 2866
Reputation: 6541
I guess the answer is "efficiency". If you want to only track holes, the worst case would be to have a number of holes equal half of the bytes in given memory block (every second byte in the memory block would be a "hole"), which means that for every memory block of given size you need additional number of pointers equal half the size of the block, e.g:
Block size: 1024B
Max numer of "holes": 1024/2 = 512
Each pointer to track s single "hole" is 4B (on 32-bit architecture), so: 512 * 4 = 2048B !
I hope I don't have to convince you that this is not the best solution. Some clever folks found out, that to "solve" the problem you need some bigger "granularity" of memory. In other words the OS allocates memory only in chunks of some fixed size (called pages). Usually one page is 4KiB (4096B). You can think of it this way: when we are talking about memory allocation the OS is not a retailer. OS allocates memory only in pages. Much smaller granularity of allocation is needed on the process level - implementation of C library comes to play there (the C library allocation routines are retailers) e.g.: malloc
, free
functions and friends. These functions allocate memory from the OS (in pages) and then keep track of "used" and "unused" chunks (this is only a simplification: it is much more complicated then that, they have different "strategies" depending on the size of the chunk requested).
P.S. I know that this is very general, but I don't know how broad is your current knowledge on the subject.
Upvotes: 1