Site hosted by Angelfire.com: Build your free website today!

“640K ought to be enough to anybody.” – Bill Gates, 1981

Operating Systems

Memory Management

Goals

When you complete this section you will be able to:

Lessons

Theory - This file contains the background and theory you'll need to successfully complete the lab exercises for this lesson. You should read this first.

DOS Lab - This is the Disk Operating System (DOS) lab manual. It contains activities and exercises to help you understand the theory as it applies to DOS.

Windows 98 Lab - This is the Windows 98 lab manual. It contains activities and exercises to help you understand the theory as it applies to Windows 98.

Windows XP Lab - This is the Windows XP lab manual. It contains activities and exercises to help you understand the theory as it applies to Windows XP.

Linux Lab - This is the Linux lab manual. It contains activities and exercises to help you understand the theory as it applies to Linux.

Skill Check - This set of questions will quiz your understanding of the operating system theory and practice presented in this lesson.

Challenge - This set of advanced lab exercises is designed to help you apply your understanding to new challenges.

Introduction

This lesson will introduce you to memory management and how that task is accomplished by the operating systems we study. You will be amazed at how operating systems manage to give applications hundreds of megabytes of memory when there is less than 100 MByte installed in the computer.


Theory

Managing memory is one of the core functions of any operating system. In this chapter you’ll see two different memory management schemes and learn how to check the memory use on a DOS, Linux, or Windows computer.

Types Of Memory

Cache

A cache (pronounced “cash”) is a small, high-speed memory area that stores the most recently used instructions or data. Cache speeds up the computer since programs tend to use only a relatively small subset of instructions or data. If this frequently used information is placed in a high-speed memory area (the cache) the computer can work faster.

For example, when the processor (CPU) performs a read operation (that is, retrieving instructions or information from memory), it will first check to see if that data is in the cache - if so, the processor retrieves it very quickly. If the data or instruction is not in cache, the CPU fetches the item from the main memory, but also stores a copy in cache. Now this data item is in the cache for the next CPU request. Since the cache is rather small, anything added will remove something else that hasn’t been used for a while. Obviously, a larger cache can store more data and be more likely to speed up the system.

Cache is usually identified as L1, L2, or L3. The “L” stands for “Levels” away from the CPU. A 1st Level cache, (also called an internal, or L1 cache) is usually built into the CPU chip itself. L1 cache is the fastest possible storage; however, real estate inside the CPU is very expensive so L1 cache is usually quite limited.  Second level cache (also called external, or L2 cache) is outside the CPU but on a high-speed, direct connection to the CPU. For L2 cache, the system designer usually chooses very fast (but expensive) memory chips – so there is a trade-off between price and performance (isn’t there always?). Some systems may have levels beyond L2, but they are rather rare. As caches move away from the CPU (the “L-number” increases), they typically get larger, but slower.

RAM

Random Access Memory (RAM) is the “regular” memory in your computer. RAM is the memory you can buy and insert in your computer and is usually what people mean when they say “I’ve got to get more memory” (except my old aunt Sally, who means she wants to be able to memorize things better). Today most computers have 64 Mbytes (or more) of RAM.

RAM comes in dozens of different configurations, types, pin-outs, and other forms. Before you buy RAM for your computer, be sure you know what type to get. You should consult your main board’s manual or (if you’ve lost that) check online for help.

ROM

Read Only Memory (ROM) is used to store information about your computer system that never changes. ROM contains, for example, the boot-up routine for your computer.

Memory Management Under DOS

DOS uses one of the simplest forms of memory management available. Since DOS is a single-user system that does not permit multiple programs to run at once, any program that is running gets the entire memory area (with only a few exceptions). Under DOS, the computer’s memory would be divided like this:

Image - DOS Memory Map

Figure 1 - DOS Memory Map

DOS has a bit of a problem with memory. From the beginning, the engineers at Microsoft determined that 640 Kbytes of memory was adequate for any programs DOS would ever run. They then created DOS to only address (or access) 640 Kbytes of memory. As computers became more advanced and memory became cheaper, the Microsoft engineers were stuck with a terrible problem. There were so many programs that were written for DOS (with its 640 Kbyte limit) that if they changed the DOS memory rules those programs would become obsolete.

Soon, memory chip manufacturers began marketing 1 Mbyte chips because they were as easy to make as a 640 Kbyte chip and they proved to be very popular with the public. The engineers at Microsoft then decided to use the space above 640 Kbytes for systems-type programs and device drivers. This memory area came to be called “Upper Memory” and its use freed space in conventional memory for programs.

Soon, though, memory became even cheaper and people started installing two Mbytes or even more in their computers. This memory became known as “Extended Memory.” Rather than change the way DOS worked, Microsoft decided to add a memory manager (called EMM386) that would swap parts of extended memory into and out of conventional memory. By doing this, a user could start a program in conventional memory, and then start a second program. When the second program started, the first would be halted and moved into extended memory where it would wait. When the user wanted to switch between the two programs, DOS would simply swap them and then restart the program in conventional memory. Of course, all the program’s data would also be swapped so it would appear that two programs were running at once. (This type of process management is called “task switching” and was covered in Chapter 3.)

The next improvement in the memory manager permitted DOS itself to shift into the first 64K of Extended Memory - the so-called “High Memory” area. By moving DOS out of conventional memory it left virtually all the 640 Kbytes free for programs.

Additionally, many third-party software developers began to create memory managers to replace EMM386. These products would often compress the memory and swap it in and out more efficiently. In the end, the user would seem to have more memory - but the computer would slow down a bit since it had to compress and decompress programs when it swapped them.

The 640-kByte-memory scheme worked for DOS – but DOS provided no multi-user or multiprocessing capability. As computers became more advanced and users became more sophisticated they began to demand an enhanced working environment. Things were about to change…

At this point, I should take a moment to discuss the Expanded Memory Specification (EMS) since you may see this in your reading. Lotus, Intel, and Microsoft cooperated to first devise EMS (hence it’s often called LIM/EMS). Despite the name’s similarity to extended memory, EMS is much different. EMS required a special circuit card be added to the computer and that card could contain up to 32Mbytes of memory (a huge amount when this was first proposed). The EMS card had software built-in that would swap programs or other information into conventional memory as needed. If this sounds a bit like extended memory it’s because it is like extended memory. The difference is that with EMS a user could add more memory to the computer than could possibly be used by any program of that day – so there was no possibility of an “out of memory” error. The downside of EMS was it required a hardware installation – which was expensive and troublesome. In the end, EMS never did become very popular and is not used at all today.

Virtual Memory

Eventually, computer programs began to grow and needed more memory for execution. Not only that, but users began to want more from their computers and started running more than one program at a time. It’s not unusual for a personal computer to be constantly checking e-mail with an Internet connection, playing music on a compact disk, monitoring some Web site for updated stock quotes, and using drag-and-drop to transfer information between Word and Excel (both programs running at once).

All of this activity at one time means the system needs far more memory than is usually available. For example, as I typed this page on my home computer I checked Windows Allocated Memory and found it was using 317.7 Mbytes - not a bad trick for a computer that only has 128 Mbytes installed! (By the way, later in this lesson I’ll show you how to check the amount of memory Windows has allocated on your computer.)

This additional memory requirement is called Virtual Memory - it doesn’t really exist, but the programs think it’s available. This is a pretty neat trick - but how does it work? Welcome to paging (and I don’t mean a device that buzzes on your belt)…

Memory Paging

Most modern operating systems divide the computer’s physical memory into numerous small “pages.”

