[CS61C FA20] Lecture 05.4 - C Memory Management: Memory Management — Transcript
Full transcript
- 0:00and welcome back so now that you know
- 0:02about
- 0:03static area the heap the stack
- 0:06how much of that do you have to manage
- 0:08how much of that does the system manage
- 0:09let's actually dig deeper into that
- 0:11so the heap is dynamic memory as you saw
- 0:15before it's a large
- 0:16pool it's not necessarily contiguous
- 0:18although every memory request will
- 0:19result in a contiguous region if i say
- 0:22int i want a thousand inch it's going to
- 0:24give you a thousand inch contiguously
- 0:26then i want another thousand ins that
- 0:28second thousand may be very far away or
- 0:30very close to the original
- 0:31maybe back to back maybe abutted maybe
- 0:33far away you don't know those things
- 0:34um as you saw before this is how you
- 0:37call it you say malik the size you want
- 0:39how many bytes you want and it returns
- 0:41uninitialized memory all that's garbage
- 0:42you didn't have to initialize it
- 0:44and use it before i mean you have to
- 0:45write to it before you read from it you
- 0:46know that before
- 0:48how did you how do you manage that
- 0:50memory well code and static are easy
- 0:51they don't grow and they're managed by
- 0:53the operating system so don't worry
- 0:54about it
- 0:56the stack space is also easy every time
- 0:57i make a function call a new frame is
- 0:59created every time you return from a
- 1:00function call
- 1:01that frame goes away and that stack
- 1:02pointer internally moves things around
- 1:04and you can't get access to that
- 1:05explicitly
- 1:06um there are ways around if you really
- 1:08know how to do it as a pro
- 1:10but for the most part you know you don't
- 1:11get access to that you don't you don't
- 1:13control that
- 1:14explicitly the system does um
- 1:17you'll do when you're writing assembler
- 1:18you'll actually will explicitly do that
- 1:20but from the point of view
- 1:21of c it's below your abstraction line um
- 1:24managing the heap is tricky though uh
- 1:26you don't have to do a lot of memory
- 1:27management in terms of thinking about
- 1:29um where to move things around again
- 1:31that's that's the os is doing that
- 1:33but you need to think about the requests
- 1:35the size requests you make and how you
- 1:37work with it so
- 1:37let's actually talk about let's take a
- 1:39little deeper into that you want malloc
- 1:41and free to be fast
- 1:42um stack allocation was really fast
- 1:46here int ar open square bracket of a
- 1:48million
- 1:49really fast if it works if it doesn't
- 1:50work it crashes but if it works it's
- 1:52fast really so that's great but you want
- 1:54malicking for you to be fast
- 1:55you want to memorize memory overhead how
- 1:57much overhead how much bookkeeping you
- 1:59need to keep track of to do this
- 2:00and you want to avoid fragmentation
- 2:02you've probably heard of this with your
- 2:03disk where you have
- 2:04you know one file might be broken up
- 2:06into lots of pieces
- 2:08and that's an issue actually with malek
- 2:09and that's probably the biggest issue so
- 2:11how we deal with fragmentation is the
- 2:12core problem with that
- 2:14and technically we call this external
- 2:15fragmentation because you have these
- 2:16things there is something called
- 2:17infernal fragmentation
- 2:18we'll talk about that at some other time
- 2:22so here's an example of what
- 2:23fragmentation looks like so
- 2:25you put a request r1 for 100 bytes boop
- 2:27there's 100 bytes yay we got our space
- 2:30then you make r2 for one byte there
- 2:32there's a byte
- 2:34then i free r1
- 2:38fragmentation those two spots are now
- 2:41your memory has been fragmented to two
- 2:42areas and when you say request r3
- 2:45where does r3 go does it go above does
- 2:47it go below
- 2:48how is it being smart about where to put
- 2:50r3 thinking about what your past pattern
- 2:52has been
- 2:53and what you should do so this is an
- 2:55interesting question how you manage this
- 2:56memory
- 2:57how the system manages memory you don't
- 2:58have to worry about how the system will
- 2:59manage memory for you
- 3:02so if you want to read knr there's a
- 3:04section 8.7 that talks about
- 3:06some features that we're not going to
- 3:07worry about um but it talks about some
- 3:10idea that each block of memory actually
- 3:12has a header that says the size of the
- 3:14block
- 3:14and i pointed to the next guy almost
- 3:16like a linked list look at that
- 3:18and it turns out it's a linked list
- 3:20except we get to the end
- 3:21in which it wraps around in the
- 3:23beginning again so it's kind of a
- 3:24continuous
- 3:25circular linked list it's kind of neat
- 3:27as a data structure
- 3:28and all free blocks all the free blocks
- 3:30are kept in there so initially in the
- 3:32program before we had this one big area
- 3:34and once i you know made a request and
- 3:36then made a second request and freed it
- 3:38now the sudden my f we call our free
- 3:40list now has two elements in that
- 3:42circular linked list
- 3:43the top area and the bottom guy are now
- 3:44two of those elements in matt circular
- 3:46linked list kind of neat
- 3:48how's malik work well malek says you've
- 3:50given me a request
- 3:52some size i have to now search my free
- 3:54list to figure out do i have can i
- 3:56can i uh serve you the memory you want
- 3:59it
- 4:00so it looks the free list and it walks
- 4:02the free list remember it's a linked
- 4:04list
- 4:06slow right all the way around if not a
- 4:09frown it says sorry nothing it returns
- 4:11null
- 4:12okay so that's important free
- 4:15here's what free does here's here's a
- 4:18here's a free space here's a free space
- 4:21you're freeing the guy in the middle
- 4:23well once you free the guy in the middle
- 4:24shouldn't this coalesce into one big
- 4:26free space
- 4:27so free has to do some work so all of a
- 4:28sudden you realize that malik is no
- 4:30longer a mile it's a function call right
- 4:32you know it's you're making a function
- 4:33called amount so malik isn't like
- 4:35instant return
- 4:37ar of a million ar square bracket close
- 4:39bracket a million that array
- 4:40request is really fast like that next
- 4:42line is instant
- 4:44basically one clock cycle but malik
- 4:46could take a long time first of all we
- 4:48got the overhead of a function call
- 4:49which we haven't talked about yet but
- 4:50that's there's some overhead to that
- 4:52and but you need to know at least you
- 4:54know that the stack has to grow right
- 4:56it's a function called every function
- 4:57called grows the stack
- 4:58so malek has a function the stack grows
- 5:00just to call malik
- 5:01it's kind of funny how the stack and
- 5:02malik are both you know malick's going
- 5:04to affect the heap but you've got to
- 5:05grow the stack to even call a function
- 5:07and that malick is a function
- 5:09but malik has to then walk its freelance
- 5:10has to look up its tables and do his
- 5:12stuff and do its internal mechanics to
- 5:14walk its
- 5:14circular free circular list to find out
- 5:16what you've got and maybe it's a lot of
- 5:18slivers
- 5:19wow you can imagine malik takes a long
- 5:22time
- 5:22to return null that's crazy in fact
- 5:24that's the worst case right
- 5:26it took all the time you paid the price
- 5:28in terms of clock cycles
- 5:29your program kind of stalls out and then
- 5:31you won't see it but i mean internally
- 5:33if i were
- 5:33if you're living at the speed of a
- 5:34gigahertz you'd be like i'm waiting here
- 5:36i got some places to go
- 5:38no time no time goes around and now i
- 5:41waited all this time and then even then
- 5:42you didn't give me anything really
- 5:44so that's hard okay
- 5:48so how do we choose a spot as i'm
- 5:50searching how do i choose a spot here's
- 5:52some
- 5:53common ways to do this so best best fit
- 5:56says
- 5:57i go through everybody you ask for a
- 5:59hundred and i say
- 6:00of every single freeze block i have in
- 6:03my circular list
- 6:04which is closest to a hundred if i have
- 6:07a hundred done
- 6:07the first guy first i was looking at was
- 6:09a hundred done here's the hundred
- 6:11but if i have 101 well maybe i keep
- 6:13looking to find a hundred okay
- 6:14you can imagine that's a little you know
- 6:16i could have given 101 but who knows if
- 6:18i would have had a perfect fit later
- 6:19it's almost like you're trying to close
- 6:21it's not perfect but
- 6:22kind of a little long here a little long
- 6:23there no i want the perfect fit best fit
- 6:25says the perfect i got to search
- 6:26everybody to get that you can see the
- 6:28trade-offs there but
- 6:29but i will give you exactly the tightest
- 6:31fit i can now what if i only what if i
- 6:33had a gap
- 6:34of 104 and you asked for 100 well i now
- 6:36have a sliver of four
- 6:38see that sliver they're gonna start to
- 6:40accumulate so think about that at these
- 6:41slivers become now that sliver is still
- 6:43another element of my list
- 6:44now i have i used to have a hundred now
- 6:45i have a little sliver for only four and
- 6:46so you might have a lot of these slivers
- 6:48that make my list even bigger
- 6:50oh yeah first fit says all right i come
- 6:53into the list
- 6:54i grab the first guy i can fast as i can
- 6:56get in get out
- 6:57bloop anybody bigger than 100 nope nope
- 6:59yes pick 100 yours go
- 7:02as i start to use that you might need to
- 7:04see a lot of these slivers start to
- 7:05happen at the beginning
- 7:06right there's always a point to the
- 7:07beginning of the circular list well you
- 7:09can imagine the slivers
- 7:10might start to grow in the front now i
- 7:12have a lot of these slivers in the front
- 7:13i keep doing first fit there's a lot of
- 7:15action in there making a lot of pebbles
- 7:16think about getting from here to the
- 7:18deep ocean the pebbles are there like as
- 7:19you walk to the pebbles to get to
- 7:21the big rocks down low next fit
- 7:24says it's like first fit first one that
- 7:27fits
- 7:28except that rather than always start the
- 7:29beginning it rotates wherever i stopped
- 7:31last time i'll remember that and that's
- 7:32the next time i'll do it so next fit
- 7:34says
- 7:34remember where you were and go through
- 7:36ah that's where i'll start next time
- 7:38so then i'll do this so it kind of
- 7:39distributes the pebbles if you think
- 7:40about that around that
- 7:42and that's kind of interesting resume
- 7:43searching from where you stopped last
- 7:44time
- 7:46and there are trade-offs on all three of
- 7:47them and you can think of workloads that
- 7:49may make
- 7:50each one of those three look really good
- 7:51or really bad you can think of that as
- 7:52well
- 7:53in conclusion this is this semi-final
- 7:56lecture in c
- 7:56very exciting almost at the end c has
- 7:58three pulls of memory
- 8:00static storage the stack the heap
- 8:03static doesn't move the stack grows and
- 8:05shrinks with function calls
- 8:07and it's where your temporary variables
- 8:08are and your parameters and the heap is
- 8:10where your malic action happens
- 8:12okay that the free and malik action
- 8:13happens three ways to deal with the free
- 8:16list that malek's going to affect
- 8:18best fit find the one that search
- 8:20everybody until they find the one just
- 8:21snugly fits the best
- 8:23first fit always at the beginning the
- 8:24first guy that matches that's bigger
- 8:25that's
- 8:26equal to or bigger than the space you're
- 8:28asking for and best fit says
- 8:30sorry and next fit says the same as neck
- 8:32as first
- 8:33except that you always remember where
- 8:34you were and start there next time
- 8:37see the next lecture we're almost there
- 8:38folks take care
About this transcript
This page contains the full transcript of [CS61C FA20] Lecture 05.4 - C Memory Management: Memory Management by CS 61C Departmental, generated from the public captions YouTube serves with the video. The transcript has 1,970 words across 306 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.
What you can do with it
Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to translate the transcript into 100+ languages.
Free YouTube transcript tool
YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, translation to 100+ languages, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.