Back

Memory management

Memory management
Your computer has some Immediate Access Store (known also as IAS but you may also know it as RAM – the terms are used interchangeably). This is a fixed size although you can of course go out and buy some more! For example, you may have
128 Mbytes of RAM and you could upgrade this to 512 Mbytes. IAS is used to store the programs and data that you currently need to use (as opposed to secondary storage devices such as the hard disk, CD-ROM or floppy disk, which are used to store applications and data that you may use in the future but don’t need now).

When you want to use some applications or want to work on a file, you typically double-click on it. It is then transferred by the operating system from a secondary storage device into the IAS. The operating system has a piece of software called a ‘loader’ that is responsible for:

    • Transferring an application or file into an appropriate place in the primary memory.
    • Adjusting any references to RAM addresses that have been made in a program. For example, if you have an instruction that says “Get data from RAM location 1000 000” and the operating system has used that location for another program instead of a data file, then the loader will have to adjust the reference so it is looking at a different (but correct) RAM location.

If you are multi-tasking, however, then you may have a number of applications and a number of files open (i.e. in the IAS) at the same time. There is a potential problem here for the loader. These applications and files may interfere with each other and the loader must ensure that they don’t! They all need to be in RAM but each one is in danger of overwriting the RAM locations of other applications and files! For example, consider the following. A computer with 32 Mbytes of RAM has an operating system and a virus checker running (in RAM). Let us assume that the operating system is in memory locations 0 Mbytes up to address 20 Million. The virus checker starts at 20 Mbytes and goes up to address 25 Million. We can represent this as:

If the user then double clicks on some word processing software and opens some files (let’s assume that this take 4 Mbytes of RAM), the application and the data must be put somewhere after 25 Mbytes. If they are put, e.g. from address 18 Million onwards then the Virus checker software would be overwritten (and wouldn’t therefore work) and some of the operating system would be overwritten (which may cause the computer to crash). Deciding how the RAM is to be used for all of the different applications and data is known as memory management and is an important role of the operating system. Our RAM now looks like this:

Now suppose the user opens another application as well as the word processor. Suppose the user opens a spreadsheet that needs 5 Mbytes. The operating system puts it at the start of the free space, at address 29 Million – but then realises that there isn’t enough RAM to fit the whole application in!! You may think that the computer might crash or at the very least produce an error message. However, the computer has a memory management trick up its sleeve. It uses some ‘pretend RAM’ by ‘converting’ temporarily some of the unused space available on the hard disk. The proper name for hard disk space being used like RAM is ‘virtual memory’.

How can more than one program be running in memory at the same time, even though there isn't enough primary memory to run both programs? The operating system relies on ‘virtual memory’. It relies on the fact that, even if you have 5 applications open at the same time, only one of them is ‘active’, or being serviced by the CPU, at any one time. In addition, you often don’t actually need the entire code for an application in RAM at any one moment. For example, if you are not using some clipart that comes with a word processing application, then the clipart doesn’t need to be in RAM, although if you do start using it then it must be moved into RAM.

    • All programs are split up into equal lengths by the operating system. These equal lengths are known as pages. A page might be 32 Kbytes long, for example.
    • The RAM is also split up into pages.
    • The computer keeps as many pages as it can in RAM, especially those needed to run the active application. In the above example, if the word processing software was active, then the operating system, the virus checker, all of the word processing application and 3 Mbytes of the spreadsheet software would be kept in RAM.
    • The remaining 2 Mbytes of the spreadsheet software can’t fit into RAM and would be stored, in page-size blocks, in virtual memory, on backing storage (the hard disk) in an area called the page file. In Windows, this is also known as the swap file.
    • When you switch from the word processor to the spreadsheet, so that the spreadsheet is the active application, what happens?
    • The CPU moves out of RAM and on to the hard disk some of the pages that it doesn't need for a while, (in this case some of the word processing pages) and then moves from the backing storage into RAM those spreadsheet pages which it now does need!
    • This takes a little bit of time - the hard disk is very slow compared to the CPU. Have you ever noticed when you are multitasking that it can take a little while (sometimes ages) before you can actually access your computer when you switch between applications?
    • When the pages have been swapped, you can then start using the spreadsheet.
    • If you swap back again, the above process is repeated.

What you are doing here is to use a little part of your hard disk as a sort of (slow) RAM. Because this part of your hard disk is being used like real memory, but isn't actually memory, it is known as ‘virtual memory’.

Problems and solutions
Virtual memory allows a user to have and use apparently more RAM than is actually available. There are problems, however.

    • As has already been highlighted, swapping pages in and out of the hard disk takes time! ‘disk threshing’ (or thrashing your hard disk) as it is known, causes the computer to work slowly. The more memory you have, however, the more pages you can keep in it and the less swapping you need to do! Ideally, you want to have enough RAM to run applications without using virtual memory. In the example above, we only needed 3 Mbytes of virtual memory, so it would have been unlikely to slow down the computer too much. Imagine if we had 200 Mbytes of applications and only 32 Mbytes of RAM!! You would recommend a user in this situation to go out and buy some more RAM, quickly!
    • You should run a ‘defragmentation utility’ regularly on your computer's hard disk. As you use a hard disk, files get split up and stored increasingly all over the hard disk. This might eventually mean that pages used in virtual memory can't be stored together. To access pages that are all over a hard disk is slower than accessing pages that are all together. Running Defrag regularly will keep your hard disk working at its optimum performance.

Page tables
The operating system manages the primary memory in a computer and one very important part of memory management is the management of virtual memory. This was described in the previous example. All the operating system has to do is keep track of all of the pages. Each application will have associated with it a page table. This will identify each page for each application and will contain information about where to find that page if it is on the hard disk in virtual memory. If a page is in RAM then the entry for that particular page will be blank. When a page is needed, the operating system looks up the page reference in the page table, finds out where it is in virtual memory and then transfers it from the hard disk to the primary memory.

This, of course, all takes time and contributes to the computer slowing down. This kind of tracking system for pages is relatively simple to implement but there are some disadvantages.

    • Paging is a simple system to implement but may not be the most efficient. This is because the pages are of a fixed size. A page may only have a small amount of data in it but will require a whole page to store it in.
    • Pages are fixed sizes. They will contain code that may not be a logical group of code (for example, a logical block of code such as a function might run across 6 different pages). This means that to load that particular function will require 6 pages to be loaded up, which is not as efficient as loading e.g. one page for the whole function.
    • Page tables are typically large and accessing them slows down the computer.

Segmentation
An alternative approach is to use segmentation. Instead of pages being a fixed size, they are a variable size. Because their size can vary, code can be logically grouped into one segment. You could have a segment per procedure, for example, and this would reduce the size of the tables needed to store the segment information. The downside to segmentation is that retrieving a segment is more complicated. You need to store both the start address of a segment and the size of the segment. Start addresses have to be calculated with care because the sizes of each segment varies. With paging, it is entirely predictable where each page starts and finishes.

Back