I’m sure you’ve been in the post office and seen the postboxes where customers can pick up their mail. Imagine that the post office has a shortage of boxes and decides to implement an odd scheme so every customer who wants one can get a box (of course, we all know that the US Post Office would never use odd schemes). They decide to divide the day into two halves and put one customer’s mail in a given box in the morning, then another customer’s in the same box that afternoon. In order to get their mail, a customer must come in at the right time of day. While this is an unusual idea, it would actually double the number of customers the post office could service. Of course, the customer be somewhat inconvenienced, but most customers only use the post office box for a few minutes every day anyway so this would not be totally out of the question. (I once submitted this suggestion and then went back a few days later to check on it. I found my suggestion posted on the wall with the word “kook” written on it. I got the feeling that they didn’t like it.)

Your computer’s operating system is faced with too many customers (applications needing memory) and not enough boxes (actual physical memory installed). Operating systems programmers have solved that problem by creating memory paging and swap files.

Figure 2 illustrates the concept of virtual memory mapping. Let’s take a few minutes to consider this important topic.

Image - Memory Paging

Figure 2 - Memory Paging

On the left side of the figure, you’ll see that some program has requested 100 memory locations. These 100 memory locations have been divided into 10 virtual pages with 10 locations in each page. (I know full well that virtual memory pages usually contain anywhere from 512 bytes to 8 Kbytes of space - but for this illustration I have chosen use multiples of 10 to keep the discussion easy to follow. If you ever design your own operating system you can use any page size you want - so there!) Virtual pages actually exist only on the computer’s hard drive and are not usable by the CPU until they are loaded into physical memory.

Unfortunately, the computer only has 50 physical memory locations. These locations have been divided into five page frames with 10 locations in each frame.

When the program started, it realized it needed information from memory locations 1-10 (virtual page 1), 21-30 (virtual page 3), 31-40 (virtual page 4), 51-60 (virtual page 6), and 91-100 (virtual page 10), just to get going. The operating system then loaded the virtual pages that contained these locations into the page frames in physical memory. For example, virtual page 1 went to page frame 1, virtual page 3 went to page frame 2, and so forth. Note that the virtual pages do not have to appear in order in the page frames - they can go anywhere. The operating system is smart enough to know what part of the program is in each frame.

Now, all the necessary parts of the program are loaded into memory - so the program can run without any problems.

However, suppose the program suddenly realizes it needs the information that is in memory locations 81- 90 (virtual page 9). Those locations are not in physical memory (the operating system generates a “page fault” when this happens) so the operating system must take action to load them into a page frame so the program can use them. The operating system will first return the contents of one of the page frames back to the disk drive, and then load the needed virtual page into physical memory. Perhaps the operating system decided that the information in page frame 5 would not be needed for a while - so it moves that information back to virtual page 4 (where it came from on the disk), then moves virtual page 9 into page frame 5.

While it seems complex, it is an amazingly simple way to ensure every program has all the memory it needs to operate. Of course, the operating system must work hard to keep up with the page swapping - but the end result is that every program you want to use gets all the memory it needs.

Summary

In this lesson you learned some of the most important memory management concepts. Here's what you should remember as you start your labs:


DOS Laboratory

Checking Memory Use

You can check to see how DOS is using your computer’s memory. From the DOS screen, enter this command:

mem

When I did this on my computer at home, I got this result:

Image - DOS Memory Check

Figure 1 - DOS Memory Check

In Figure 1 you’ll note that DOS reports the amount of conventional, upper, reserved, and extended memory on the computer (see the discussion of DOS memory in the theory section for a definition of these terms). I have 636 Kbytes of conventional memory loaded on my computer; I’ve used 64 Kbytes of that memory, and have 572 Kbytes free.

Maybe you notice a problem with the “Extended Memory” line. The reason it shows 129 Mbytes of extended memory free when there are only 65 Mbytes total is because DOS has a hard time detecting memory above the 64 Mbyte limit and I have 128 Mbytes on my computer. It just plain doesn’t compute that much memory properly. This is also why some of the extended memory numbers don’t total properly. Remember, DOS was first made to only see 640 Kbytes of memory – stretching it to beyond 64 Mbytes is just too much!


