uwhpsc-2016 / lectures

Notes, slides, and code from the in-class lectures.
7 stars 21 forks source link

When do `malloc()` and `free()` invoke an OS system-call? #2

Open cswiercz opened 8 years ago

cswiercz commented 8 years ago

Moved from uwhpsc-2016/homework1#40

My understanding of the way that malloc() and free() work is that they operate on a global compile-time memory pool (which we call the heap) allocated by their library implementation. I am not sure how to track whether malloc() invokes a system call to "extend the size of the heap" from OS, or just declares failure.

What system call would it invoke for Linux?

How would the OS guarantee that the requested heap block is contiguous with whatever heap was allocated initially?

cswiercz commented 8 years ago

This website has a very good description on how to implement your own malloc which sheds light on the types of Linux system-level calls that are made.

cswiercz commented 8 years ago

Linked from the aforementioned website is this great analysis of how a program manages memory with the OS. I'm sure some of the things I said in class are corrected by the contents of the linked article.

This part of the article specifically describes the program heap behavior. To answer your question, the C library has heap allocation / memory management tools of its own. It seems that the OS-calls are only performed when the program sends a request for more heap.

Speaking of the heap, it comes next in our plunge into address space. The heap provides runtime memory allocation, like the stack, meant for data that must outlive the function doing the allocation, unlike the stack. Most languages provide heap management to programs. Satisfying memory requests is thus a joint affair between the language runtime and the kernel. In C, the interface to heap allocation is malloc() and friends, whereas in a garbage-collected language like C# the interface is the new keyword.

If there is enough space in the heap to satisfy a memory request, it can be handled by the language runtime without kernel involvement. Otherwise the heap is enlarged via the brk() system call (implementation) to make room for the requested block. Heap management is complex, requiring sophisticated algorithms that strive for speed and efficient memory usage in the face of our programs’ chaotic allocation patterns. The time needed to service a heap request can vary substantially. Real-time systems have special-purpose allocators to deal with this problem. Heaps also become fragmented, shown below: