======================================================================
    Microsoft Product Support Services Application Note (Text File)
                WW0335: Memory Management with Windows
======================================================================
                                                   Revision Date: 4/92
                                                      No Disk Included

The following information applies to Microsoft Windows version 3.1.

 --------------------------------------------------------------------
| 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 MS-DOS(R)  |
| 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, MS-DOS, and the Microsoft logo are registered           |
| trademarks and Windows is a trademark of Microsoft Corporation.    |
---------------------------------------------------------------------


INTRODUCTION
============

This application note outlines the memory management capabilities of
Microsoft Windows versions 3.0 and 3.1. References that apply to both
versions will refer to Microsoft Windows versions 3.x.

Microsoft Windows versions 3.x provide a protected mode graphical
operating environment that runs existing MS-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, the flexibility of Windows 3.1 memory management
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.x can use or provide (conventional, extended, and
expanded) and discusses how both standard and 386 enhanced modes of
Windows 3.1 operate and use memory.


MEMORY TYPES
============

Three different kinds of memory can be installed in your machine:
conventional, extended, and expanded memory. In addition, IBM-
compatible machines contain a 384K upper memory area (UMA).

Conventional Memory
-------------------

Conventional memory is the first 640K of memory in your machine. MS-
DOS has a limit of 1024K of addressable memory (conventional memory
plus the UMA), and all MS-DOS applications must run within this
conventional memory. All Windows 3.x operating modes share this
limitation for running MS-DOS applications, but standard and 386
enhanced modes break the 640K limitation for running Windows
applications. Windows 3.x enhanced mode can create multiple virtual MS-
DOS machines (for more information on virtual MS-DOS machines, see the
section of this application note called "386 Enhanced Mode and
Conventional Memory."

  NOTE: 386 enhanced mode Windows 3.1 also allows MS-DOS applications
  to break the 640K barrier if they are written to use the Microsoft
  DOS Protected Mode Interface (DPMI) specification. DPMI allows MS-
  DOS applications to run in protected mode under Windows 3.1, using
  up to 16 megabytes (MB) of extended memory directly. Applications
  must be specifically written for DPMI in order to take advantage of
  this feature.
  
384K Upper Memory Area (UMA)
----------------------------

Between the top of conventional memory at 640K and the start of
extended memory at 1024K lies the 384K UMA. This area does not contain
physical memory. Mapped into the 384K UMA are the system BIOS (basic
input/output system) ROM chips and the display adapter memory. When
you install other accessory cards, such as network adapters, they may
also occupy space within the 384K UMA. It is important to remember
that the 384K UMA is always located in the same area of the IBM-
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 MB of address space. The
system memory occupies the first 640K, and the 384K UMA occupies the
area from 640K to 1 MB (1024K). This does not mean that the machine
has 1 MB of memory. A machine with 1 MB of physical memory has an
address space of 1408K. This consists of the 640K of conventional
memory, the 384K UMA, and the 384K of extended memory starting at
1024K.

Extended Memory
---------------

Extended memory is the simplest type of add-on memory to understand.
It is also the type of memory used by Windows 3.x running in either
standard or 386 enhanced operating mode. Extended memory is a seamless
continuation of the original 1 MB address space on 80286 and 80386
computers. Extended memory always starts exactly at 1024K, where the
384K UMA 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 of
system memory plus the 384K UMA). This is one reason why Windows 3.1
cannot run on 8086/8088-based machines, as it requires a minimum of
256K of extended memory (or 1024K for enhanced mode). The 80286
processor can address 16 MB of total memory, and the 80386 processor
can address up to 4 gigabytes (GB).

  NOTE: PC manufacturers often refer to extended memory as expansion
  memory, which is not to be confused with expanded memory.

