Skip to content
Advertisement

Mem alloced via mmap without munmap will cause leak after process exits or terminals

there is the code about alloc mem via mmap

void *ret = mmap(NULL, 4 * 1024, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);

when process exits normally, the memory will be return to os ?

Advertisement

Answer

According to the man and under unmap:

The region is also automatically unmapped when the process is terminated.

Which sounds very reasonable as the memory is added to the processes pages in the virtual memory which is freed upon termination.

Opengroup doesn’t say anything on the subject.

If the system does not free the memory You can try to set an at_exit up to clear up any still allocated memmaps that are still valid if you can find a pointer to it.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement