Virtual Memory: 3 What is Virtual Memory?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
all right so now let's talk about what virtual memory is and I'll give you a hint here virtual memory is all about indirection remember we mentioned that there are different address spaces for the program and the memory and virtual memories about the in direction pointing between them so virtual memory is a layer of indirection and this comes from the general idea that any problem in computer science can be solved by adding indirection if you got a problem add a layer of indirection and you can almost always solve it let's take a look at how this works in virtual memory so without virtual memory we don't have indirection the program address that the program uses in MIPS is exactly the same as the RAM address so if the program tries to load from address 2024 it's going to look for RAM address thousand 24 so here's our 32-bit address space the program guarantees and here's our 32-bit address space if we only have one gigabyte of RAM in our computer and we saw before these addresses are fine address 0 is going to map over here to 0 1 2 1 etc all the way through the first gigabyte but what happens is when we start using addresses that are beyond the first gigabyte they don't have anywhere to map to so with this direct mapping here we're going to crash if we try to access more memory so virtual memory is going to solve this by adding a level of indirection so in virtual memory we're going to take the program addresses and map them to a ram address so instead of going 1 to 1 like this we're on a slightly different mapping so here's virtual memory and it's gonna do this mapping so we have our 32-bit program address space we still only have one gigabyte of memory installed in our computer but now we're going to have this map in the middle and this map is going to provide exactly this indirection that I was talking about before so let's see how this can work with the map so here's address 0 where's it going to go to well we look in the map the map sends it over here to ram address 1 ok no why not it can map it to anywhere it wants here comes the next one it Maps it up here to address 0 ok sure here comes the next one it Maps it into here now we've done the same thing we've done here we've mapped everything it's not the same mapping but it's just a mapping what happens now when we access this next piece of data well now that we have a map this next piece of data can say hey it's not in memory it's somewhere else it's on the hard disk for example so our program is not going to crash it's going to know it has to go get the data from the hard disk before can continue so what virtual memory does here is it gives us flexibility in how we can use our memory here so this mapping allows us to use our memory in a more flexible way so let's take a look at how virtual memory solves these three problems that we looked at before so here's our same 32-bit address program space and our same one gigabyte 32 30 bit ram address space so we have one gigabyte of memory installed and MIPS promises four gigabytes of memory space to every program now we've got this map in the middle and we've got a disk over here where we can put stuff that doesn't fit so let's take a look at this we're going to map some of the programs address space to disk when it doesn't fit and when we need it we'll bring it into memory so here we are with virtual memory and remember the program address is going to map to the RAM addresses so the program goes and tries to load address zero goes to the map the map says ah ok address zero is over here no problem you can access it so the virtual memory has mapped program address 0 to RAM address 1 now the program wants to access will address 1 ok so we go into the map and the map goes over here and it maps address 1 to RAM address 0 no problem go to program address 2 it goes and maps it down here that's fine now here's where it gets fun so now we try to load the program address 3 the next one here and this one doesn't fit into the amount of memory we have here so this part here took up our first gigabyte of memory now we're trying to load something beyond 1 gigabyte what's going to happen well when you do this you go to the map and the map says hey I don't have any more space in your memory that's installed in your computer for this data what am I going to do but because I have this flexibility I can move the data around so it's going to go put it on the disk now so virtual memory is going to find the oldest piece of data in memory in this case it's a 0 1 here it's going to go and move it out to the disk this is called a page out it's taking this page of memory and writing it out to disk then it's going to go and update the map so the map here still says that this is in memory I'm going to update that map to say nope it's not in memory it's on disk now that we've freed up this memory here now we can go ahead and access program at address 3 here and in the memory that we freed up from that earlier data so by having this mapping we can use our disk to give us the illusion of unlimited memory so as long as we have four gigabytes of space on our disk we can always give every program four gigabytes of memory because we can do this mapping and put as much of the data on the disk as we need to so that's how virtual memory solves the problem of not enough memory now here's a question about virtual memory performance what's going to happen to the program performance when the data it needs is on the disk well we're going to get really bad performance reading from disks is slow just sort of thousands of times slower than memory so if we have to put the data on the disk it's going to slow down our performance enormous ly this is why people tell you if you buy more memory for your computer it'll run faster because you'll have less time you have to go to the disk to access data now let's take a look at how we solve problem number two this was the holes in the address space so remember we quit program all over one up here and it left these two one-gigabyte holes and we decided we couldn't run program three because it didn't fit into either of these holes now we're going to have this map so we can be more flexible in how we use the memory so here's program three in program two here's program twos map and as we saw program two was mapped into the middle chunk of memory here but with virtual memory we can map program three wherever we want so here's program threes map we're going to take the first part of program three we'll map it up here we'll take the second part of program three and we'll map it down here so now program threes memories split around programs twos but it doesn't matter because this mapping allows us to map any part of program threes memory to any part of our real memory so we got this added flexibility from having these mapping and now we could fill up these holes and really use all of our memory so with each program having its own mapping we can put our data wherever we want and we can be more flexible about using these holes in memory so let's take a look at the third problem here keeping programs secure so here's program 1 and program 2 and remember what's going to happen program 1 is going to store your bank balance at address 2024 and program 2 is going to store your videogame at address 1020 four so we're going to use maps for them to put them in different RAM addresses so program one has a map and program two has a map program one is going to go to per address 1024 and its map is going to send the data somewhere so in this case we're sending the data over here to ram address 1 ok so it writes the RAM address 1 program 2 is also going to try and access address 1024 but it's map is going to send the data somewhere else so in this case it's going to send it over here to ram address 5 so now they write two different places because we have these mappings that make sure you even though each one of them is trying to access thousand 24 they have different mappings that send them to different physical locations in memory and they don't overwrite so neither one of them can touch the other one and we got this security they each have their own address space and they can't access each other's maps as long as we don't map them over each other so here's a question about program isolation so we just showed you how you can isolate programs here is this always good if you completely isolate the programs what's a downside of this so the pride of this if you do it entirely as you can't share data so programs often share a lot of data but if I have this mapping where they're completely isolated they can't share that now we can use the same mapping to enable shared data so say I have a mapping here that map's part of program ones addresses down here and another mapping that map's part of programs to addresses right here now they can explicitly share that same data regardless of where they need to access it and this is actually very common so if we take a look at this here here's the save dialog box from my mail program and here's the save dialog box from my web browser you'll notice these are exactly the same all of the code and all of the icons and graphics for both of these dialog boxes are the same so you want these to be shared between each program so the memory that represents the save dialog boxes that's going to be shared between the two programs so we need to make sure they can do the sharing so save dialog box fonts graphics all those things are examples of data that you want to share between programs
Info
Channel: David Black-Schaffer
Views: 511,983
Rating: 4.9540982 out of 5
Keywords:
Id: qlH4-oHnBb8
Channel Id: undefined
Length: 8min 46sec (526 seconds)
Published: Mon Jul 14 2014
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.