Windows 3.x and all applications running under Windows 3.x access
extended memory through the Microsoft Extended Memory Specification
(XMS). Rather than accessing extended memory directly, access is made
through an XMS driver. The driver supplied by Microsoft for this
purpose is called HIMEM.SYS. Older MS-DOS applications that check
available extended memory through interrupt 15, service 88H will not
see any extended memory with an XMS driver loaded. Such applications
must be rewritten to use the XMS, instead of interrupt 15, to access
extended memory.

Expanded Memory
---------------

Versions of Windows prior to 3.0 used mostly expanded memory. Windows
3.x does not use expanded memory but may be able to provide it to MS-
DOS applications. It is important to understand the concept of
expanded memory if you still run MS-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 numbers.

LIM 3.2 Expanded Memory
-----------------------

We discussed earlier how PC/XT machines with 8086/8088 processors can
address only 1024K of memory, of which only 640K can be used as RAM
for MS-DOS and MS-DOS applications. You can't add more than 640K of
conventional 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 UMA of the machine to provide this
functionality.

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 (EMM). A LIM 3.2 EMM establishes a
64K page frame within the 384K UMA to perform EMS bank switching. The
64K page frame consists of four contiguous 16K pages. LIM 3.2 expanded
memory won't work without a 64K contiguous page frame.

The page frame is the area where the EMM 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 makes 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.

LIM 4.0 Expanded Memory
-----------------------

LIM 3.2 expanded memory is fine for storing data such as spreadsheets
in expanded memory; however, because the LIM 3.2 specification does
not allow programs to run in expanded memory, it is not useful for
multitasking.

There are two of major differences between LIM 3.2 and 4.0. First,
with LIM 4.0 you can have many more than four 16K pages. LIM 4.0
supports up to 64 pages, which are enough to bank 1 MB of memory at
once. Second, the page frame itself no longer must 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.

Although LIM 4.0 is a standard that offers many advantages over LIM
3.2, few applications have yet been written to take advantage of LIM
4.0.

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.

However, there must be no active ROM or RAM of any sort where you put
an expanded memory page. Thus, you can't map EMS pages on top of
anything that is already occupying address space in your machine (such
as loaded MS-DOS or video/system ROM). On the 80286 processor, 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 because the
expanded memory card is now supplying the memory, it can bank switch
the memory. This capability is called "large page frame" EMS, and it
allows programs to run executable code in expanded memory, a major
improvement over LIM 3.2.

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 memory with an 80386 EMM such as
Microsoft's EMM386.EXE.

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,
because Windows 3.1 supports extended memory directly, backfilling
expanded memory will not give you any advantages, and the memory on
the motherboard is often faster than memory on an expanded memory
board.

LIMulators
----------

Some programs, known as LIMulators, emulate expanded memory on 8088-
and 80286-based machines using hard disk space and/or extended memory.
These programs are not much of an advantage, because although they
supply expanded memory, they are not hardware. They must locate a 64K
EMS page frame in conventional memory and also take up space for the
driver itself. LIMulators generally take close to 80K of conventional
memory to run. Because conventional memory is the most precious memory
on your machine, we do not recommend these types of programs. They are
also typically extremely slow.

