The Origins of Process Memory | Exploring the Use of Various Memory Allocators in Linux C
84,928
Published 2021-09-30
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)
-
well son, when a cpu loves some data in its cache very much...
-
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.
-
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
-
Never seen brk or sbrk before after 7 years in the embedded world. Really interesting, thanks
-
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.
-
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.
-
Awesome video! Been a C programmer for 4 yrs and haven't ever heard of sbrk or mmap!
-
Awesome explanations. I never thought about using system calls to allocate memory.
-
Just discovered your channel this week, it's my fav find of the year kudos
-
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.
-
Quality content as always. Thank you!
-
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?
-
niceļ¼I love this kind of tutorial video
-
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!
-
Great video thanks š
-
Thank you for the great content. Can I cast the new allocated space to a void pointer or would that create an error?
-
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.
-
Thanks for sharing
-
Such a cool channel
-
It is to much advance concept for me. But it is so interesting š¤. Thanksā¤ļø for introduce this new concepts.