Presentation on theme: "MEMORY MANAGEMENT Y. Colette Lemard. MEMORY MANAGEMENT The management of memory is one of the functions of the Operating System MEMORY = MAIN MEMORY ="— Presentation transcript:
MEMORY MANAGEMENT Y. Colette Lemard
MEMORY MANAGEMENT The management of memory is one of the functions of the Operating System MEMORY = MAIN MEMORY = RAM
Memory Management Each process must have enough memory in which to execute A process must not encroach on the memory space of other processes nor have its memory space encroached on by other processes The different types of memory must be used properly and effectively The AIMS of memory management
Memory Management Note that as processes execute the CPU either reads data from memory or writes data to memory
Memory Management How is memory organized to facilitate these objectives?
Memory Management Memory is partitioned into large sections of fixed sizes called segments. The segments are separated by boundaries. The segments are used for the different types of software.
Segmentation Memory is like a fixed set of slots (segments) Each segment has a unique address RAMRAM
Memory Management When the system is booted the O/S is loaded into memory first. It occupies the lower level of memory. O/S
Memory Management The drivers needed to control the hardware are next loaded into memory O/S Drivers
Segmentation The rest of the space is normally used for application programs ( user processes ). One entire process is loaded into one segment O/S Drivers Process 1 Process 2 Process 3
Segments Partitions may all be of the same size or the sizes may vary. The size(s) is(are) fixed however. They must be large enough to hold all the data for an entire process
Memory Management The O/S is responsible for : -Keeping track of which parts of memory are currently being used and by what process -Knowing which parts of memory are free -Deciding which process is to be loaded into memory when space becomes available.
Segmentation The segmentation approach sometimes causes difficulties because of the inflexibility of the partition sizes.
Segmentation Lots of space is wasted. Lots of space is wasted. Most programs do not use up the exact amount of the segment O/S Drivers Process 1 Process 2 Process 3
Segmentation -Sometimes the total of the left over space is enough for another process but it is fragmented and processes can not share partitions.
Segmentation -Segmentation therefore imposes a fixed limit on the maximum number of processes which can run at any one time => # of user segments => # of user segments
Segmentation II To counteract this the O/S can use variable partitioning Do not predetermine the sizes of the segments; give a process just the memory it needs. Do not predetermine the sizes of the segments; give a process just the memory it needs.
Segmentation II O/S Drivers Process 1 Process 2 Process 3 VARIABLY PARTITIONING Create the partition when it is needed, give just as much memory as will be required.
Segmentation In the long run this still results in holes in the memory as when a process is finished, the next process will not need the same amount as the previous one. If only a part of the memory is used by the next process, some is still left unused resulting in some fragmentation over a period of time
Segmentation A Solution – The O/S can periodically pause all tasks and compact memory so that all the free space falls in one continuous area which can be used by other processes.
Segmentation The major disadvantage of this approach is the time it takes to compact memory. Remember that this is not a user task so it takes up valuable time which could be used in constructive processing.
Memory Management For best use of the resource (memory) we therefore need another approach to managing RAM
Paging This is an alternate approach to partitioning. Here memory is divided into very small frames as compared with segments. A single process would find that all its data cannot fit into one frame. This is an alternate approach to partitioning. Here memory is divided into very small frames as compared with segments. A single process would find that all its data cannot fit into one frame. Frames are in the order of 2 9 to 2 13 bytes in size. They are fixed in size for a particular system.
Paging The program and data therefore would also have to be partitioned so that portions of them can fit into the individual frames. The program and data therefore would also have to be partitioned so that portions of them can fit into the individual frames. The programs (and data) are said to be divided into pages. The page size would be the same as the size of the memory frames.
Memory Management Program & data pages Main memory frames
Memory Management PAGING has proven to be quite effective
Memory Management Questions ?
Memory Management Despite the best efforts of the operating system though, it sometimes finds that it needs more memory than is available because of the number and size of the processes being run.
Memory Management One solution is to add more memory to the machine This could work BUT … - memory is expensive - only a little part of the data in memory is actually being used by the CPU at any one point in time so spending more money on memory could be unnecessary
BUT … Because memory is finite, AND because the user code is broken up into pages, AND because the CPU can only operate on one page of data at a time anyway… DO WE REALLY NEED TO HAVE ALL OF A PROGRAM IN MEMORY AT ONCE?
Virtual Memory Only the pages presently being used, last used or about to be used need to be in memory. The other pages can rest in a section of the hard drive awaiting their turn.
Virtual Memory To the user it appears that the entire program is loaded into memory but it is not.. This is VIRTUAL MEMORY This is VIRTUAL MEMORY
Virtual Memory Disk space is relatively inexpensive compared to RAM. This is a better approach than adding more memory because ….
Virtual Memory Virtual memory therefore involves using a portion of the disk to extend the size of RAM.
Virtual Memory When the disk is full the O/S has less space to utilize for virtual memory. When the disk is full the O/S has less space to utilize for virtual memory. This explains why a system moves so very slowly when the hard disk is full
Question A computer uses a virtual memory management scheme with frames of 512 bytes. It has a physical memory of 16 MB. 1.How many frames does main memory have? 2.What is the size of each page? 3.If memory is fully utilized how many pages are mapped to memory?
Answers A computer uses a virtual memory management scheme with frames of 512 bytes. It has a physical memory of 16 MB. 1.How many frames does main memory have? 32 2.What is the size of each page? 2 9 Bytes 3.If memory is fully utilized how many pages are mapped to memory? 32
Virtual Memory In a virtual memory paging system, because we have some pages in RAM and some on the disk we need what is called a page table to keep track of where each page is being stored.
Virtual Memory Remember that in this system, the program could be split two ways: 1.Some pages are on the disk 2.Other pages are in scattered frames in memory
Virtual Memory When the CPU needs a particular page of data the O/S must know where to locate it (Hopefully it is in memory) The page table is used in the search
Page Table Page # Loc Loc = location
Page # Loc Program Program divided into 4 Pages Page Table31 2 Main memory
The page table is therefore searched for the particular page of interest; its location is noted and that address in main memory is accessed.
Page # Loc Program 4 Pages Page Table31 2 Main memory
The page table sometimes includes a column which indicates whether or not the page is in memory. If a page is not in memory it is accessed on the hard disk and brought into a free frame
Note that in our example one page is on the hard disk. Before the CPU can use that data it has to be brought into main memory
Page # Loc Program Program divided into 4 Pages Page Table31 2 Main memory
If main memory is full then the O/S has to begin swapping. It removes one page, stores it on the hard drive and then puts the needed page into that frame. SWAPPING
When the O/S has to spend a lot of time swapping pages from main memory to virtual memory THRASHING is said to be taking place. This reduces the efficiency of processing by slowing down execution time THRASHING
Virtual memory therefore -allows more programs to be running at a time and -Enables the execution of very large programs which can not hold in memory at one time