On 80386 machines, it is possible to use a 386 expanded memory manager
(such as EMM386 or Quarterdeck's QEMM) to emulate expanded memory.
These LIMulators use the XMS rather than interrupt 15 memory to
emulate expanded memory and are much more efficient than other types
of LIMulators.

Expanded Memory Difficulties
----------------------------

LIM 3.2 expanded memory requires a page frame to work, and the page
frame is located within the 384K UMA 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; RLL, SCSI, and
ESDI disk controllers; and even high-resolution video cards, 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 locate the LIM 3.2 page frame.
   LIM 4.0 does not require a 64K page frame but is almost useless
   without it, as very few applications have been written to take
   advantage of the LIM 4.0 specification. Frequently, the address
   areas of various adapter cards need to be shuffled to open a
   contiguous 64K page frame. Complicating this process are boards
   such as the IBM 3270, which have nonmovable addresses in most
   machines.

2. MAPPING CONFLICTS. Most 386 EMMs (such as EMM386.EXE and Windows
   3.1 386 enhanced mode) use a search algorithm to find unused memory
   addresses between C000 and DFFF located in the 384K UMA 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 is often true of Token Ring network adapters and can
   cause hanging and intermittent operation.

In case of problems, the first thing to do is disable expanded memory.
This procedure will determine whether a page conflict is causing your
difficulty. 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. Consult your memory
manager’s documentation for information on how to exclude an address
range. The adapter may also have to be moved; you do this in different
ways with different memory managers.
   

WINDOWS 3.X MODES AND MEMORY USAGE: HOW EACH MODE WORKS
=======================================================

This section examines how standard and 386 enhanced mode Windows 3.x
use the different types of memory we have discussed (conventional
memory, the 384K UMA, extended memory, and expanded memory).

  NOTE: Real mode is not available in Windows 3.1. Real mode for
  Windows 3.0 was supplied mainly for compatibility with older Windows
  2.x applications. Most, if not all, of these older applications have
  now been updated to work properly with Windows 3.x. Valuable
  development time was saved in not maintaining this outdated mode,
  time that was then spent improving the two remaining Windows modes.


STANDARD MODE WINDOWS 3.1: BREAKING THE 640K BARRIER ON 286 MACHINES
====================================================================

Standard Mode and Conventional Memory
-------------------------------------

When standard mode Windows 3.1 is started, it adds the amount of free
conventional and extended memory and looks at the total amount as one
contiguous block of memory. Conventional memory has no special meaning
under standard mode, except when running MS-DOS applications. While
standard mode Windows 3.1 has MS-DOS application sup-port, the MS-DOS
application(s) must run within conventional memory.


Standard Mode and the 384K Upper Memory Area (UMA)
--------------------------------------------------

In standard mode, Windows 3.x does not directly access the 384K UMA.
Still, if a physical expanded memory board is present (or if a 386
memory manager is providing expanded memory under Windows 3.1), the
EMM will use areas of the 384K UMA in order to provide a LIM 3.2 page
frame. If you suspect a 384K UMA conflict is causing a problem,
temporarily remove the external EMM for testing.

Standard Mode and Expanded Memory
---------------------------------

Standard mode Windows 3.1 does not use expanded memory. MS-DOS
applications running under standard mode can access expanded memory
only if a physical expanded memory board, along with the appropriate
memory manager, is present in the machine. Compatible 386 EMMs such as
EMM386.EXE can be loaded to provide expanded memory outside Windows.
However, 386 EMMs cannot be used to provide expanded memory to MS-DOS
applications running in standard mode Windows. If possible, the 386
EMM will be disabled when standard mode loads. For more information on
using expanded memory with standard mode see the "Expanded Memory for
MS-DOS Applications" section of this application note.

Standard Mode and Extended Memory
---------------------------------
   
Windows 3.x standard mode can access extended memory directly. It adds
free conventional memory and free extended memory. It then provides
the total as memory for Windows applications to use (minus the memory
required by standard mode Windows itself). Standard mode accesses
extended memory through the XMS driver HIMEM.SYS. MS-DOS applications
that use extended memory can also be run from standard mode Windows.


386 ENHANCED MODE WINDOWS 3.X: ENTERING THE WORLD OF VIRTUAL MEMORY
===================================================================

386 Enhanced Mode and Conventional Memory
-----------------------------------------
   
Windows 386 enhanced mode deals with conventional memory in much the
same way that standard mode does. When 386 enhanced mode Windows 3.1
is started, it adds the amount of free conventional and extended
memory; in addition, Windows 3.1 can use hard disk space as virtual
memory (the "swap" file) and look at the total amount as one
contiguous block of memory. Conventional memory has no special meaning
under 386 enhanced mode, except when running MS-DOS applications. In
386 enhanced mode, MS-DOS applications run by creating virtual MS-DOS
machines with up to 640K of free memory.

Each virtual MS-DOS machine inherits the environment that was present
before Windows 386 enhanced mode was started. This means that every
driver and terminate-and-stay-resident (TSR) program loaded before you
run Windows, as well as the environment, is present and consumes
memory in every subsequent virtual MS-DOS machine. The net memory
available within virtual MS-DOS machines under 386 enhanced mode is
slightly less than the free memory at the MS-DOS prompt before you
start Windows, due to the overhead of creating and managing a virtual
machine.

386 Enhanced Mode and the 384K Upper Memory Area (UMA)
------------------------------------------------------

Windows versions prior to 3.0 used the 384K UMA only to place the
expanded memory page frame, and then to a maximum of only four 16K
pages. Windows 386 enhanced mode uses the 384K UMA for two purposes:

1. To place MS-DOS protected mode application programming interface
   (API) translation buffers.

2. To place the LIM 3.2 expanded memory page frame (if required). As
   noted earlier, there may be adapters, such as network cards, trying
   to use the 384K UMA as well. Frequently, all the free pages in this
   area are used by 386 enhanced mode.

API Translation Buffers in the 384K Upper Memory Area (UMA)
-----------------------------------------------------------

Although Windows 3.1 386 enhanced mode breaks the 640K barrier for
Windows applications, it still runs on top of MS-DOS. The copy of MS-
DOS upon which Windows is running can execute and access data only
within conventional memory. The same restriction applies to network
software or other drivers loaded before 386 enhanced mode Windows. 386
enhanced mode allocates buffers in the 384K UMA to translate MS-DOS
and network API calls from protected mode to real mode. Since the 384K
UMA is within the first megabyte of address space, it can be accessed
by MS-DOS in real mode. The translation buffers are used as a window
through which applications running in protected mode can pass
information to and from MS-DOS and the network drivers.

Ideally, there would be enough free space in the 384K UMA to place
both the translation buffers and the expanded memory page frame. Yet
on many configurations there isn’t enough room, and on those systems
you have to make a choice. Either the expanded memory page frame can
be eliminated, or the translation buffers can be allocated in
conventional memory instead of in the 384K UMA. If the translation
buffers are allocated in conventional memory, they take up space in
every virtual machine that you create from Windows, and you will have
less space in which to run MS-DOS applications. The translation
buffers can be allocated either in the 384K UMA or in conventional
memory, but never half and half.

Fortunately, Windows 386 enhanced mode provides a method for
specifying your preference. To do this, place the following setting in
the [386enh] section of the SYSTEM.IN file:

   ReservePageFrame=Boolean

If ReservePageFrame=true (the default), 386 enhanced mode Windows
allocates the page frame first and the translation buffers second.
This makes it likely that the translation buffers will be forced into
conventional memory, but allows you to use expanded memory in MS-DOS
applications. If ReservePageFrame=false, the translation buffers are
allocated first and the page frame second if there is still room.

The ReservePageFrame=false provides the most free memory within
virtual machines, although some MS-DOS applications may not be able to
use expanded memory. Remember, however, that Windows applications do
not need expanded memory to function; only MS-DOS applications that
can use expanded memory will be affected.

  NOTE: If you are using the EMM386.EXE driver, translation buffers
  are always placed in conventional memory.

Page Frame Placement in the 384K Upper Memory Area (UMA)
--------------------------------------------------------

As noted in the expanded memory discussion earlier in this application
note, expanded memory support requires a page frame located in the
384K UMA. With LIM 4.0 expanded memory, it is not necessary to have a
64K contiguous page frame in this area. In fact, it's not necessary to
have a page frame at all in LIM 4.0. Still, if you want to use
expanded memory in your MS-DOS applications, you must have a 64K
contiguous page frame (composed of four contiguous 16K pages in the
384K UMA) because most applications are written to take advantage of
LIM 3.2 expanded memory.

On a typical IBM-compatible machine there are only 12 free 16K pages
in the 384K UMA, and on some machines (such as IBM PS/2s) there are
only 8. Four contiguous pages are required for expanded memory support
in MS-DOS applications, leaving only eight other free pages. Other
items such as network adapters and many hard disk controllers, also
need to occupy pages in the 384K UMA. Frequently, the combination of
installed adapters can break up the free area in the 384K UMA so there
is no 64K contiguous area in which to place the page frame. In this
case, you will not see any free expanded memory in your MS-DOS
applications.

If you experience this problem, you may need to rearrange your adapter
memory locations to get expanded memory support for MS-DOS
applications. This is easiest to do on machines using the Micro
Channel Architecture (MCA) or Extended Industry Standard Architecture
(EISA) bus standards. IBM PS/2 models 50 and above are equipped with
the MCA bus. PS/2 machines allow you to change adapter memory
locations simply by restarting with the PS/2 Reference Disk and
choosing Change Configuration. A similar procedure is available on
most EISA bus machines, such as the COMPAQ SYSTEMPRO and the HP Vectra
486. Normal Industry Standard Architecture (ISA) bus machines such as
the IBM PC/AT and COMPAQ 386 may require you to open the case and flip
DIP switches or move jumpers on the cards to change memory addresses;
check your hardware manual for more information on changing memory
addresses.

You can entirely disable expanded memory support under Windows 386
enhanced mode by adding the NoEMMDriver=yes setting to the [386enh]
section of the SYSTEM.INI file.

Controlling 386 Enhanced Mode 384K Upper Memory Area (UMA) Mapping
------------------------------------------------------------------
   
In versions of Windows prior to 3.0, the WIN.INI file settings
EMMExclude= and EMMInclude= were used to control expanded memory page
frame placement. These settings are used under Windows 386 enhanced
mode to control not only the expanded memory page frame but also the
API translation buffer mapping. Under Windows 3.x these settings begin
with the letters "EMM" only for compatibility reasons; they no longer
apply only to expanded memory. The EMMPageFrame= setting, however,
still applies only to the expanded memory page frame. In Windows 3.x
these three settings have been moved from the WIN.INI file to the
[386enh] section of the SYSTEM.INI file. Earlier versions of Windows
(2.x) do not use the E000-EFFF range of the 384K UMA unless
specifically instructed to do so. Windows 386 enhanced mode will use
this segment unless the machine identifies itself as a PS/2.

To explicitly exclude an area of the 384K UMA from mapping by Windows
386 enhanced mode, use the EMMExclude= setting. The EMMExclude=
setting accepts a four-digit hexadecimal memory range, such as
EMMExclude=E000-EFFF. Since there is no standard for hardware
implementation of the E000-EFFF area, it is frequently necessary to
exclude this range in order for 386 enhanced mode to function
properly. Most adapter cards are automatically detected and excluded
by Windows 386 enhanced mode. If you suspect a problem with a 384K UMA
memory conflict, use the EMMExclude= setting.
  
  NOTE: If you suspect that your machine has multiple adapter mapping
  conflicts, it may be useful to exclude the entire UMA (A000-EFFF)
  for testing purposes.
  
There are very few uses for the EMMInclude= setting. Normally Windows
3.1 386 enhanced mode automatically uses all free pages in the 384K
UMA. Likewise, there are few uses for the EMMPageFrame= setting
because it controls only the expanded memory page frame and not the
translation buffers.

386 Enhanced Mode and Expanded Memory
-------------------------------------

Windows 386 enhanced mode does not use expanded memory itself. It can,
however, create expanded memory for use by MS-DOS applications written
to use expanded memory, such as Lotus 1-2-3 versions 2.x. Expanded
memory required by an MS-DOS application can be specified in the
application’s program information file (PIF). It is no longer
necessary for Windows applications to use expanded memory because they
can now access extended memory directly. Thus, the main issues for
expanded memory under Windows 3.1 are page frame conflicts, as
discussed above.

Windows 386 enhanced mode provides large page frame LIM 4.0 expanded
memory in all virtual MS-DOS machines. Although this feature is not
currently useful for most MS-DOS applications that use expanded memory-
-they use only the LIM 3.2 64K page frame itself, not the additional
bankable pages in conventional memory that large page frame LIM 4.0
supplies--it may be useful in the future.

A compatible external 386 memory manager such as EMM386.EXE can be
loaded to provide expanded memory for MS-DOS applications running
outside Windows 386 enhanced mode. Such a memory manager is not needed
for expanded memory to be available inside Windows 3.x, and is
actually turned off when Windows 386 enhanced mode is run. See the
"Expanded Memory for MS-DOS Applications" section of this application
note for more information.

386 Enhanced Mode and Extended Memory
-------------------------------------

Like standard mode, Windows 386 enhanced mode can directly access
extended memory. It adds free conventional memory and free extended
memory plus virtual hard drive memory (the "swap" file). It then
provides the total as memory for Windows applications to use (minus
the memory required by 386 enhanced mode Windows itself). MS-DOS
applications that use extended memory can also access extended memory
in virtual machines under 386 enhanced mode Windows. The extended
memory supplied to MS-DOS applications by Windows 3.1 386 enhanced
mode can be virtualized.

Running Protected Mode MS-DOS Applications
------------------------------------------

Windows 386 enhanced mode allows MS-DOS applications to run in
protected mode if they are written to use the DPMI specification.

Windows 386 enhanced mode uses the XMS driver to load itself and its
drivers into extended memory before starting up. This is why 386
enhanced mode requires the presence of HIMEM.SYS or another compatible
XMS device driver.

386 Enhanced Mode and Virtual Memory:
A Virtual Memory Conceptual Analysis
-------------------------------------

Virtual memory allows you to simultaneously run more programs than the
amount of physical memory installed on your computer would normally
allow. Virtual memory has been widely used for years in the world of
mainframe computers. Windows 386 enhanced mode offers virtual memory
to the PC-compatible world by using the special demand paging
capabilities of the Intel 80386 processor.

All Windows programs can take advantage of virtual memory in Windows
386 enhanced mode. When you start Windows 386 enhanced mode, choose
About Program Manager from the Help menu. You will see that much more
memory is available than is installed on your machine. Windows
applications can use this extra memory without being written specially
for it because Windows applications are device independent; they let
Windows handle the memory management and simply ask Windows for memory
allocations. With virtual memory, Windows applications keep asking for
more memory, which Windows 386 enhanced mode is now able to deliver.
If you have less physical memory and are forced to rely on virtual
memory, the application runs more slowly; if you have more physical
memory, it runs more quickly--in either case, it is almost always able
to run.

While a program is running in virtual memory, at any given moment some
parts of its code and data are in physical memory while the rest of
the program is swapped to the hard disk. When a reference is made to a
memory address, if the information is in physical memory, the
information is used without program interruption. However, if the
desired information is not in physical memory, a page fault occurs,
and the Windows 386 enhanced mode virtual memory manager (VMM) takes
control.

The required code or data is pulled into physical memory from the hard
disk, and if necessary, some other information is swapped out. Pages
are swapped out on a least recently used (LRU) basis. The pages that
have not been accessed for the longest time are the first to be
swapped out. All this swapping is invisible to the user, who sees only
a little hard disk activity.

Windows 386 enhanced mode virtual memory brings pages of data into
physical memory when they are referenced: this is called a demand-
paged system. This system does not attempt to predict which pages will
be required in the future. The 386 enhanced mode virtual memory
subsystem is implemented by the Windows 3.x VMM and the page swap
device. The VMM maintains the virtual memory page table; it lists
which pages are currently in physical memory and which are swapped to
disk. Because Windows 386 enhanced mode is a multitasking environment,
the VMM page table also contains a list of which memory pages belong
to which process. When the VMM needs a page that is not currently in
physical memory, it calls the page swap device. The page swap device
allocates and deallocates virtual memory and maps pages into and out
of physical memory.

Some virtual memory systems rely on program segmentation to do their
work. Windows applications are segmented; however, virtual memory
under Windows 386 enhanced mode is not related to the segmentation of
Windows applications. All memory, virtual and physical, is divided
into 4K pages, and the system is managed on this basis. Page mapping
starts at zero K and works up. Two kinds of pages can be allocated:
physical pages and virtual pages. The number of physical pages is
simply the amount of physical memory in the machine divided by 4K. In
contrast, memory allocated to an application is made up of virtual
pages. At any given time, a virtual page can be in physical memory or
swapped to the hard disk.

As mentioned earlier, Windows 386 enhanced mode virtual memory
management uses the LRU page-replacement algorithm. The virtual page
table contains flags for each page that indicate whether the page has
been "accessed" and if the page is "dirty." Accessed means that a
process made a reference to the page after it was originally loaded.
Dirty means that a write was made to the page after it was loaded.
Because a memory write qualifies as an access, the dirty attribute
implies the accessed attribute.

To illustrate, assume Windows 386 enhanced mode is out of physical
memory space. A process requests additional memory. Windows has to
decide which pages currently in physical memory it should swap to disk
to fulfill the request. This decision is a three-step process:

1. The VMM scans the page table, looking for pages that have neither
   an accessed nor a dirty attribute. During the scanning process, the
   VMM clears the accessed attribute from all the pages.

2. If the VMM can find enough pages meeting the not accessed/not dirty
   requirement, it fulfills the request. It swaps the pages to disk
   and gives the resulting free memory to the process.

3. If the VMM can't find enough pages the first time through, it
   repeats the scan. However, this time through, none of the pages has
   an accessed attribute, because it was cleared by the first scan.
   Theoretically, therefore, more pages will meet the requirements,
   and the request can be fulfilled. If the required pages are still
   not found by the second scan, Windows then swaps out the pages
   regardless of their attributes.
   
The benefit of Windows 386 enhanced mode virtual memory support is the
ability to run more programs than can be supported by actual physical
memory. The drawbacks are the disk space requirement for the virtual
memory swap file and a decrease in overall execution speed when
swapping is required. However, it's better to be able to run a program
slowly in a virtual memory system than not to be able to run it at
all.

Virtual Memory Paging File Options and Controls
-----------------------------------------------

Windows 386 enhanced mode can use one of two types of virtual memory
paging files, or swap files: temporary or permanent. Only one type of
swap file can be used at a time when running Windows 3.1. Do not
attempt to create a swap file on a RAM disk; this is a self-defeating
pursuit--you would be sacrificing physical memory to provide a place
to create virtual memory to replace the physical memory you have used
to create the RAM disk. Windows 3.0 in 386 enhanced mode requires a
minimum of approximately 1.5 MB of hard disk space free on the paging
drive to provide virtual memory support with a temporary swap file.
With Windows 3.1, that number is down to 512K.

A temporary swap file is simply a normal MS-DOS file created on the
hard disk that can shrink and grow in size as necessary. The temporary
swap file is called WIN386.SWP, and it is created automatically when
Windows 3.x is started. The swap file does not have a hidden or system
attribute and can be deleted, if necessary, any time you are not
running Windows, although it is normally deleted automatically when
you exit Windows 386 enhanced mode.

Temporary swap file location and size can be adjusted by inserting
parameters in the [386enh] section of the SYSTEM.INI file. In Windows
3.0, the temporary swap file is always created in your Windows 3.0
directory unless you set the PagingDrive= setting. If the PagingDrive=
setting is set (for example, PagingDrive=D:), the swap file will be
created in the root directory of the specified drive. There is no way
to specify a subdirectory for the temporary swap file location in
Windows 3.0. In Windows 3.1, the drive and subdirectory of the
temporary swap file may be specified with the PagingFile= setting.
Temporary swap file size is controlled by the MaxPagingFileSize=
setting. The size of the temporary swap file can also be limited in a
different way by the use of the MinUserDiskSpace= setting, which tells
386 enhanced mode to leave the specified amount of disk space in
kilobytes free when creating a temporary swap file.

A permanent swap file occupies a contiguous section of your hard disk.
Using a permanent swap file improves the speed of the Windows 3.x
virtual memory system because there are fewer processor mode
transitions and the swap file's access requires less overhead than a
normal MS-DOS file. The permanent swap file is a hidden file called
386SPART.PAR, which also has a system attribute; the file is always
placed in the root directory of the specified drive. Since the
permanent swap file must be contiguous, you can't create one larger
than the largest contiguous free segment of your hard disk.

Permanent swap files are created and deleted by running SWAPFILE.EXE
in Windows 3.0 real mode, or from the Windows 3.1 Control Panel by
choosing the 386 Enhanced icon and then choosing the Virtual Memory
button. A SYSTEM.INI file setting is not used to point to the location
of the permanent swap file. Instead, when the permanent swap file is
created, Windows creates a file called SPART.PAR in your Windows 3.x
directory. Windows 386 enhanced mode reads the SPART.PAR file to find
out where the permanent swap file is and how large it is. SPART.PAR is
marked read-only to keep you from accidentally deleting it. If you
delete SPART.PAR, Windows will not know about the permanent swap file
and won't be able to make use of it. Because SWAPFILE.EXE and the
Virtual Memory utility both read SPART.PAR, they cannot be used to
delete the permanent swap file if SPART.PAR is deleted.

If Control Panel (or SWAPFILE.EXE with Windows 3.0) says that the
maximum swap file size it can create is smaller than your free disk
space, your hard disk is fragmented.

If you want to create a larger permanent swap file than Control Panel
(or SWAPFILE.EXE) reports possible, you must optimize your hard disk
(optimizing is also known as unfragmenting, or compacting). Optimizing
must be done with a third-party utility program such as Bridgeway
Publishing FastTrax, Norton Utilities, Mace Utilities, Golden Bow
VOpt, or Central Point PC Tools. It is important to run these programs
outside the Microsoft Windows environment. If you have already created
a permanent swap file, make sure you delete it using Control Panel
before optimizing your hard disk. It is not necessary to delete the
permanent swap file every time you optimize your hard disk--only when
you want to increase the size of the permanent swap file by
optimizing, then re-creating it.

The Virtual Memory utility supports only disks that use 512-byte
sectors for the swap file. If 512-byte sectors are not being used,
this indicates a non-standard configuration such as a third-party disk
partitioning driver. The Virtual Memory utility (and Windows 3.x
itself) supports drives with 512-byte sectors that have been
partitioned with the MS-DOS fdisk command. (NOTE: Certain OEM versions
of MS-DOS have altered versions of fdisk that will partition drives
using sector sizes other than 512 bytes.) Never run the Virtual Memory
utility on a drive that uses a partitioning driver in the CONFIG.SYS
file, with the exception of Compaq’s ENHDISK.SYS. If you receive a
message that your swap file is corrupted, use Control Panel (or
SWAPFILE.EXE in Windows 3.0) to delete the corrupted swap file and
create a new one.

Paging to Network Drives
------------------------
   
Paging to a network drive, while certainly possible, is extremely slow
and is not recommended. Only temporary swap files can be used on
network drives. If you want to page to a network drive, the directory
must not have an MS-DOS read-only attribute; you must have create and
write access to the directory.

  NOTE: Because the root of a Novell network drive is the root of the
  server, it is not MSNet-Redirector compatible. Do not set the
  PagingDrive= setting to a Novell network drive. If you do so and
  multiple users run Windows 3.1 386 enhanced mode, their computers
  will hang because they will overwrite each other’s swap files, all
  with the same name and all created in the root directory of the
  server.