Why free() doesn't really frees memory?

前端 未结 4 2218
情歌与酒
情歌与酒 2020-12-01 22:45

i\'m doing some tests allocating and deallocating memory. This is the code i\'m using:

#include 
#include 

#define WAVE_SIZE          


        
相关标签:
4条回答
  • 2020-12-01 22:59

    When memory is allocated and later free'd, that memory still stays with the process but is marked as free so it can be allocated again. This is because otherwise the operating system have to alter the virtual memory mapping of the process each time you call malloc or free, which takes time.

    0 讨论(0)
  • 2020-12-01 23:03

    free does not necessarily release the memory back to the operating system. Most often it just puts back that memory area in a list of free blocks. These free blocks could be reused for the next calls to malloc.

    0 讨论(0)
  • 2020-12-01 23:18

    When the system memory will be actually released depends on the OS.

    Foe example in Windows, even if you close a program, the memory is not released at the same time. It is made in order to reuse it on the next program start.

    0 讨论(0)
  • 2020-12-01 23:20

    free() merely tells the allocator that your program no long needs this block. The allocator may cache it for further allocation, or it may return it to the system by change the brk pointer. It all depends on the implementation.

    0 讨论(0)
提交回复
热议问题