Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mmap allocates memory in heap ?

Tags:

mmap

I was reading about mmap in wikipedia and trying out this example http://en.wikipedia.org/wiki/Mmap#Example_of_usage. I have compiled this program with gcc and ran valgrind overit.

Here is valgrind output:

# valgrind a.out 
==7018== Memcheck, a memory error detector
==7018== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==7018== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==7018== Command: a.out
==7018== 
PID 7018:   anonymous string 1, zero-backed string 1
PID 7019:   anonymous string 1, zero-backed string 1
PID 7018:   anonymous string 2, zero-backed string 2
==7018== 
==7018== HEAP SUMMARY:
==7018==     in use at exit: 0 bytes in 0 blocks
==7018==   total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==7018== 
==7018== All heap blocks were freed -- no leaks are possible
==7018== 
==7018== For counts of detected and suppressed errors, rerun with: -v
==7018== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2)
PID 7019:   anonymous string 2, zero-backed string 2
==7019== 
==7019== HEAP SUMMARY:
==7019==     in use at exit: 0 bytes in 0 blocks
==7019==   total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==7019== 
==7019== All heap blocks were freed -- no leaks are possible
==7019== 
==7019== For counts of detected and suppressed errors, rerun with: -v
==7019== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2)

My Question is

Does mmap allocate memory on Heap ? if not what does munmap do ?

like image 313
Sethu Avatar asked Feb 19 '13 09:02

Sethu


People also ask

Does mmap allocate physical memory?

The mmap() system call can also be used to allocate memory (an anonymous mapping). A key point here is that the mapped pages are not actually brought into physical memory until they are referenced; thus mmap() can be used to implement lazy loading of pages into memory (demand paging).

What does mmap actually do?

In computing, mmap(2) is a POSIX-compliant Unix system call that maps files or devices into memory. It is a method of memory-mapped file I/O. It implements demand paging because file contents are not immediately read from disk and initially use no physical RAM at all.

Does mmap use malloc?

For very large requests, malloc() uses the mmap() system call to find addressable memory space. This process helps reduce the negative effects of memory fragmentation when large blocks of memory are freed but locked by smaller, more recently allocated blocks lying between them and the end of the allocated space.

How does mmap work internally?

mmap works by manipulating your process's page table, a data structure your CPU uses to map address spaces. The CPU will translate "virtual" addresses to "physical" ones, and does so according to the page table set up by your kernel. When you access the mapped memory for the first time, your CPU generates a page fault.


1 Answers

On a Unix-like system, your program's address space consists of one or more virtual memory regions, each of which is mapped by the OS to physical memory, to a file, or to nothing at all.

The heap is, generally speaking, one specific memory region created by the C runtime, and managed by malloc (which in turn uses the brk and sbrk system calls to grow and shrink).

mmap is a way of creating new memory regions, independently of malloc (and so independently of the heap). munmap is simply its inverse, it releases these regions.

like image 160
Oliver Charlesworth Avatar answered Sep 24 '22 02:09

Oliver Charlesworth