The working set of an application is trimmed when its top-level window is minimized (293215)



The information in this article applies to:

  • Microsoft Win32 Application Programming Interface (API), when used with:
    • the operating system: Microsoft Windows NT 4.0
    • the operating system: Microsoft Windows 2000
    • the operating system: Microsoft Windows XP

This article was previously published under Q293215

SYMPTOMS

When an application's top-level window is minimized through the Minimize command from its System menu or a click on its Minimize button, the operating system will trim the working set for the process. This is done to free up RAM for foreground applications. As a result of this trimming, a process may experience significantly poorer performance because its memory pages are being faulted back into RAM.

MORE INFORMATION

If an application minimizes its top-level window programmatically by calling the ShowWindow() API with the SW_MINIMIZE command, the working set of the process will be trimmed. However, the working set will not be trimmed when the window is programmatically minimized through the use of ShowWindow() with the SW_SHOWMINIMIZED command.

The working set of a process is the set of memory pages currently visible to the process in physical RAM. Access to this memory is very fast because the pages are resident and available for an application to use without triggering a page fault.

A process can explicitly trim its own working set by calling the SetProcessWorkingSetSize() API while passing "-1" for both the dwMinimumWorkingSetSize and dwMaximumWorkingSetSize parameters. This is essentially how the system trims the process when its top-level window is minimized. This does not mean that the memory pages used by the process are immediately discarded from RAM. In fact, these pages may remain resident for quite a while. They are simply flagged so that the system can use them for other processes as necessary. This is significantly faster than waiting on the system's standard trimming algorithm.

When a window is minimized through the Minimize command from its System menu or the Minimize button, the window is sent a WM_SYSCOMMAND message with the SC_MINIMIZE command. If the window procedure passes this message on to the system's default window procedure by calling DefWindowProc(), the default procedure will handle the message by calling ShowWindow() with the SW_MINIMIZE command. As stated earlier, this will cause the process working set to be trimmed.

You can write an application that can be minimized without having its working set trimmed. To do this, the window procedure for the top-level window should intercept the WM_SYSCOMMAND message and respond to the SC_MINIMIZE command by calling ShowWindow() directly with the SW_SHOWMINIMIZED command. It should not pass the SC_MINIMIZE command on to DefWindowProc().

Sample Code

The following code demonstrates how to write a window procedure that intercepts the WM_SYSCOMMAND message to bypass the default window procedure and prevent the process working set from being trimmed:
LRESULT CALLBACK WndProc (HWND hWnd, UINT iMsg, WPARAM wParam, 
      LPARAM lParam) {

   switch (iMsg) {

      case WM_SYSCOMMAND:
         if (wParam == SC_MINIMIZE) {

            // programmatically minimize the window
            ShowWindow(hWnd, SW_SHOWMINIMIZED);

            // do not pass the minimize command on to the system's
            // default window procedure
            return 0;
         }

         // allow other system commands to be passed on to the
         // default window procedure
         break; 

      // handle other window messages here...
      case WM_WHATEVER:
         break;
   }

   return DefWindowProc(hWnd, iMsg, wParam, lParam);
}
				

Modification Type:MajorLast Reviewed:5/19/2005
Keywords:kbAPI kbKernBase kbMemory kbprb KB293215