Skip to main content
Loading Explore guides…
Memory Management Overview · Acedra
Home
Explore
Computer Science
Memory Management Overview
Sign in to save
Chat
If YouTube asks you to sign in, open the video on YouTube.
Watch on YouTube
Reload player
Transcript
526 segments
Copy
Hide video
0:10
OK. They're calling on the transcription. Have started.
0:17
On the previous warning applies.
0:24
OK. We want to move on to our next chapter. And don't forget that this is our next chapter and not the next chapter. If you have to, the same addition of the book that I have. Because remember, we are leaving out a couple of the chapters.
0:43
Specifically.
0:46
The chapter on.
0:48
He'll say synchronization. And the chapter on their looks.
0:55
I. He strongly encourage you to have a read of them because they are important. But it's just the amount of stuff that we can cover in a second year class. So we're gonna jump on to.
1:09
My edition, chapter 8. Which is memory management.
1:22
As we've been talking about for the last.
1:26
Six weeks or so.
1:29
One of the key tasks for the operating system, or certainly for a modern operating system. Is to. Allow us to run. Multiple applications at once.
1:43
Although of course, if you have been listening and not really run all at once. Because typically we only have one CPU. We'll just running them one after the other really quickly, to pretend that we're doing them all at once.
1:58
That we're doing lots of things. We have to have those things in memory. So if you think back to your LMC, you're. LMC program couldn't run until it copied itself into memory and runs from memory. It couldn't run from the desk. The same thing happens with.
2:14
How systems we need to get our processes, our programs into memory and all the for them to run. Now if we've got lots of those.
2:26
You've got lots of processes all running. Then presumably we are going to have to manage the memory because otherwise we're going to have a big issue with processes overrating other processes with memory accidentally being taken by other processes. We need to have something in there that allows us to manage the memory.
2:50
Ensure that these sorts of things don't happen. So what we're going to do is we're going to go through. Looking at different ways we might manage that.
3:01
We'll look at the. The advantages and the disadvantages and see if we can come up to something. That is a a modern memory management.
3:12
Approach.
3:14
Now, as I said, when we're doing this, remember we need to bring stuff from its storage medium. Discussed USB whatever into memory for it to run.
3:29
CPU can has to have it in there because.
3:33
The CPU can't access anything other than main memory and its own registers, so we have to have it somewhere that CPU can get access to that program and run it.
3:46
But we should bear in mind are these things here about how the CPU works, about only understanding addresses? It's it doesn't know whether they're data instructions, anything else. It's just getting memory.
4:02
We should also remember the relative speed of some of these things. So if the CPU accesses data.
4:09
And itself is stored in one of its registers. It takes 1 clock cycle.
4:14
That may mean memory because it's on a.
4:19
A different device because it has to get their via the bus because you have to request it, wait for that device to give it to you, send it back over to the bus. That takes, not surprisingly, more than one. Blocks equal.
4:32
And of course, while you're waiting for that to happen, you can't do anything else in the term for that.
4:39
And I'll see it again. For those of you that haven't been paying attention, it's in blue. So you probably want to remember, but that's called a stall.
4:47
One of the ways that we can speed that up is by using a cache between main memory and the CPU.
4:57
Ah, yes, Miller, there is a screen share except what happened, of course, was that.
5:06
Erin Screen Shield has screen and completely put him off my game so air and it's all your fault.
5:12
So yes, I have been talking about these slides.
5:17
Since we started this, so I'm up to there. I don't know where you guys are. I'm up to there already.
5:23
So slide 4, not too bad so.
5:28
I'll just quickly go through them just so that you know them.
5:33
OK, so as just the introduction and the objectives, so you've come and just on the the kind of stopped bit.
5:42
Sorry about that, I. I hadn't realized I wasn't sure on my screen.
5:52
Which means you have to put up with a copyright thing as well, yeah.
5:58
OK, so we want to talk about the memory management and.
6:03
One of the ways that we can approach this.
6:08
Bearing in mind that if we use our register.
6:12
I things will be much quicker.
6:16
Is that we can store.
6:21
Uh. Each process in memory and remember where it is using something called a base and a limit register.
6:30
Which are exactly the way they sound. So here we've got a memory map.
6:35
Uh, previous ones we've looked at have had memory starting with zero at the bottom. This one has zero at the top, just showing you it doesn't actually matter as long as you know which way.
6:46
And we're talking about, it doesn't matter. So as long as it's labeled, it's fine.
6:51
So what we're seeing is we've got multiple processes in memory. Each of them starts at particular place, it's base.
7:01
And has a limit, so it doesn't store the end. Piece of memory it says this is how much memory you are allowed.
7:10
So that allows us to change how much memory a process is given on the fly.
7:16
So.
7:21
One of the ways we check to ensure.
7:25
That a process isn't overrating someone else's memory, is it every time that's process tries to access memory, it goes through a WeChat. And it says is the address that you have asked for your greater than or equal to the base address.
7:46
If it's not, that's in here. Don't do it like it's not allowed to do that. It's not your memory.
7:52
If it is, then is it less than base plus limit? No. Then another errors generated, but if both of those conditions are acceptable, then you get the memory. This is called hardware address protection. We actually build that into the hardware with some registers and some hardware logic to see is it acceptable.
8:20
As I said. That means that this process will never ask for a piece of memory. It has to be between these limits. So us for anything outside those. It generates an error. I'm gonna see it. I mean the operating system.
8:36
So the operating system says Nope, not allowed that go away.
8:41
Feeds are back to the process and typically a stops the process as well because it doesn't want it to to enter feel anything else.
8:52
Everyone OK with the hardware stuff?
8:56
OK. So far so good.
8:59
Alright, so we've got all these things on disk and then after we've brought into memory.
9:04
To execute. And if you go to start more than one at a time. Clearly, we can only excuse me access or storage system. Serially, we can only do it one at a time, so if you try and access more than one, we have an input queue.
9:27
We saw a few weeks ago some very early operating systems that said OK.
9:33
Operating system starts at memory address 0 and. Processes come in immediately above that.
9:43
But think about that from the point of view of someone who writes the process.
9:49
They can't know where it's going to be booted into memory.
9:53
So what they do is they assume that their first address is address 0.
10:01
And leave it up to something else, the operating system.
10:07
Just sort that out.
10:11
So there's a few things that happen in there.
10:15
And again, this is one of those ones about pure garmins. If you're not pure grammar.
10:20
Worry about it and if you are a programmer, you probably know that's already.
10:26
So when you write up your gram and you might have done this with your LMC as part of the reason I get to do the LMC, you may have said, OK, I'm going to store some information in data location data 8 or you might have gone away bit further and you might actually have given data location 88 and name. That's one of the things you can do in the LMC.
10:48
So you've given it a symbolic name.
10:52
And put one and put two and put 3. And any time your program says input, one that goes Oh yeah, that's memory location 88.
11:00
So your source where it says input one is symbolic and then when you're code is compiled it's bound to the address.
11:11
It's dead easy for you to see because you've only got 100 addresses going on anyway.
11:18
So the binding is.
11:23
No, absolute, it's relative. So it says from the start of this process our binding is X number of bytes from the beginning. So I'd said you're storing input one in eight to 8. So binding says OK, well, it's 88 from the start.
11:39
Now it's important to remember that because. Put this process doesn't do is stop you overwriting your own memory. Some of you have found that you managed to do that. You could actually overrate your own program.
11:54
Self modifying Code is an interesting topic, but it's not one that you want to get involved in.
12:06
OK, so we have a symbolic source code. It's bound to. A relocatable address. Relative to the start and then when you load it into memory it says OK well, that process is going in at a particular address. So you add that one. So we go from. And put 1/2. 14 bytes from the start to, well, it's starting at memory location 74000. So we actually want 74014.
12:40
So there's lots of things happening for us to make sure.
12:44
That we are getting. The right piece of memory that we actually want and that can happen at a lots of different times.
12:55
If we know where the program is going to be in memory, we can just say OK, generate absolute code.
13:03
And this comes back to me reminding you that. Operating systems aren't just.
13:09
Window is on Michael. Whatever. You all sorts of operating systems. If the operating systems for the ABS computer in your car are written. They're never going to change.
13:22
So why would you bother? Having extra code that made you do that extra jump, why wouldn't you say? Well, it's never going to change, but. Put into the car and it's going to stay there. Why not just have an absolute address rather than have to worry about it?
13:36
So you great program, you say, yeah, it's going to be memory location it to eat just like you, a little man computer. It's never going to change.
13:44
For a more general purpose machine, though, it will probably change at the load time. In other words, when you load up what we don't know where in memory it's going to be, so we need something called relocatable code.
13:59
And indeed, if you follow that thought on.
14:05
Why won't you put it into memory? Does it have to stay there? Maybe we could move it about later. If it made sense to do so. And later on, we'll probably look at reasons why it might make sense to do so.
14:19
So.
14:21
We don't just look at it when we load it. We look at it when we execute it.
14:28
So there's a diagram there of Wayne. All these things might happen pale time, load time, execution time. But again, if you're not into programming, don't worry too much about it other than.
14:43
There's something there that says dynamically the dynamically loaded system library. Not ring a bell to anybody who runs a windows system.
14:57
So I got something to do with thread libraries maybe? No. No exactly, although with thread libraries off stalled and dynamically loaded system libraries.
15:07
What if I just said dynamically loaded libraries?
15:13
Anyone ever tried to load something? Sorry. Delight.
15:16
I was just gonna say is that like when you're programming and you're bringing like a JavaScript library or something? Yep. And never run something and said wrong version of DLL.
15:28
Wrong version of the dynamically loaded library.
15:33
So DLLS are things that the operating system has to. What are you to do? Stuff you quite often see it that you you have a program and it's the wrong version of the C Visual C++ DLL. And in fact, if you look at your system, you probably give it 10 versions of that on there because you get different programs running different versions.
15:57
That's if you're into your programming. If you'll know it's just an aside, watch out for it next time you see an issue with that DLL file on Windows.
16:09
What all this stuff really boils down to though? Is that we have a we think of our memory. So again I go back to the LMC, you're thinking of it as memory allocation, 88, but when you run that LMC program, you were running it in a.
16:28
Probably in a Windows browser.
16:33
Which is a process loaded on a Windows operating system.
16:38
So not only was the browser located somewhere in memory.
16:44
The program that you're running in the browser was located in a sub part of that.
16:50
Memory.
16:53
Which is a long way around seeing we've how we think if we are. Memory is. That's the logical address memory location 88 for input one.
17:04
The physical address might be entirely different.
17:12
And in fact, it most likely is different.
17:24
We've got another quirk.
17:27
Which is the CPU works on. The logical address because it's running your program and it thinks, Oh yeah, this is at 88 from the start of. The process.
17:42
The physical address that you want to ask for.
17:46
We'll be somewhere different. So we actually have a piece of hardware. Setting in there called the memory management unit that says Ohh you've asked for.
17:59
Location 88. Yeah, doorbird. I know where that is. So because you've asked for 88, I'm going to go and get you location 74088.
18:18
Later on, we might wonder whether or not we want to have all of the process all loaded at once.
18:26
So there might be another quirk that the memory management unit might say. Now I've not got that one.
18:32
But we'll come to that later when we do it. Just bear in mind that might have other jobs that it needs to do is well.
18:41
Which is by way of seeing that all of these logical address species that the process the program needs.
18:50
Is different to the physical address space.
18:54
Used.
18:56
And as I see, we use a memory management unit to do that. A piece of hardware. Why? Because we want it to go as fast as possible. If we're look at this simple thing that we saw earlier where we had a couple of checks, we're doing a comparison. Is it greater than or equal to? Then we're doing another comparison. Is it less than?
19:18
No, I am going to guess that when you did your early MC, you had to do some comparisons. And you had to work out which things were going. Which numbers were bigger?
19:30
Always than others to do your sorting, think of how many.
19:36
True, say so. Say calls that took.
19:39
To go get the numbers to compare them to give an answer and then to do something. Based on that. So we have very simple through or is it greater than or equal to a number? If you think back to your LMC and think back to how many CPU processor cycles of that takes. That's actually quite a long thing. It would probably take.
20:03
Let's call it.
20:05
Eat processor cycles every time you wanted to access memory.
20:10
Ohh welds anytime you wanted to get something from memory, it's going to be 8 times as slow.
20:17
The eat me or may not be accurate, but the.
20:21
And then the reasoning is.
20:25
So instead of doing it as a comparison in the CPU, we have a piece of hardware of called a memory management unit.
20:35
And what that's going to do is.
20:39
Take. The memory location you've asked for. And actually go and get the memory allocation, but you need.
20:49
And the simple example is the one that just showed you where you have a base register. Where it stops.
20:57
Just a complicated for you. If you use a memory management unit, the base register that we talked about. The referred to that as our relocation register, where it's relocated in memory.
21:10
So the process thinks it starts at 0, but you're not. You're relocating it somewhere on the memory management units. Stores that in a register.
21:19
The process never sees. It doesn't know anything about it, doesn't care and shouldn't have to care. It's all done. Five it always. When you run the system.
21:38
So you start to run something.
21:44
It's.
21:46
Put through the MMU. To go and get. The correct space and memory.
21:54
But he also we work for you.
21:59
To be half the load everything at once.
22:02
Not all these things you get your source program you which creates an object module, but you might be using other. Modules we've talked about APIs before new if you don't remember what they were.
22:15
Even if you don't remember the detail, you remember that there are ways of giving you extra functionality. You know for moving a mouse or clicking.
22:24
You may also have system libraries or DLLS. All of those form your program.
22:33
But if these are physically separate systems.
22:38
Do you actually need to load them all at once?
22:41
Wouldn't it make more sense to only load them as and when you needed them?
22:47
So if you load up. What?
22:51
What has a DLL that spell check DLL and I know that it's separate because it uses the same spell check. Library for. Excel, PowerPoint and all the other Microsoft things.
23:06
But what if you're not doing a spell check?
23:10
Why would you load everything into memory at once? It doesn't sound.
23:16
I'm very.
23:20
It sounds like a way of wasting a lot of time and a lot of memory.
23:23
So don't.
23:27
Keep the stuff on desk.
23:30
And are relocatable load format. Or if you prefer. I dynamically loaded format.
23:45
And all the load and when you need it.
23:54
Why not?
23:58
So you end up with something called dynamic linking.
24:04
So static linking is where you have everything at once and everything put together and it's all one big lump.
24:10
We're talking about dynamic linking route. We wait until we execute.
24:15
So if you call the spell check, you don't know which memory address it is. You know that it's gonna be in this dynamically loaded module and you decide that when you actually run it.
24:26
What you need to do that you need to know where all the modules are. So we have something called a stop that just says OK, hold it all. It made me resident library teens. This one here. This one's there. This one here.
24:40
That also gives us the option of sharing those routines.
24:45
As I said, the spell check stuff can be used by worked on Excel and PowerPoint, so if they're all using it, why would you load it more than once? Why wouldn't you just load the library once and call it lots of times?
25:02
So. When you load these things, it doesn't actually load them. It just says OK, I'll remember that you might want that. That's one of the things that you see you will need will be. Will see.
25:14
It simply.
25:16
Loads of stop and says Yep gonna have this only when you run it does it actually check to see if it's in the memory address.
25:26
And the memory space. If it isn't, it loads it in.
25:35
Which is a very efficient way of using these things.
25:41
Of course, as I said earlier, you may have seen issues where.
25:47
The library changes, it may have new functions, it might change the way you call other ones. And I suggested you may have seen that from some programs that you have learned that it once had different version. Of a DLL to be installed like. I don't know how many times I've installed the visual C++ libraries.
26:08
I don't know how many versions of go on my system.
26:12
Because different programs were written with different versions in mind.
26:20
Everyone happy with that?
26:28
OK.
26:31
Well, let's ask ourselves another question.
26:34
If we're happy to do this.
26:37
I've got your happy. To have.
26:41
Stuff loaded in only when we use it.
26:48
Well, maybe once we've used it.
26:51
And we don't need it anymore. We could. Junket.
26:57
Take it out of memory so it's not taking up any space. In other words, maybe we could swap it.
27:04
Swap it back out.
27:06
And only bring it back in when we need it.
27:10
It means that we need some backing storage and that needs to be reasonably fast. And again, I would suggest you have all seen times where you've been running something. Your system is slowed down and it's because it's bringing something in from a slow backing storage.
27:29
And.
27:32
We can address some of that by prioritizing some things that we would want to bring in more quickly.
27:41
So it's it's a good idea. It's saves memory, but it can be slow. Just to bring stuff and from memory.
27:53
But he was be floor.
27:57
We've assumed that we're bringing these in and running them and giving them a memory address. Well, for swapping them back out.
28:07
I'll be guaranteed to come into the same memory address.
28:12
Give me a thumbs up or a thumbs down.
28:19
And you give up arms down. But I'll say no.
28:22
And you're quite right, if it if you were guaranteed that they came back to the same memory address, there be no point swapping them out. Because we would have to keep that memory address when coming back in anyway, and we couldn't use it for anything else. So the whole point would be gone.
28:38
So we don't know where we're going to come back into. So we might need to. Update as we go along.
28:47
So what we end up doing is we swap out processes to backing storage and typically we think of that as well disk.
28:57
And then swapping other processes.
29:01
OK, so remember it's processes, so it could be the the controlling program, it could be the DLL's, it could be any of the processes that we are using. The point is, we can swap them out and swap them back in. Eyes and queen, we need to.
29:18
And of course, that brings up the point about when do we need them.
29:23
And that's down to.
29:26
What we've been talking about before with CPU scheduling.
29:31
The CPU says I need this process. With any to figure out if that processors and memory and if not, we need to swap it back in. As I said, that can take ages. You've got an example in the screen there.
29:45
What example? And. Of the time that it takes to swap in a process.
29:53
And I'll leave you to work that out. Or when hard disks for nowadays.
29:59
The numbers make me different.
30:02
But the concept is the same. It takes much much longer. Which is partly why, if you're replace your help desk with an essays DC, an increase in speed for your system overall because it's swap time for your SSD is much lower than it is for a hard disk.
30:24
That's assuming that you can get it from your backing storage immediately. What if other things are doing it? What if you're CPU needs a process just as one of your other processes has asked for a failed to be loaded?
30:41
And that's where I'm saying we might want to prioritize things for I/O to say, OK, well, maybe.
30:50
Maybe we want some. I ought to be higher priority than not.
31:03
We tend not to do this on operating systems for other types of devices, so if you've got an Android phone or an iOS phone. You don't have backing storage as such, you've just got. Memory.
31:19
You've got, you know, 120 GB of your phone has.
31:24
Men's old enough that it has 16.
31:30
The point, as you don't really have main storage and backing storage, you've just got memory. You've just got storage.
31:40
And if you know anything about that kind of storage, you'll know that it has a limited number of rates equal.
31:47
So you want to minimize that, otherwise we're going to use up our memory really quickly.
31:55
Instead of forcing them out, we ask apps to just say yeah, OK, I'm not needed anymore.
32:06
Android approaches it slightly differently, and it simply says OK, we don't have much memory left. I'm gonna terminate you, but I'm gonna remember this state that you were in, so that when you come back in, I'll just start you exactly where you were.
32:24
Everyone happy with that? Before we move on?
32:29
OK.
32:31
In that case, let's think about how we are actually going to allocate the memory then, so we've brought stuff in. We need to put an end to memory and the question is.
32:43
We.
32:48
Who do we allocate that memory between all these processes?
32:54
Well.
32:57
One we have doing it similar to say, processes must be contiguous in memory in contiguous as a fancy name for. Altogether, one after the other in memory. So there can't be any gaps in there.
33:12
For one block of memory for the Paul says all in one line.
33:20
And as we've said before, we use our geolocation light register to see if we have the staff has. And.
33:28
To check that we're not going to overrate anyone else's. And memory by trying to access the years rather than hours.
33:38
So we do that as we're working.
33:41
And.
33:44
We make those checks.
33:48
So same as we looked at before, we check that it's. Within the limit and then we relocate to wherever that process is sitting in memory, not what's where we've put it in the memory map.
34:08
As it stopped there.
34:11
Or does it start there? What does it start there? We don't know. But the relocation register does.
34:26
So what can we do? We can. Have a memory map. We'll have our operating system in there as usual, and then we can start loading in processes up to.
34:38
The.
34:40
Size of memory.
34:43
Purchases themselves will be different sizes, so we don't know how many will be able to load in. It will depend which ones.
34:52
The payload.
34:55
Too much space. They're going to take. So here we've got in that first diagram process 5.
35:01
Relatively small compared to process 8, which is much larger than process too.
35:08
When we finish. With the process. So process 8 finishes running, it's going to the exit.
35:15
Member back to our diagram, it's been running. It's going to ECS. It's terminated. So clearly we don't need that memory anymore. Process he's 25, are still running. They're still in memory, but now we've got a whole in memory.
35:30
And the next one that's allocated by the scheduler is process 9. So we've got a nice hole in memory, so we put. Pearl says 9. And to the first piece that we can.
35:44
Simoly process 10 comes in.
35:48
And we just put that into the start as well.
35:55
That's a very simple example.
36:01
Small number of processes and here we've got lucky that the new ones that were bringing in fit.
36:09
What if it had been processed to that it stopped and processed 10 didn't fit in that space?
36:16
What if process 9 now stops?
36:20
It will get 510 and two still running, but we want to bring in process 8 again.
36:27
And for.
36:30
Even if process five then stops, so process is 5, nine and two stop. And we want to bring in purchase, eat. It's too big to fit in the space where process is 5 and nine were. So we've got all this issue of.
36:47
Holes and memory. We don't know how big those holes are.
36:52
Because it depends on the process that we're running. There may not be together.
36:57
So we've got holes all over memory of different sizes.
37:02
So we're gonna need to think about. How we allocate processes into those spaces, those holes in memory, and sure enough, there are some approaches that we can take. They can just say, OK, go through memory, look for all the spaces and the first one that's big enough chocolate. Next processing in there.
37:25
That seems wasteful. Maybe what we could do is look through all the spaces and memory and look for one that's.
37:34
Closest to.
37:36
The process that we know want to load in. So we waste as little memory as possible because if we load something in, we clearly have a smaller space left, and that's even more unlikely to be able to to fit another process. So maybe we say, OK.
37:53
Or for best fit to have the smallest leftover hole.
37:59
That seems efficient, but actually when you play with it, you start seeing lots of small holes and memory being left. So maybe a better way is to actually go for the worst fit. To say, OK, let's put something into the worst possible place. In other words, the one that leaves the biggest space leftover.
38:20
The idea being that that big space left over is probably big enough for another process. There was a small space left by best fit isn't.
38:37
Those different ways of approaching it and different operating systems have used all of those.
38:42
What most modern operating systems don't use this approach at all? Because you end up with fragmentation.
38:53
There's two types of fragmentation. The holes and memory are.
39:00
Big enough.
39:02
To fit the pure says that you want to load, but the problem is they're scattered all over memory. That's called external fragmentation. We do have enough spare memory, but it's not altogether.
39:17
Or we could have internal fragmentation.
39:22
And that's the one it was speaking about with best fit sort of with. Put a process into a space that exists but the but that's leftovers. Not much use for anything really. And that's internal fragmentation. And we can actually do some analysis. We've done this before in other approaches. The analysis for this. Tells us that if we allocate N blocks, we lose half of them to fragmentation.
39:53
So what we could do is do something that some of you might be familiar with from your hard disk, because the same thing happens with hard disks. You stole data all in them. You delete data, you store them all data. That leaves holes.
40:08
So every so often you might want to.
40:11
Compact your hard disk may want to bring all the storage into one block. Maybe we could do the same with memory. Shuffle all and bring it all together to put all the free memory in one place. And of course that's possible. We do need to dynamically. Hey, allocate memory, but that's fine. We've talked about that already.
40:33
But of course it will take time.
40:37
They're going to shuffle all the memory. Of course. That's going to take time. So you're gonna have to stop.
40:44
Connor Gates, wouldn't you compact? Our desks. That's called defragging. Yes, defragmentation.
40:58
So we end up with memory fragmenting. We end up with backing store fragmenting. And of course, backing store was where we said that we were going to put our processes when they weren't being used. When we're swapping them in and out.
41:21
Some of you might not have defragged your hard disk because as James has put into the chat, some systems just do it now and don't tell you.
41:30
And I think the one he's put in is an automatic weekly optimization for your hard disk.
41:40
Used to be you didn't manually. There was A and you still can actually. The most systems go why do we need to tell?
41:49
And nothing against 70 year old grannies. But why do we need to tell our 70 year old Granny to optimize their hard disk? Shouldn't we just do it for them and the answer? Almost always as yeah.
42:01
And I see it almost always cause my system tends to do it pretty much 3 minutes after I start an online lecture and my computer slows down really really quickly.
42:14
OK. Is everyone happy with that?
42:18
OK, if everyone's happy with that, then what? I think we will do.
42:24
Is.
42:26
Take a break.
42:28
Because that's been.
42:30
Have to 8 minutes of a slight there, so we will take a break. Go take a we walk.
42:37
And.
42:40
We will be back in what you want 5 or 10 minutes.
42:46
Moscow 10.
42:48
And we will be back in 10 minutes, says Connor.
42:53
OK, see you then.
53:33
OK.
53:36
Ah. Can't believe it took me like team to make a cup of tea.
53:42
And minutes to make a cuppa tea. Who thought?
53:47
Anyway gills.
53:52
So how do you get one of those? You think?
53:54
All right. And you get one of those cups. Yeah. Good.
54:01
Ah.
54:05
I just use escalate a lot of tea. It's not even going to well, I don't.
54:15
Right. OK. So.
54:20
Before we paused, we were talking about memory management. And different strategies anybody thought of any questions?
54:30
Yes, assessment isn't the same room as usual J318. They always.
54:36
Sorry. No, it's about that question. But I was just wondering about the fragmentation. So at the minimum, I talked about logical memory, it's not actually physical. Was it because the dimension you can't move that around? No.
54:48
No. We are talking about physical because what we're talking about is this thing here.
54:55
Because what we're seeing is if at.
54:59
This point.
55:00
We hide.
55:03
I feel says 5 stopping.
55:09
So we now have something like this.
55:13
Where we have OS.
55:16
There was nine.
55:19
And process too.
55:21
And we want to bring in process here again.
55:25
We've got enough space. But we don't have enough space contiguously. So the the real memory has fragmented and we can't do what we want to do.
55:37
And that's where we could compact it. We could move all this up, move that up to just under OS, and then move that up to under P9.
55:47
But we are talking about actual real memory.
55:55
Yes.
55:56
Although it's quite possibly the worst John diagram. Ever.
56:04
Any other questions just now?
56:13
My example there was external fragmentation. We do have enough memory space but not contiguous.
56:24
OK so.
56:26
If we have that issue and part of the issue is because. I'm purchases are different sizes.
56:35
Maybe what we could do instead? Excuse me.
56:42
Is think about the processes themselves and think of how they're made-up.
56:52
So we've talked about the different bets going into making up process.
56:59
And we could go further if we wanted and again.
57:03
Caveat programming applies if you know what all these things are, good. If you don't trust me that they are all things that you get in programs, maybe we could treat them as different. For memory management purposes, maybe we could just use all these rather than have them all in one process. Maybe we could say ohh yeah, well, we'll have some memory for the simple table. It's memory for the stack and some memory for the square root function.
57:33
And instead of justice bringing in a process, we could cut it down, make it smaller so that we can bring in bits of process.
57:43
So we've got program that goes 1234 but and physical memory. It goes 1/4 lots of space 2/3.
57:55
The same stuff would work.
57:58
We still have. I'm. I stop and end of particular bits, it's just the segmentation table will gets a bit trickier.
58:14
So we say we have a base. We are the part of.
58:20
The system starts. We have a limit to see how long it is.
58:25
And. I'm.
58:28
Whoops.
58:30
And we have that in memory itself. So we actually have to remember where this segment table is because that can change as you run in processes and they have different bits to them that will get bigger.
58:46
So we have a segment table itself stored in memory.
58:52
Allows us to do the same protection, although it gets a wee bit trickier.
59:00
So instead of saying, we're going to a particular memory address and checking whether it's within the limits that we hide.
59:08
We have to split up that address and we're saying well, it's segment 1. An address. D We have to check that the segment is there.
59:27
Only if it is to be check whether it's within. The address space.
59:33
And actually go to memory.
59:37
So the same can idea, but it's just getting a wee bit more complicated.
59:42
To try and and cope with.
59:47
Multiple parts to a processor.
59:51
So maybe we could take another approach rather than using segmentation. Where we don't know the size. Of the blocks of memory. Maybe we could use something else which we will call paging.
1:00:08
And the idea with paging is that instead of leaving it up to the processes to decide how big they are, chunks of memory are.
1:00:17
We'll get the operating system.
1:00:21
To do it for us. So the operating system will split up memory into chunks called pages.
1:00:34
Sorry.
1:00:36
Let me rephrase that.
1:00:39
This is an extra step.
1:00:43
The operating system will divide memory and T. Chunks called friends.
1:00:50
I'm going to say it how big the frame is.
1:00:54
And that size is this usual thing that I've been talking about. It depends. It depends how much memory you have. Because remember, we have to keep track of all these frames.
1:01:05
So the usual pros and cons apply.
1:01:10
The.
1:01:11
I am.
1:01:13
The temptation is to split into small frames, because that means you don't waste memory, but then you have to keep track of lots of frames.
1:01:23
If you want to keep track of your frames, then you have to make the frames larger, which means you'll get more chance of wasting memory.
1:01:37
A logical memory that we've spoken about is then split up into pages so the process is split up into pages.
1:01:47
So physical memory and to frames logical memory and to pages. These are the same size.
1:01:56
And what we do is we keep track of. The free space that we have in memory keep track of any frames that are free.
1:02:06
And if we want to run a program with impages, we need N frames.
1:02:12
The nice thing about that is we don't lose much memory. We will use some, we'll lose some, but not much.
1:02:21
Ohh then he does a page table. So whenever the process asks for a memory location in Page 6.
1:02:30
This is OK well, that's in frame 82. So we've got a one to one link between the page and the frame.
1:02:40
Let's see. Still some wasted space because it's unlikely that your process will neatly fit into chunks. Of whichever size you've chosen, so you'll always have some space. Almost always have some space. Lost at the end and in fact. The average internal fragmentation that you have corners, drinking wine? Ohh, I'd. I'd never thought about drinking wine.
1:03:07
That's why Wayne.
1:03:09
There's no way.
1:03:11
Looks like we went to me. Is green tea.
1:03:18
The average waste is half of whatever size your pages.
1:03:28
And then we just need to set up a translation scheme.
1:03:32
You have a page table.
1:03:35
Most pages and that contains. Where that is in physical memory. And an offset. So we'll get start over and how and the offset within the page.
1:03:50
So every memory location is split up into 2A page.
1:03:57
And you have a page table. So whenever the CPU asks for memory allocation, we split it up into two. First thing we do is the page. We then send that to the pH table, which says oh, that's in frame. And it gives A-frame number.
1:04:14
I mean then add that in to the logical address, so that's a bit within the page slash frame.
1:04:21
To give us our physical address in memory.
1:04:27
So we can end up with something like this. We've got the program, so logically it thinks of itself as 4 pages, page 0123.
1:04:43
But to go along with that, we create a page table where we say that if you ask for page 0 that's actually in frame one of physical memory.
1:04:56
Page 1 is in frame 4.
1:05:01
Age 2 is in frame three and Page 3 is in frame 7.
1:05:10
OK, so we have. A link that there's no need for these to be in order.
1:05:16
There's no need for them to be contiguous.
1:05:38
And where we then ask for a particular piece of memory. So here we'll still get our four.
1:05:45
Heggies.
1:05:47
Or logical memory. And we've actually identified the memory allocations 0123.
1:05:58
So page 0.
1:06:01
Is in frame 5. And that'll put memory location.
1:06:09
Starting at.
1:06:12
Stream 20.
1:06:16
So the offset within the page slash frame stays the same, but they can be scattered all over physical memory.
1:06:25
We OK with that before we move on?
1:06:29
Think so?
1:06:31
Yeah Amazon.
1:06:35
I'm not seeing any. The result to absorb it's just. Ohh yeah. I mean don't get me wrong. Thank you. Have questions at a time. It's. Yeah, and. And you know, I'm doing this over an hour or two. You will have to go back over and look at it. And of course, there's plenty of questions to help you do that.
1:06:54
And that's part of the reason the first thing I always do is ask if there's any questions from the previous week, cause something might occur that doesn't occur as you're doing it. So yeah, if you have questions, write them down. Ask me next week or stick them on teams, the teams discussion and I'm happy to answer them when I see them on teams as well.
1:07:15
OK, so we can do some calculations.
1:07:21
As an example, we've decided to have a 2K page size.
1:07:27
If the process size is this.
1:07:31
That divided by that gives us 35 pages plus 1086 bytes leftover.
1:07:39
If a page size is 248 and we'll get one weight 6 bytes leftover that still has to go into a page so that page wastes 962 bytes. So you've got a top and a low. And for that.
1:07:55
You could waste almost all the frame by well I needing one bait. Or you could waste almost none of it by only having one bite leftover. So as I see, on average you'll lose half a frame.
1:08:09
So I found average was half A-frame. You might want to make the frame size smaller but. You have a bigger page table and there's more to track.
1:08:23
That takes time and it takes memory itself, so it's about finding that balance somewhere.
1:08:33
Next thing we have to do, of course, is figure out where we're going to put these pages in memory. O we're going to need to have a list somewhere of the free frames that we have in memory. So if this process has four pages and we've got a free frame list that says we've got these 5 frames free. Which sounds like a tongue twister and absolutely his.
1:09:00
What we do is we allocate them. And there's no particular reason to allocate them in anything other than the order they come in. Paid zero goes to Train 14, page one goes to frame 13, page 2 goes to frame 18, and Page 3 goes to frame 20. Add them into a page table and we load them into memory.
1:09:23
Where we've said the go.
1:09:28
And we still have 1/3 for him.
1:09:33
#50.
1:09:36
So as I say, we need to store these somewhere. The page table itself needs to be stored that has to stay in main memory.
1:09:45
So every time we decide that.
1:09:49
Yeah, page size is going to be smaller. And Members, perils of tours were, if we were half it, we have to double our page size, which means we take double the memory. So that's part of that. Trying to get a a sweet spot thing again.
1:10:04
Regardless, we've got a couple of registers, one points to the page table and one says how big the page table is.
1:10:15
Because we have that we have to access memory twice.
1:10:21
Because we need to go to the page table and figure out which frame we need an actual physical memory.
1:10:30
We need 2 memory accesses. We're doubling the time it takes to access any piece of memory.
1:10:37
We can use all the things we can add in more hardware, something fancy called associative memory.
1:10:45
But of course that's expensive. So that's the other trade off we make. Of course, we can make things faster, but it's going to cost us more money.
1:10:59
Always the trade off with me.
1:11:05
And you kind of intuitively know that.
1:11:09
If money wasn't an issue, you wouldn't have a hard disk.
1:11:13
You'd have a two terabyte main memory.
1:11:17
And just keep all your stuff in main memory all the time.
1:11:20
Why would you do anything else?
1:11:23
But. Course there's an issue and having two terabytes of main memories quite expensive.
1:11:29
So you don't you make the trade off, you have some amount of memory, some amount of this.
1:11:36
To painting and what you're doing, you might make a different trade off.
1:11:42
And.
1:11:44
One of my surface machines has got 4 gig of memory ones get 8. Some of you guys doing gaming will have 16 or 32 gig of main memory.
1:11:53
Because you've made that trade off and you say no, I'll I'm happy to spend more to get a faster system. And that's where it comes down to in the end, how much you're prepared to spend.
1:12:03
You get a faster system.
1:12:13
And.
1:12:15
So we can add an extra hardware associative memory, also called translation lookaside buffers.
1:12:25
Wonder who come up with that one?
1:12:28
And TLB's store address space identifiers, ASI's.
1:12:34
And the good news for you is they're both in blue. So you may remember both of that.
1:12:43
Some of these are.
1:12:46
Unique to the process, so you need to get rid of them each time you go through. And. Whenever you change the process, whenever the content, whenever you change the context, you need to clear that out as well.
1:13:02
We meet.
1:13:04
Some of this and.
1:13:07
So we might use.
1:13:10
Translation lookaside buffers in the same way as we use a cache might say OK well we can't have everything in this, but maybe we can have some of it.
1:13:20
And then general, you run the same process as again and again.
1:13:26
Something's run a lot. Something's run. Hardly ever. So we could maybe put some of that in there.
1:13:33
And. I'm load the most recently used processes so that you get faster access.
1:13:44
And if it's something that is particularly important, I don't know, Windows might decide that the move a mouse process is important. You might say, yeah, but gonna have those permanently in there. And it's up to the operating system.
1:14:03
What does this give you? Or associative memory simply says.
1:14:08
It doesn't have to go through each line and turn the way you would in memory to find a a link. It can search all of them at once. It's a parallel search, so you just. And the page number and you get out the frame number immediately.
1:14:24
So you end up with something like this.
1:14:30
The CPU asks for memory which you split up into the page. And. The actual memory address within that page you send that page to the TLB, which you've remembered is. Translation lookaside buffer?
1:14:47
That can search the page table or the TLB rather. I can see actually that buffer in parallel, so you got lots of things here.
1:14:59
And if it's a hit with A-frame number that gets added into the frame and you can go and get memory.
1:15:07
But we've actually drawn 2 hours here as well as going to the TLB, we send it to the original page table so that you can be looking through the page table in case it's not in the TLB.
1:15:19
But you only need that if it's a mess. Efforts are have we can get the fast physical address if it's a mess, we still get the slower physical address.
1:15:33
Ohh, that says it takes longer to do this.
1:15:39
Not your life.
1:15:41
The question then becomes. How much slower?
1:15:45
And not surprisingly, we can calculate that and your favorite Greek. Letters are back. I know you'll be. I know you'll be jumping for joy to see alpha. Make a ariton. Doesn't it's been too long since we've seen an alpha.
1:16:04
Again, it's one of those ones where. Remember that we have an effective access time. Remember that we can calculate it.
1:16:14
Remember what it depends on.
1:16:17
Which is basically how often. The pages in here and you get a hit.
1:16:25
But you don't need to remember.
1:16:28
The actual formula you can just trust the people that know about this stuff.
1:16:38
Here we've got an example where.
1:16:43
We've taken some. Typical values or seeing it takes 29 seconds.
1:16:50
To search.
1:16:52
The TLB. As opposed to 100 nanoseconds.
1:16:59
For memory access. And we can plug that into. And.
1:17:08
Equation and say that if we expect 80%. Over accesses to be in the TLB.
1:17:19
Hey sorry.
1:17:21
If we expect all the 80% of our accesses to be in the TLB, that means it takes 120 nanoseconds. So we've gone up from 100 to 120.
1:17:33
If the heat rates better, if it goes up to 99%, then it only takes 101 nanoseconds. So we've increased it by 1%.
1:17:43
Bottom line, we've got a an equation. It tells us the effective access time based on these things and you can plug it into your system to understand.
1:17:53
Who will work?
1:17:56
Everyone happy with that?
1:18:01
I think so.
1:18:05
It needs some digestion time. Yeah, and you'll have a better idea once you do the chapter tests, so that will give you a better idea of how much you're you're understanding it and how much you're not. So make sure you do those and make sure you can get through all of them.
1:18:21
OK, so for doing all that we need to remember that we need to protect memory, in other words. I am.
1:18:32
Sometimes we don't want memory to change.
1:18:39
We might see that some memory is read only. Some memories retreat.
1:18:44
So in the same way as we looked at fail systems and we looked at.
1:18:50
What you're allowed to do with fails. We can do the same sort of thing with memory.
1:18:58
And one of the things we can add to is a valid or invalid bit that simply says. Does this piece of memory belong to this process? As it valid.
1:19:16
We end up putting an extra column into the page table.
1:19:24
Remember, we've got we've got logical address with the process that has page table, the page table.
1:19:33
Tells us which frames things are in, but the frames may not all be there.
1:19:38
The ones out there are valid, the ones aren't are invalid.
1:19:47
We also have to cope with shared code these DLLS.
1:19:51
The fancy name for code that you can share is reentrant.
1:19:57
Because it means that it doesn't make any assumptions on the code.
1:20:03
So we can share pages as well. The spell checker that I was talking about.
1:20:08
Or we can do the opposite. We can keep some stuff private.
1:20:14
Even though you're running the same spell checker, you're running up in a different document, so you've got private code and data, but running the same. Could to do the same stuff edit. Spell check.
1:20:30
Move the mouse.
1:20:33
Because you've seen it move on here and I'm seeing it move on to teams system.
1:20:37
But we're using the same code to make that noise to make it appear.
1:20:42
So uh, table gets slightly more complicated.
1:20:51
Here we've got a couple of processes.
1:20:56
We have.
1:21:03
We've got the same.
1:21:11
Would being run.
1:21:14
Which one has played like cases and frames 3-4 and six?
1:21:20
Ohh, we'll get two different sets of data.
1:21:25
And their and their own unique pages.
1:21:34
So see processes running the same code. Using different data. So we need to keep track of that. As well.
1:21:46
And you will all be relieved to know.
1:21:49
I mean, you disappointed to know that was no more? Greek characters be relieved to know that we are done.
1:21:56
So that any questions about any of that just now?
1:22:04
Hey I had one there but the the free frames going into the logical part I think you said there was no order in the queue is that? It was a while back.
1:22:18
You know the bike. Yeah here. Yeah, the free frame, the free frame last year. What I said was. Yeah. So what I said was we'll get a new process and he's 4 pages that's already split up into 4 pages. What we're saying is we can, there's no need to choose in this free frame list because I'll just going into memory anyway. So in this particular case, it's just chosen them in the order they come. Page 0 went to three and 40.
1:22:42
OK. Page 1 went to page 13. There's no need to make that contiguous, so we don't need to put it in order. We don't need to do it in order in memory. It doesn't makes no difference whatsoever. We can just assign it to the first floor that we have, so the few dream list can just be really simple in terms of its storage. We never need to sort out. We never need to think about it. It's just a list. Yeah. So I'm just trying to overcomplicate it basically.
1:23:07
And in the nice thing about pages and frames is it takes away a lot. That complication part they're using the move to Yeah. that rather than using the previous ones that we talked about is. This does involve a wee bit extra work, but it's less complicated in terms of keeping track of things in terms of how big things are in terms of how we fit them into memory. Here we've just got a whole bunch of stuff that's all the same size, and we just Chuck it in and just keep track of where it's chopped
1:23:33
in.
1:23:34
Which as you can imagine, this is as a nice way to do it. He's enough.
1:23:38
Umm.
1:23:40
OK. That's all I had. Thank you.
1:23:43
Anyone else?
1:23:56
Yeah, I'm not seeing any questions. So I am going to stop their calling.
Here to help you learn
Sign in to keep a private conversation with your course tutor.
Ask me anything about the material…
Sign in to chat