StackOverflow on the answer:
In many malloc/free implementations, free does normally not return the memory to the operating system (or at least only in rare cases) . the reason is, that you will get gaps in your heap and thus it can happen, that you just Finish off your 2 or 4 gb of virtual memory with gaps. this should be avoided of course, since as soon as the virtual memory is finished, you will be in really big Trouble. the other reason of course is, that the os can only handle memory chunks that are of a specific size And alignment. tO be specific: normally the os can only handle blocks that the virtual memory manager can handle (most often multiples  OF 512 BYTES EG. 4KB).
so returning 40 bytes to the os will just not work. So what does free do?
Free will put the memory block in its own free block list. Normally it also tries to meld together adjacent Blocks in the address space. the free block list is just a circular list of memory chunks which have of course some administrative data in the beginning. this is also the Reason, why managing very small memory elements with the standard malloc/free is not efficient. Every memory chunk needs Additional data and with smaller sizes more fragmentation happens.
the free-list is also the first place that malloc looks at when a new chunk of memory is needed. it is scanned before it calls for new memory from the os. when a chunk is found that is bigger then the needed memory, it is just divided into two parts. one is returned to Caller, the other is put back into the free list.
There are many different optimizations to this standard behaviour (for example for small chunks of memory). but since malloc and free must be so universal, the standard behaviour is always the fallback when alternatives are not usable. there are also optimizations in handling the free-list -- for Example storing the chunks in lists sorted by sizes. but all optimizations also have their own limitations.
first malloc ()/free () malloc () free () free 512bytes free malloc ()
Does the operating system take back the memory immediately after malloc has requested the memory and free it?