The Origins of Process Memory | Exploring the Use of Various Memory Allocators in Linux C

84,928
0
Published 2021-09-30
In this video, we discuss how memory can be allocated to a process when coding in C using the Linux System Call ABI. We talk about how an ELF gets processed and loaded into memory, and how the memory is mapped between the user and kernel space. We go over three seperate methods (malloc, sbrk, and mmap) to allocate more memory to a process other than the predefined stack space that the compiler puts in at compile time.

Drop a like and subscribe to be alerted about new videos!

šŸ« COURSES šŸ«
www.lowlevel.academy/courses/

LLL Merch: linktr.ee/lowlevellearning

Follow me on Twitter: twitter.com/lowlevellearni1
Follow me on Twitch: twitch.tv/lowlevellearning
Join me on Discord!: discord.gg/gZhRXDdBYY

All Comments (21)
  • @Bureikusaru
    The addresses returned by calling sbrk(0) immediately followed by sbrk(0x1000) should actually be exactly the same if no other procedures like printf are allocating memory behind the scenes. What sbrk returns is actually the previous break, not the new break as suggested in the video. The returned address of sbrk(0x1000) can be used as newly allocated memory. Then calling a sbrk(0) after sbrk(0x1000) would actually show the new break after it was incremented by 4096 (0x1000) bytes.
  • @kalebbruwer
    I've heard of writing your own memory allocators, but it never occurred to me that there is something below malloc, which uses heap pointers directly. Very interesting
  • @metal571
    Never seen brk or sbrk before after 7 years in the embedded world. Really interesting, thanks
  • @RayBellis
    The heap is not really separate - the underlying memory returned by a `malloc` call is itself allocated using `sbrk` or `mmap` (these days more often the latter). It's an abstraction layer on top of those system calls, and it also permits resize and deallocating, which is why it's more complicated and slower. A particular disadvantage of using `sbrk` is that it's not possible to reclaim that memory once allocated - it becomes a permanent part of the process's virtual memory size (VSZ). You should probably also mention `alloca`, which can allocate a variable amount of memory on the stack but only for the duration of the current function - it's automatically released when the function terminates when the stack frame is reverted to its pre-function-call state.
  • @willvoiceit1
    mmap actually returns MAP_FAILED (-1) on an unsuccessful allocation, so a NULL check won't catch it like with malloc. Learned that the hard way! Did not know about sbrk though.
  • @doowi1182
    Awesome video! Been a C programmer for 4 yrs and haven't ever heard of sbrk or mmap!
  • @marciomaiajr
    Awesome explanations. I never thought about using system calls to allocate memory.
  • @paxdriver
    Just discovered your channel this week, it's my fav find of the year kudos
  • @bastian9945
    Keep this going I don't use it but your explanations are really good so I just like to watch and learn how it works. And I really like to see more rust.
  • @repflies
    Quality content as always. Thank you!
  • @jakemorales7949
    Memory allocation was such a mystery to me in college, and this helps me get a better picture. Thanks! So, can you also say that the 3 ways for getting memory is from the Heap, Stack, and OS Virtual memory, respectively?
  • @jackyli6716
    niceļ¼I love this kind of tutorial video
  • @ManuelGx2
    Couple of questions. At the 5:00 mark: - The program break was incremented by 0x21000 (in the terminal output) and not by 0x1000 (as seen in line 11, in the editor). Why is that? - When you use that new allocated space as an array you start indexing at position 100, is there a particular reason why? BTW I really enjoy your videos, keep up the great work!
  • @MoudiGhrb
    Thank you for the great content. Can I cast the new allocated space to a void pointer or would that create an error?
  • @zemoxian
    Itā€™s been well over a decade since Iā€™ve used mmap but I think itā€™s possible to use it for Inter process communication. If you map the memory to a file, multiple processes can used the same file to share the memory. Thatā€™s about all I recall about the usage at this point. I wouldnā€™t even be surprised that Iā€™m misremembering some other means of shared memory. Iā€™m pretty sure I used mmap for that.
  • @tejasjani2544
    It is to much advance concept for me. But it is so interesting šŸ¤”. Thanksā¤ļø for introduce this new concepts.