Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is memory allocation a system call?

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.

like image 887
Jim Avatar asked Jun 30 '11 05:06

Jim


People also ask

Which system call can be used to allocate memory?

brk and sbrk are basic memory management system calls used in Unix and Unix-like operating systems to control the amount of memory allocated to the data segment of the process. These functions are typically called from a higher-level memory management library function such as malloc.

Is malloc a SYS call?

malloc is not a system call.. First of all let me explain what malloc() is.. In simple terms, memory can be assigned either statically or dynamically.. malloc() is a routine which can be used to allocate memory in dynamic way.. But please note that “malloc” is not a system call, it is provided by C library..

What is called memory allocation?

Memory allocation is a process by which computer programs and services are assigned with physical or virtual memory space. Memory allocation is the process of reserving a partial or complete portion of computer memory for the execution of programs and processes.


2 Answers

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.

like image 104
André Caron Avatar answered Sep 17 '22 18:09

André Caron


(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.

like image 20
Mat Avatar answered Sep 19 '22 18:09

Mat