Handling Low Memory Situations

No matter how effectively you use the VirtualAlloc, LocalAlloc and HeapCreate functions to allocate memory, and how efficiently your application uses RAM, the system may run low on memory. When memory is low, the VirtualAlloc function cannot find any unallocated pages. This can cause your application to stop functioning.

At a programming level, a low-memory situation can manifest itself to the application in the following ways:

The first two failures are returned in an orderly fashion and the user receives warning messages. These failures occur following a call by the application to a function, such as CreateWindow. The third manifestation has more serious consequences because it causes the process to wait for a free page, and causes the program to stop responding.

To avoid the problems associated with low memory, the system constantly monitors the amount of memory available and tries to prevent low memory situations from occurring. It does this in several ways. When an application attempts to allocate memory, the system filters the request. Filtering prevents a single application from using all available memory with one large allocation by lowering the maximum allocation limit. When the system enters a low memory situation, the system lowers the memory limit further.

Another way that the system tries to prevent low-memory situations is by sending warning messages to applications. When available memory falls below a hibernation threshold, the system enters the limited-memory regime from which it asynchronously sends a WM_HIBERNATE message to each active application. This message warns the application that available memory is scarce.

Windows CE uses two additional thresholds, low and critical, that define successively more restrictive memory regimes. Values for memory thresholds are described in the following table. The values are based on a 1 KB memory page; values for a 4 KB memory page are provided in parentheses.

Threshold Value Description
Hibernation threshold 128 KB

(160 KB)

The point at which the system enters a limited-memory state. The system sends a WM_HIBERNATE message when its memory falls below this value.
Low-memory threshold 64 KB

(96 KB)

The minimum available memory size that the system must maintain when it is in a low-memory state.
Critical-memory threshold 16 KB

(48 KB)

The minimum available memory size that the system must maintain when it is in a critical-memory state.

Maximum memory allocations in limited-memory states are described in the following table.

Maximum allocations in limited-memory states Value Description
Low Memory 16 KB The maximum amount of memory that the system allows Virtual Alloc to allocate when the system is in a low-memory state.
Critical Memory 8 KB The maximum amount of memory that the system allows VirtualAlloc to allocate when the system is in a critical-memory state.

The system must respond to four limited-memory scenarios in Windows CE. These scenarios occur when an application does one of the following:

If an application calls VirtualAlloc and requests less memory than the low-memory maximum, but enough to cause the system to fall below the low-memory threshold, the system displays a System Out of Memory dialog box. This dialog box is discussed in more detail in the next section. In this scenario, the user must either select applications for the system to close or allocate more RAM to program memory. If the user chooses to close applications, the system sends WM_CLOSE messages. If an application receives a WM_CLOSE message but does not shut down within eight seconds, the system displays an End Task/Wait dialog box. This gives the user the choice of terminating the application or to see if the application closes itself.

Any VirtualAlloc call fails if it causes the amount of available memory in the system to fall below the low-memory threshold. The system does not display the System Out of Memory dialog box. LocalAlloc does not necessarily have the same constraint as VirtualAlloc. Depending on the state of the system heap, a function call to LocalAlloc can succeed.

If an application calls VirtualAlloc to request less memory than the critical-memory maximum, the system displays the System Out of Memory dialog box. The user must either select applications for the system to close or increase the amount of RAM allocated to program memory, if there is any available RAM. The system calls TerminateProcess to terminate any applications selected by the user.

Applications that call VirtualAlloc fail if the function causes the amount of available memory in the system to fall below the critical-memory threshold. Depending on the state of the system heap, a function call to LocalAlloc can succeed.