Memory Management Under MS Windows - An Introduction The following information was obtained from Microsoft Online services. -------------------------------------------------------------------- | INFORMATION PROVIDED IN THIS DOCUMENT AND ANY SOFTWARE THAT MAY | | ACCOMPANY THIS DOCUMENT (collectively referred to as an | | Application Note) IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY | | KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO | | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A | | PARTICULAR PURPOSE. The user assumes the entire risk as to the | | accuracy and the use of this Application Note. This Application | | Note may be copied and distributed subject to the following | | conditions: 1) All text must be copied without modification and | | all pages must be included; 2) If software is included, all files | | on the disk(s) must be copied without modification (the DOS | | utility DISKCOPY is appropriate for this purpose); 3) All | | components of this Application Note must be distributed together; | | and 4) This Application Note may not be distributed for profit. | | | | Copyright 1992 Microsoft Corporation. All Rights Reserved. | | Microsoft and the Microsoft logo are registered trademarks of | | Microsoft Corporation. | -------------------------------------------------------------------- ====================================================================== INTRODUCTION ====================================================================== A new angle on memory management Version 3.1 of Microsoft Windows brings a new angle to PC memory management. It provides a protected mode graphical operating environment that runs existing DOS applications and allows Windows applications to break the 640K barrier. Windows 3.1 memory management is simpler in many ways than that of previous versions. However, its flexibility continues to present a challenge to those who want to understand its operation in depth. This document examines the three types of memory that Windows 3.1 can use (conventional, extended, and expanded) and discusses how the three modes of Windows 3.1 (real, standard, and enhanced) operate and use memory. ====================================================================== MEMORY TYPES ====================================================================== There are three different kinds of memory that can be installed in your machine. These are conventional, extended, and expanded. In addition, IBM PC-compatible machines contain a 384K reserved I/O address space. CONVENTIONAL MEMORY =================== Conventional memory is the first 640K of memory in your machine (see Figure 1). DOS has a limit of 640K of addressable memory, and all DOS applications have to run within this conventional memory. All of the Windows 3.1 operating modes share this limitation for running DOS applications, but standard and enhanced modes break the 640K limitation for running Windows applications. Windows 3.1 enhanced mode can create multiple virtual DOS machines (for more information on virtual DOS machines, see the "Enhanced Mode and Conventional Memory" section of this application note). NOTE: Enhanced mode Windows 3.1 also allows DOS applications to break the 640K barrier if they are written to use the Microsoft DOS Protected Mode Interface (DPMI) specification. DPMI allows DOS applications to run in protected mode under Windows 3.1, using up to 16 megabytes of extended memory directly. Applications must be specifically written for DPMI to provide this feature. ************************************************** Figure 1: Conventional and Extended Memory Graphic not available in text-formatted file. Please see printed document for Figure 1. ************************************************** 384K RESERVED I/O ADDRESS SPACE =============================== Between the top of conventional memory at 640K and the start of extended memory at 1024K lies the 384K reserved I/O address space (see Figure 2). This area does not contain physical memory. Mapped into the 384K reserved I/O address space are the BIOS (basic input/output system) ROM chips and the display adapter memory. When you install other accessory cards, such as network adapters, they occupy space within the 384K reserved I/O address space as well. It is important to remember that the 384K reserved I/O address area is always located in the same area of the IBM PC-compatible computer's address space: from 640K to 1024K (A000 to FFFF hexadecimal). There are no exceptions to this rule. This means that a standard IBM-compatible machine with 640K of conventional memory installed really has 1 megabyte of address space. The system memory occupies the first 640K, and the 384K reserved I/O address space occupies the area from 640K to 1 megabyte. This does not mean that the machine has 1 megabyte of memory. A machine with 1 megabyte of physical memory has an address space of 1408K. This consists of the 640K conventional memory, the 384K reserved I/O address space, and the 384K of extended memory starting at 1024K. The IBM PS/2 Models 50, 50Z, and 60 are examples of machines that always ship with at least 1 megabyte of memory installed. EXTENDED MEMORY =============== Extended memory is the simplest type of add-on memory to understand (see Figure 1). It is also the type of memory used by Windows 3.1 running in standard or enhanced operating mode. Extended memory is simply a seamless continuation of the original 1 megabyte address space on 80286 and 80386 computers. Extended memory always starts exactly at 1024K, where the 384K reserved I/O address space ends. There are no exceptions. It is not possible for an 8086 or 8088 machine to have extended memory. This is a hardware limitation of the 8086/8088 processors, which can handle only 1024K of total address space (that is, 640K system memory plus 384K reserved I/O address space). The 80286 processor can address 16 megabytes of total memory and the 80386 processor can address up to 4 gigabytes. Note that PC manufacturers often refer to extended memory as expansion memory, which is not to be confused with expanded memory. Windows 3.1 and all applications running under Windows access extended memory via the Microsoft eXtended Memory Specification (XMS). Rather than accessing extended memory directly, access is made via an XMS driver. This driver is called HIMEM.SYS. Older DOS applications that check available extended memory via the interrupt 15 service 88H will not see any extended memory with an XMS driver loaded. These applications must use the XMS, instead of interrupt 15, to access extended memory. EXPANDED MEMORY =============== Older versions of Windows use mostly expanded memory. Today, Windows 3.1 uses expanded memory only when running in real mode. However, it is important to understand the concept of expanded memory if you still run DOS applications that use it. There are two different kinds of expanded memory, differentiated by their Lotus/Intel/Microsoft (LIM) Expanded Memory Specification (EMS) version number. LIM 3.2 Expanded Memory ----------------------- We discussed earlier that the old PC/XT machines with 8086/8088 processors can address only 640K of memory. You can't add more than 640K of standard memory to the system, but you can provide more than 640K through a technique called bank switching. Expanded memory uses an empty area in the 384K reserved I/O address space of the machine to provide this functionality. Figure 3 shows the memory addressing range of an 8088-family processor on the left and an expanded memory card on the right. To use an expanded memory card, you must load a device driver to let the card know how to communicate with the PC. This device driver is called an Expanded Memory Manager, or EMM. The LIM 3.2 EMM establishes a 64K page frame within the 384K reserved I/O address space to perform EMS bank switching. The 64K page frame consists of four 16K contiguous pages. LIM 3.2 expanded memory won't work without a 64K contiguous page frame. The page frame is the area where the expanded memory manager maps information into and out of the RAM of the expanded memory card. Information is not physically copied from the EMS card to the RAM of the computer. The device driver simply changes the card's page registers to make the page frame point to the data on the expanded memory card. The data then appears in the page frame, and your application can access it. ************************************************** Figure 2: The 384K Reserved I/O Address Space (Use this page for reference when troubleshooting memory address conflicts.) Graphic not available in text-formatted file. Please see printed document for Figure 2. ************************************************** ************************************************** Figure 3: LIM 3.2 Expanded Memory Graphic not available in text-formatted file. Please see printed document for Figure 3. ************************************************** LIM 4.0 Expanded Memory ----------------------- The LIM 3.2 standard is fine for storing data such as spreadsheets in expanded memory, but it does not help for multitasking. This is why Lotus, Intel, and Microsoft created the LIM 4.0 standard. Comparing Figures 3 and 4 shows two of the major differences between LIM 3.2 and 4.0. First, you can have more than four 16K pages, much more. LIM 4.0 supports up to 64 pages, which are enough to bank 1 megabyte of memory at once. Second, the page frame itself no longer has to be four contiguous 16K pages. In fact, you need no page frame at all. Whichever EMS version you are dealing with, the basic operating principle of bank switching is still at work. Backfilling ----------- With LIM 4.0's limit of 64 16K pages, you have enough pages to bank switch the entire conventional address range of the processor. Yet there must be no active ROM or RAM of any sort where you put an expanded memory page. This means you can't map EMS pages on top of anything that is already occupying address space in your machine (such as loaded DOS or video/system ROM). On the 80286 processor, this means you should disable as much motherboard memory as possible (down to 256K) and let the expanded memory card supply that memory. This process is called backfilling. This doesn't mean that your computer will have only 256K -- your programs still see a 640K machine. But since the expanded memory card is now supplying the memory, it can bank switch the memory. This capability is called (erroneously) "large page frame." The 80386 processor has a built-in ability to readdress any page of memory to any other location. This means you don't have to backfill on an 80386 to get large page frame LIM 4.0 functionality, nor do you need an expanded memory board. It's easy to convert your 80386's extended memory to expanded with an 80386 Expanded Memory Manager such as Microsoft EMM386.SYS. If you're using an expanded memory board on an 80386 machine, read your manual carefully before you backfill. Not all memory boards have the register support to supply more than four 16K pages (LIM 3.2). Though a memory driver may conform to the LIM 4.0 specification, that does not mean the hardware can provide more than four pages. Also, since Windows 3.1 supports extended memory directly, backfilling expanded memory will not give you any advantages unless you plan to run only real mode Windows. ************************************************** Figure 4: LIM 4.0 Expanded Memory Graphic not available in text-formatted file. Please see printed document for Figure 4. ************************************************** LIMulators ---------- There are some programs available that emulate expanded memory using extended memory and/or hard disk space. These programs are sometimes known as LIMulators. They are not much of an advantage because although they supply expanded memory, they are not hardware. They must place a 64K EMS page frame in conventional memory and also take up space for the driver itself. LIMulators generally take upward of 80K conventional memory to run. Since conventional memory is the most precious memory on your machine, we do not recommend these types of programs. They are also extremely slow. Expanded Memory Difficulties ---------------------------- Expanded memory requires a page frame to work, and the page frame is located within the 384K reserved I/O address space of your machine. Unfortunately, your EMM is not the only competitor for that memory space. Add-on boards such as network cards, 3270 emulation cards, ESDI disk controllers, and others can contend for this address space. Several potential difficulties can arise due to this contention: 1. Lack of Space The major problem is simply finding at least 64K of contiguous free space in which to place the page frame. LIM 4.0 does not require a 64K page frame but is almost useless without it. Frequently the address areas of various adapter cards need to be shuffled about to open a contiguous 64K page frame. Complicating this process are such boards as the IBM 3270, which have nonmovable addresses in most machines. 2. Mapping Conflicts Most EMMs (such as EMM386.SYS and Windows 3.1 enhanced mode) use a search algorithm to find unused memory between C000 and DFFF located in the 384K reserved I/O address space to use as page frames. Some cards (adapters) do not reserve their address space until you access the card, so the memory manager can inadvertently map EMS pages on top of an address the card will request. This can cause hanging and intermittent operation. This problem is fairly rare because the page search routine can locate almost all popular adapters. In case of problems, the first thing to do is to disable expanded memory. This will show if a page conflict really is causing the symptom you are experiencing. If the problem goes away without expanded memory, the memory manager must be told to exclude the address the adapter is occupying from consideration as a page location. The adapter also may have to be moved. You do this different ways with different memory managers. Consult your memory manager's documentation for information on how to exclude an address range. ms-windows