Handling Low-Memory Situations

Windows CE uses a series of memory thresholds to conserve system resources. The following table shows values for memory thresholds. 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 a system enters a limited-memory state. A system sends a WM_HIBERNATE message when its memory falls below this value.
Low-memory threshold 64 KB
(48 KB)
The minimum available memory that a system must maintain when it is in a low-memory state.
Critical-memory threshold 16 KB
(48 KB)
The minimum available memory that a system must maintain when it is in a critical-memory state.

The following table shows the maximum memory allocations that Windows CE allows in the low-memory and critical-memory states.

State
Value
Description
Low-memory 16 KB The maximum amount of memory that the system allows the VirtualAlloc function 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.

Windows CE must respond to four limited-memory scenarios. These scenarios occur when an application takes one of the following actions:

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 case, 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 seeing 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. The LocalAlloc function does not necessarily have the same constraints as VirtualAlloc. Depending on the state of the system heap, a call to LocalAlloc succeeds even if it makes memory fall below the low-memory threshold.

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 the TerminateProcess function 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 call to LocalAlloc can succeed even if it makes memory fall below the low-memory threshold.

Note The system polls memory every five seconds.

It is possible to totally skip over both the low-memory and critical memory thresholds. Whenever the system attempts to allocate more than 16 KB of memory, it has the potential to fail without displaying the System Out of Memory dialog box and without sending a low-memory warning to the user. In the same manner, any memory allocation greater than 8 KB, while in a low-memory state, has the potential to fail. Therefore, your application should not allocate large amounts of memory in its shutdown code. The user already knows that a low-memory situation exists. Instead, use small memory allocations, such as 4 KB or less, whenever possible. By consistently using small memory allocations in your applications, you avoid bypassing the low-memory and critical-memory thresholds. Your memory allocations activate the System Out of Memory dialog box and gives a user the opportunity to respond.