Resource Management Under Microsoft Windows

Okay, so you've got this fancy 4GHz Pentium IV with 1Gb of RAM and Windows 98. Still, applications seem to be running out of memory. They run slow, or you may receive the dreaded "Unable to create control" message, or worse yet the entire operating system freezes up. What the heck is the problem?

The short answer: resources. Resources? How could that be? You've got gigabyte of RAM! The short answer: that makes no difference. You add another 512Mb of RAM. Now you have 1.5Gb of RAM. Still you have the same problem. The sad truth is, the amount of physical memory has absolutely no impact on system resources. When your system runs low on system resources bad things, like the foregoing, happen.

Note that, while I seem to be picking on Windows 98 here, the memory model is essentially the same in Windows 95, Windows 98 and Windows Me. Later, we'll talk about Windows NT and its successors Windows 2000 and Windows XP, which share a significantly different memory model.

What are system resources?

The term System Resources actually covers two main areas of Windows memory that are reserved for and used by specific Windows components. They are called User and GDI. User resources refer to the input manager user32.dll. It handles input from your mouse, keyboard, and other sources, such as communication ports, file handles, etc. GDI stands for Graphics Device Interface and is in charge of the visible components of Windows. It stores fonts, brushes, bitmaps, and other graphics stuff, as well as lends support to other graphic output devices such as printers. On Windows 9x/Me systems, you can monitor resources using the resource monitor. It returns the amount of free resources as a percentage.

A little history

Under Windows 3.x, each of these two areas of memory was limited to 64K. All running applications shared that 64K for User resources and 64K for GDI. Needless to say, that created a huge bottleneck.

With the introduction of Windows 95 and continuing through Windows 98 and Windows Me, that 64K was increased dramatically and each of the two areas was further subdivided as follows:

However, each one of these five memory sections is still fixed in size due to the Windows 9x/Me architecture, and cannot be increased, regardless of the amount of physical memory (RAM) installed on the machine. Modern applications demand more and more of the system. The more controls an application creates and the more files it opens, the more stress is placed on the operating system.

The gloom darkens

Now, we have this problem about resources, but wait; it gets worse. When you first install the operating system and start the computer, you probably have somewhere in the neighborhood of 96%-98% free system resources. Over time, you install applications and utilities. Now, when you first start the computer you may only have 70%-80% free resources. This is because many applications install small programs that are started when the system starts. Each of these use resources.

As you run various applications, each application further depletes these system resources. Theoretically, when an application terminates, the resources it used should be returned to the operating system to use for other applications. In the real world, this doesn't always happen. In some cases, it is normal. Some shared resources are not loaded until an application requests them. Those are not normally released when the application terminates. They are kept loaded in memory to allow the next application faster access to them.

Some applications, however, do not behave correctly. They may not free all the resources they allocate. This is called resource leakage. In this case, a block of memory is marked by the operating system as being in use and it cannot be used by the operating system, or any other application. When this happens, the only way to recover that area of memory is to reboot the computer.

How bad is it?

Okay, we've painted a pretty dark picture. What can be done about this problem? The truth is, if you are a home user who runs a word processor, a spreadsheet, an Internet browser and an e-mail client program, the answer is that it is not that big a problem. Those programs are normally not terribly resource intensive and, if you reboot the computer regularly, you'll probably never have a problem. If you receive the dreaded "Unable to create control" error, rebooting the machine will normally free resources that have been lost by misbehaving applications and allow your computer to function normally.

Just remember that applications are sharing memory here. If you encounter a system error, like a General Protection Fault, there is a high likelihood that system memory has been corrupted. You should immediately save any open documents and reboot the computer if you want the system to remain stable. The computer may appear to function normally after such an error, but it is unreliable and may cause a seemingly unrelated error later on. Trust me on this. If you receive a system error, you should reboot the computer.

If, on the other hand, you want to run some more resource intensive applications, like a high-end graphics editor, or a program or web development environment, the Windows 95/98/Me memory model will be a constant source of problems for you. If you have a relatively new, clean install of the operating system and you haven't installed a lot of other programs and you don't have a lot of fonts installed and you don't run too many applications at the same time, etc., etc., you may get away with it. Sooner or later, however, it will become a problem.

The light at the end of the tunnel

Alright, now we've defined the problem. What is the solution? If you develop software, web sites , or graphics (using high end software), the answer, although you may not like it, is to upgrade the operating system. The Windows NT memory model is the answer. Windows NT, Windows 2000 and Windows XP share a vastly different memory model. Under this NT based memory management system, resources are not limited. As long as you have enough physical memory (RAM) or virtual memory (disk space) your applications will not run out of resources. The Windows NT memory system allocates resources dynamically. As long as the memory is available, your application can have it.

Even better, each application gets its own virtual copy of the operating system. That means that, applications are isolated and, if an error occurs, system memory is not corrupted. When an application terminates, all the memory allocated to it is released back to the operating system. That means that resource, or memory leakage is also nearly nonexistent. It is possible for an application to continuously demand more and more memory and the system will give it, until memory becomes short, but closing the application releases all that memory without the need to reboot the machine.

The following chart shows the memory allocated for each specific purpose by the various operating systems:

Resource Comparison

Resource Windows 3.x Windows
9.x /Me
Windows
NT /2000/XP
Window/Menu Handles about 200 32KB (each) Unlimited
Timers 32 Unlimited Unlimited
COM/LPT ports 4 each Unlimited Unlimited
Listbox items (per listbox) 8KB 32KB Unlimited
Listbox data (per listbox) 64KB Unlimited Unlimited
Edit control data (per control) 64KB Unlimited Unlimited
Regions All in 64KB segment Unlimited Unlimited
Logical pens, brushes All in 64KB segment 64KB segment Unlimited
Physical pens, brushes All in 64KB segment Unlimited Unlimited
Logical fonts All in 64KB segment 750-800 Unlimited
Installed fonts 250-300 (best case) 1000 Unlimited
Device Contexts 200 (best case) 16KB Unlimited

In summary

While Windows 95, Windows 98 and Windows Me operating systems improved memory management immensely over Windows 3.1, they were still designed for the home user. They were never intended to be a serious development platform. Encountering errors is a part of software development. An operating system that is not fault tolerant has no place in the software development environment. If you develop software, or web sites using a serious development tool, you should be using Windows NT, Windows 2000, or Windows XP.