Windows XP Laboratory

Checking Memory Use

You can use several methods to check on the available memory in Windows. One of the simplest is to double-click “System” in the control panel. You’ll get a display like this:

Figure 1 - System Properties

You’ll note that the last line of this screen indicates I have 512 Mbytes of memory (RAM). Of course, this is just physical memory and does not reflect what the computer is doing with virtual memory or a swap file. Another way to check on the amount of physical memory is to start the System Information dialog:

Start -> Programs -> Accessories -> System Tools -> System Information

Figure 2 - System Information

In Figure 2, you can see that I have 512MB of physical memory and 1.72GB of Virtual memory. You can also see that my swapfile is named "pagefile.sys" on the C:\ drive. I can find a lot more information about the computer by clicking on the "+" signs for Hardware, Components, and such along the left side of the screen.

The displays in Figure 1 and Figure 2 tell me a lot about my computer, but it would also be nice to be able to monitor what the computer is doing at any given time. This way, I can actually see the virtual memory grow and see when page faults are generated. To do that, I would start the performance monitor: Control Panel -> Administrative Tools -> Performance.

Figure 3 - Performance Monitor

The tool illustrated in Figure 3 looks somewhat daunting; however, it's not too bad. The large part of the screen is a scrolling graph (the colored lines in Figure 3) that continuously monitors various aspects of your system. By default, it will monitor the number of memory pages needed per second, the average disk quota used, and the percentage of processor use. However, you can remove these catagories and add others.

To make your system monitor useful, click on the “add” button (that’s the one on the top tool bar that looks like a "+" sign). When you do you’ll get a screen like this:

Figure 4- Performance Monitor Add Item

In Figure 4 you'll notice that I was looking for a way to monitor page faults; one of the Memory options. However, from the drop-down monitor catagories look at how many different types of performance items you can monitor for your computer!

Figure 5 - Selecting Page Faults for the Performance Monitor

After I chose the Memory catagory, I next selected "Page Faults per Second" and clicked the Add button.

Figure 6 - Performance Monitor Including Page Faults per Second

When the performance monitor is re-displayed you can see that I've now added a line for Page Faults per Second and can keep an eye on that item.


Linux Laboratory

Checking Memory Use

You can check memory usage with Linux using this command:

free

When I entered that command, I got this screen:

[selfg@localhost selfg]$ free


             total       used       free     shared    buffers     cached
Mem: 513804 295472 218332 0 31424 173328
-/+ buffers/cache: 90720 423084
Swap: 566960 0 566960 [selfg@localhost selfg]$

Figure 1 - Checking Linux Memory With Free

In Figure 1 you’ll notice that about 514 Mbytes of memory are installed on the computer (the numbers are assumed to be X1000), with about 295 Mbytes being used, leaving 218 Mbytes free.

Linux uses a swap file for memory paging. On this computer it is about 567 Mbytes big, but none of it is being used, leaving about 567 Mbytes available.

One other command you can use to check memory is:

vmstat

The results of that command are displayed in Figure 2.

[selfg@localhost selfg]$ vmstat


   procs                      memory      swap          io     system      cpu
r b w swpd free buff cache si so bi bo in cs us sy id
0 0 0 0 218332 31424 173348 0 0 27 12 140 152 6 0 94
[selfg@localhost selfg]$

Figure 2 - Checking Memory With vmstat

In Figure 2 you’ll see only one line of information (the line that starts “r b w…” is a header line). This screen shows the processes that are running (“r”), uninterruptible sleeping (b), and swapped out to the hard drive (w). The amount of virtual memory used (swpd) is 0 Mbyte and the computer had 218 Mbytes of memory available (over 31 Mbytes were being used for buffers). If you are interested in the other fields displayed by this command, you can check the man page for vmstat.