Does the operating system take back the memory immediately after malloc has requested the memory and free it?

Source: Internet
Author: User

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?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.