16.6.1 Using Code-Segment Attributes

Windows uses the same memory-management facilities for handling code segments as it does for handling data segments. You can, and generally should, partition your application into separate code segments. You can declare a particular code segment to be fixed, movable, or discardable, just as you can for the application's automatic data segment and global objects.

In your application's .DEF file, you can use the CODE statement to specify whether the code segments are by default fixed, movable, or discardable. For example, the following statement declares that the default attribute of all code segments will be MOVEABLE:

CODE MOVEABLE;

For information about overriding this default attribute for specific code segments, see Section 16.6.2, “Using Multiple Code Segments.”

If you declare your code segments as discardable, Windows can free memory held by those code segments when it is necessary to allocate additional memory. Because a code segment is always unmodifiable, there is no risk that information will be lost when it is discarded. When your application makes a call to a code segment that is not currently in memory, Windows will first load it from the .EXE file. If a discardable code segment is not in memory, however, Windows requires extra time to load the segment from disk. On the other hand, this penalty is minimized because Windows uses a least-recently-used (LRU) algorithm for discarding segments, and so Windows does not discard frequently used segments.