15.2 Using Segments

One of the principal features of Windows is that it lets the user run more than one application at a time. Because multiple applications place greater demands on memory than does a single application, the ability in Windows to run more than one application at a time significantly affects how you write applications. Although many computers have at least 640K of memory, this memory rapidly becomes limited as the user loads and runs more applications. With Windows, you must be aware of how your application uses memory and be prepared to minimize the amount of memory your application occupies at any given time.

To help you manage your application's use of memory, Windows uses the same memory-management system for your application's code and data segments that you use within your application to allocate and manage global memory objects. When the user starts your application, Windows allocates space for the code and data segments in global memory and then copies the segments from the executable file into memory. These segments can be fixed, movable, and even discardable. You specify their attributes in the application's module-definition file.

You can reduce the effect your application has on memory by using movable code and data segments. By using movable segments, you enable Windows to take advantage of free memory as the memory becomes available.

By using discardable code segments, you can further reduce the effect your application has on memory. If you make a code segment discardable, Windows discards it, if necessary, to satisfy requests for global memory. Unlike ordinary memory objects that you may allocate, discarded code segments are monitored by Windows, which automatically reloads them if your application attempts to execute code within them. This means that your application's code segments are in memory only when they are needed.

Discarding a segment destroys its contents. Windows does not save the current contents of a discarded segment. Instead, it treats the segment as if it were no different than when originally loaded and will load the segment directly from the executable file when it is needed.