61

Is memory allocation a system call? For example, malloc and new. Is the heap shared by different processes and managed by the OS. What about private heap? If memory allocation in the heap is managed by the OS, how expensive is this?

I would also like to have some link to places where I can read more about this topic.

trincot
  • 317,000
  • 35
  • 244
  • 286
Jim
  • 665
  • 1
  • 6
  • 6

4 Answers4

63

In general, malloc and new do not perform a system call at each invocation. However, they use a lower-level mechanism to allocate large pages of memory. On Windows, the lower mechanism is VirtualAlloc(). I believe on POSIX systems, this is somewhat equivalent to mmap(). Both of these perform a system call to allocate memory to the process at the OS level. Subsequent allocations will use smaller parts of those large pages without incurring a system call.

The heap is normally inner-process and is not shared between processes. If you need this, most OSes have an API for allocating shared memory. A portable wrapper for these APIs is available in the Boost.Interprocess library.

If you would like to learn more about memory allocation and relationship with the OS, you should take a look at a good book on operating systems. I always suggest Modern Operating Systems by Andrew S. Tanenbaum as it is very easy to read.

Avidan Borisov
  • 3,235
  • 4
  • 24
  • 27
André Caron
  • 44,541
  • 12
  • 67
  • 125
  • 13
    On Linux systems, `brk(2)` and `sbrk(2)` are used alongside `mmap(2)`. I would also mention that Knuth's AOCP Vol. I is a great reference for memory allocation too. – jmbr Jun 30 '11 at 06:19
  • 4
    This depends on the runtime. With Visual Studio 2010 release library malloc and free call HapAlloc / HeapFree directly threfore there is a system call for each allocation. VirtualAlloc is then used by the Heap functions. – Suma Jun 30 '11 at 06:27
  • 3
    @Suma: HeapAlloc isn't a system call, and does not necessarily perform any system call. – user541686 Aug 29 '13 at 06:46
  • @Mehrdad [HeapAlloc](http://msdn.microsoft.com/en-us/library/windows/desktop/aa366597%28v=vs.85%29.aspx) is defined in kernel32.dll if that is not a system, what is? – Suma Aug 29 '13 at 10:29
  • 5
    @Suma: A "system call" is not defined as "defined in kernel32". A system call is (more correctly) defined as something that invokes the `syscall` or `sysenter` instruction. Those instructions are in `ntdll.dll`, not `kernel32.dll`. And they are inside functions whose names begin with `Nt` and `Zw`, not `Rtl`. And in fact, `HeapAlloc` calls `RtlAllocateHeap`, which does *not* necessarily perform any system calls. – user541686 Aug 29 '13 at 14:49
  • @jmbr same remark as Suma. This depends on what flavor of malloc you link against. the default gcc suite and libc/stdlib uses a version where malloc is totally empty but for a call to `mmap`. Same truth for windows, default CRT's malloc is just a call to `VirtualAlloc`. The days of complicated `dlmalloc`, `tcmalloc` and cie are over. – v.oddou Nov 07 '18 at 08:03
  • I also highly recommend this two-part video series: "Mysteries of Memory Management Revealed, with Mark Russinovich" https://www.youtube.com/watch?v=TrFEgHr72Yg – Dan Bechard Dec 25 '22 at 20:23
26

(Assuming an operating system with memory protection. Might not be the case e.g. in embedded devices.)

Is memory allocation a system call?

Not necessarily each allocation. The process needs to call the kernel if its heap is not large enough for the requested allocation already, but C libraries usually request larger chunks when they do so, with the aim to reduce the number of system calls.

Is the heap shared by different processes and managed by the OS. What about private heap?

The heap is not shared between processes. It's shared between threads though.

How expensive kernel memory allocation system calls are depends entirely on the OS. Since that's a very common thing, you can expect it to be efficient under normal circumstances. Things get complicated in low RAM situations.

Francesco
  • 3,200
  • 1
  • 34
  • 46
Mat
  • 202,337
  • 40
  • 393
  • 406
21

See the layered memory management in Win32.

enter image description here

Memory allocation is always a system call but the allocation is made as pages. If there are space available in the committed pages, memory manager will allocate the requested space without changing the kernel mode. The best thing about HeapAlloc is, it provides fine control over the allocation where Virtual Alloc round the allocation for a single page. It may result in excessive usage in memory.

Basically the default heap and private heaps are treated same except the default heap size is specified during the linking time. The default heap size is 1 MB and grows as required.

Community
  • 1
  • 1
sarat
  • 10,512
  • 7
  • 43
  • 74
  • 6
    `"Memory allocation is always a system call"` This implies that every call to `malloc()` results in a system call (a transition to kernel-mode), which is 100% incorrect. – Jonathon Reinhart Feb 27 '15 at 01:57
7

Memory allocation functions and language statements like malloc/free and new/delete are not a system calls. Malloc\free is a part of the C\C++ library and new\delete is a part of C++ runtime system. Calls of both can occasionally lead to the system calls. In the other languages memory allocation implemented in the similar way.

In general memory management can't be implemented without involving OS at all, because memory is one of the main system resources, and due to this global memory management made by OS kernel. But due to the fact that the system calls are relatively expensive, peoples try to design languages and memory allocation libraries in such a way to minimize amount of system calls.

As I know heap is an intra-process entity. That is mean that all memory allocation/deallocation requests are managed entirely by process itself. Operating system knows only the heap location and size and services two types of requests from the intra-process memory management system:

add memory page at virtual address X
release memory page from virtual address X

Local memory management system request these services when it decides that it haven't enough memory in the memory pool of heap and when it decides that it have too much memory in the memory pool of heap. Despite the fact that the memory allocation is usually designed in such a way to minimize amount of system calls it still stay about order more expensive then memory allocation on stack. This is because the memory allocation\deallocation algorithms of heap are much more complex and expensive than the same of stack.

Delimitry
  • 2,987
  • 4
  • 30
  • 39
ZarathustrA
  • 3,434
  • 32
  • 28