Reputation: 2183
In other words, why doesn't free()
just return the memory to the operating system, and malloc
simply request memory from the operating system?
This unpacks to three closely related questions:
C
need to manage its own heap? (Is it because the OS will only allow you to allocate and free contiguous memory of a minimum size?)Upvotes: 3
Views: 466
Reputation: 2183
From the other answers, here's what I gathered the answer to be.
Most operating systems only allocate memory to processes in fixed sizes, called pages. When a process returns memory to the OS, it can only do so in pagefuls. A page is a sequence of memory of a fixed size. The start and end points of a page are fixed, so even if you've got a large enough amount of free memory, you won't be able to return it to the OS unless it's between the start and end points of a page.
On the other hand, you could imagine that there is no operating system (or that the operating system allocates memory to programs from its own heap). This helps me understand things better, because the operating system was getting in the way of my intuition, because it looked as if the freed memory was being dropped into a black hole, and the allocated memory was coming out of a similar black hole. Without any OS, all the memory in a computer could be pictured as belonging to a large sequence of cells. If you allocate all available memory in a computer, and then start freeing some memory, then you might not be able to find a large enough contiguous chunk of memory to fulfill a malloc request.
Upvotes: 1
Reputation: 21627
Why does C need to manage its own heap? (Is it because the OS will only allow you to allocate and free contiguous memory of a minimum size?)
Operating systems manage memory in pages. Allocating and freeing pages has a high overhead. Most allocations in C tend to be much smaller than a page size.
Upvotes: 1
Reputation: 67546
Actually it is left for the implementation. The question :
why doesn't free() just return the memory to the operating system, and malloc simply request memory from the operating system?
is wrong as nothing stops the implementation from doing it. So there is no answer for this question - every implementation can be potentially different (it only has to be standard compliant)
Upvotes: 1
Reputation: 8343
malloc
is a C method itself. You are using a standard library that provides it for you, but in the end, it is C code just like yours is.mmap
). This is too big for your regular data structure.Upvotes: 2
Reputation:
Why does C need to manage its own heap?
It's not actually specified that it needs to, but it needs to implement malloc()
and friends the way they are described in the standard. So if there was an OS already providing such an interface, a C implementation could just provide a tiny wrapper.
Is it because the OS will only allow you to allocate and free contiguous memory of a minimum size? And if that's true, what's the reason?
Yes. A typical OS will manage paged memory and map or unmap processes whole pages of memory. The unit of memory that can be "paged" depends on the hardware architecture. You might want to read some details on how memory management units (MMU) work. On architectures without MMU, the operating system might not do anything and a C implementation would just fullfill malloc()
requests from a fixed location in physical address space.
Upvotes: 8