Writing memory-efficient applications requires practice. Here are some suggestions to guide you while you design and program applications for an H/PC.
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.
Once the system is in a low-memory regime, any memory allocation greater than 8 KB has the potential to fail. Because of this, your application should not allocate large amounts of memory in its shutdown code. The user already knows that a low-memory situation exists.
Small memory allocations almost never fail. Before this type of allocation fails, the user has been sent both low-memory and critical-memory warnings, in the form of System Out of Memory dialog boxes, and has had an opportunity to respond.
If you are using large data sets, consider loading only the data that an operation needs immediately, especially if the data will not be modified in the process. When the data is no longer needed, write it back to the file immediately. By loading data on demand, you gain memory, although you lose speed.
If you are not modifying data, declare the data as constant so the kernel can remove it if necessary. The kernel will restore the data the next time it accesses the page.
Many programs use temporary files that are deleted when the program terminates. Though this may be convenient, such files are a burden on memory resources while the temporary files exist.
Consider not using temporary files. Though eliminating the use of temporary files may decrease speed, simplicity, or perhaps the robustness of the application, the increase in memory efficiency is sufficient to offset the liabilities of this approach. If possible, design applications that can modify their own use of temporary files depending on the state of memory resources.
Data stored in text format and as uncompressed bitmaps uses memory inefficiently. Although some compression takes place when files are loaded from a desktop computer to an H/PC, you need to use the memory-saving techniques of compression and decompression fully for maximum benefit.
While one of the most appealing features of Windows-based platforms is the rich graphical environment you can create, graphics use a lot of memory. To conserve memory, you must limit your use of bitmaps and other graphic displays. One approach to conserving memory while retaining bitmaps is to use an algorithm to generate the image on demand, rather than loading the bitmap or other graphics file into memory. Though you gain memory with this approach, you lose speed. Generating images on demand works best for line drawings, though it is practical for other types of graphics as well.
An application that downloads large amounts of data over a modem, such as e-mail or stock quotations, may require more memory than the system has available. Use GetStoreInformation to determine the amount of free memory in the object store prior to the download, and then warn the user of potential problems.
Though an application that automatically creates backup files protects the user from unintentional data loss, the backup files use valuable memory. If your application supports backup files, prompt the user to turn off automatic backups if memory is low.
If the application has a desktop component, be sure it is sensitive to the memory limitations of the Windows CE-based platform. When downloading an application or data, inform the user if the system has too little memory remaining for the device to operate effectively.