230 likes | 311 Views
T111. Memory Management. Computer Registers. very fast, very expensive, small capacity. Executable memory. Requires constant power hence short term. CPU Cache Memory. very fast, expensive, small capacity. RAM. fast, affordable, medium capacity. Flash memory.
E N D
T111 Memory Management
Computer Registers very fast, very expensive, small capacity Executable memory. Requires constant power hence short term. CPU Cache Memory very fast, expensive, small capacity RAM fast, affordable, medium capacity Flash memory slower, cheaper, medium capacity Hard disk drive slow, very cheap, very large capacity Non-Executable memory. No power required for retention hence long term. Optical memory very slow, cheap, medium capacity Tape drive very slow, affordable, very large capacity Re-cap
Address bus CPU RAM Data bus Re-cap • Available RAM = 2 to the power of memory bus bits • Eg. 20 bit bus = 1048576 Bytes /1024 = 1024KB
Re-cap • Address bus determines the amount of memory that can be addressed • CPU and OS can be factors • Other devices such as video cards can reduce the memory that can be used in 32 OS’s • Addresses in Hex
Why do we need Memory Management? • Can you think of a reason to need memory management? Process A RAM Process B
i 0700006b 5 Memory Management • When a programmer writes an instruction i=5 • The compiler will allocate an area of RAM (an address) for the variable i and then put the number 5 in this location • Suppose that the compiler decides that the variable i is to be stored at address 0700006b, the situation looks like this:
Memory Management • How does the compiler know where to put the variables (which addresses to use)? • How can it be sure that the location is not already in use? • How can it be sure that there is actually some RAM at this address? • In a multi-tasking operating system, how can it be sure that there aren’t two copies of the same program running and both using the same location to store the value of i?
Reducing the Address Space • Artificially reducing the address space available to a process • 32 bit addressing allows for addressing:00000000-FFFFFFFF 00000000 RAM 20000000 FFFFFFFF
01000000 RAM Process A 01ffffff 02000000 RAM Process B 02ffffff Memory Management • The operating system allocates a small portion of the address space for each process to use • For example – Process A will start at address 01000000h • Process B uses 02000000h as its’ starting address
01000000 RAM 000000 Process A ffffff 01ffffff 02000000 000000 RAM Process B ffffff 02ffffff Memory Management • Now with the processes separated in RAM, they can operate in their own section of RAM • Each process operates in an offset address so the situation looks like this:
Benefits of reducing the address space • Makes life easier for the programmer / compiler • Programs don’t clash with each other (provided they stay in their allocated space) • The operating system will only allocate addresses that actually have RAM assigned to them • Eg. If there was no RAM between 0a000000 and ffffffff then the operating system would not put a process at these addresses
Problem! • Multi-tasking operating systems run more than one program at a time • Example 1: • You have 16MB of RAM • You want to have ten processes loaded into memory • Each process requires 2MB of RAM • What will happen?
Problem! • Example 2: • A programmer is writing a program that requires a large amount of memory • The programmer doesn’t know how much RAM is in your computer • How do they know how much memory is available for the program to use?
Program A Program A’s memory area Program B Program B’s memory area Problem! • Example 3: • Program A has a fault and it changes another part of the memory that is used by Program B • This is likely to cause problems for Program B and Program A • This is a problem of protection
00000000 Virtual memory Process A 00000000 Actual memory (RAM) Operating system 7fffffff 00000000 Virtual memory Process B afffffff Virtual Memory • Each process “believes” that it is accessing the real RAM.
Virtual Memory Benefits… • The programmer’s life is much easier because they can now address any part of the virtual RAM without fear of conflicting with another program • Each process is running in isolation • Each process OR thread can address exactly the same virtual memory addresses whilst actually addressing different physical RAM
Problem! • You have 3 GB of RAM • Two processes each use 2 GB of RAM(with virtual memory this is possible) • How does the operating system manage to trick the processes into thinking that they each have enough RAM?
Memory Pages Wikipedia.org
Memory Pages • Memory is allocated in pages not bytes • Typical page size = 4,096 Bytes • Simplifies memory management
Memory Pages • When a process accesses a page of virtual memory, the operating system allocates a page of real memory to that virtual space • If a process does not address a page of virtual memory, then the operating system does not allocate any physical RAM at all to this virtual page • The virtual memory has holes in it! …but the process does not realise
Memory Pages • How big is the virtual address space? • How big is the physical RAM?
00000000 Virtual memory Process A 00000000 Actual memory (RAM) Operating system 7fffffff 00000000 Virtual memory Process B afffffff Review • Reducing memory space • Address offset • Virtual Memory • Memory Pages