Windows : Memory and libraries


To enable students to:

  1. understand memory management

  2. share memory between processes

  3. describe DLLs

Understand memory management

As discussed previously, each process within the Windows environment operates within its own environment, protected from other by the operating system. To achieve this each application requires its own, private area of memory to work in.

Under Windows NT every process is given 4GB of memory in which to operate. It is very unusual to find a machine with such an amount of physical memory, consequently the operating system has to provide "virtual memory" so that the process believes it has access to 4GB of memory although in reality it may be much less.

The virtual memory used by the process is divided into equal blocks known as pages. Areas of memory that have not been used recently are copied to a temporary paging file on disk, when next required the page is reloaded from disk. The virtual memory manager is responsible for managing this process, mapping physical memory locations onto the virtual ones used by each process. By tracking these pages the virtual memory manager can ensure that no process can access the memory used by another, thus protecting processes from each other.

Besides offering program security, virtual memory also makes program development easier. Previous version of Windows and DOS made use of a technique termed segmentation to allow programs to access more than 640K of memory, the details of this process are beyond the scope of this lecture but suffice to say it is convoluted and error prone. These techniques are no longer required, newer versions of Windows have introduced a "flat" memory model enabling access to 4GB of memory - more than enough for current applications!

Share memory between processes

So far emphasis has been placed on the memory protection offered to processes by Windows. Unfortunately applications that can not share information with each other are likely to be pretty useless, ways need to be found in order to pass data between separate address spaces. Several methods exist to do this, but all are based on a technique called file mapping.

File mapping allows a process to treat a disk file as if it were a block of memory. If two or more processes map the same disk file into their memory address space simultaneously a way of sharing data has been established. The section of memory representing the file can be used to pass information while the rest of the processes memory is protected from interference from other applications.

Establishing a file mapping can be tricky so several other inter-process communication mechanisms exist, although they all eventually rely on memory mapped files. For example pipes allow processes to send messages to each other through a structure resembling a queue. Messages are placed on one end by application A and read from the other by application B.

Describe DLLs

DLL stands for Dynamic Link Library. A DLL is a collection of useful routines packaged into a single file that may be loaded by executables to extend their functionality. By loading a DLL the executable has access to all the functions it implements.

Although the same DLL may be loaded by several applications simultaneously, they all have their own private copy held within their address space. To minimise memory overhead the operating system will ensure that the static portions of a DLL (its code and constant variables) are represented by a shared file with only its working memory being unique to each application loading it.