Calculating Free System Resources in Microsoft Windows

Last reviewed: July 22, 1997
Article ID: Q61803
3.00 3.10 WINDOWS kbprg

The information in this article applies to:

  • Microsoft Windows Software Development Kit (SDK) for Windows versions 3.1 and 3.0

SUMMARY

In the Microsoft Windows graphical environment, the About dialog box in the Program Manager displays a value that indicates the amount of free system resources available at a given time. The text below documents this calculation and the methods that an application can use to calculate this figure.

MORE INFORMATION

Windows uses the same method to calculate the percentage of free system resources in each of its operating modes (real mode, standard mode, and enhanced mode under Windows 3.0, and standard mode and enhanced mode under Windows 3.1).

In the Windows environment, system resources are the amount of space available in the data segment (DS) for the user dynamic-link library (DLL) or the GDI DLL. Because system resources are exhausted if either of these data segments fills up, Windows reports the smaller of the two percentages for the system. The following formula describes the calculation for Windows 3.0 and 3.1; this may or may not be accurate in future versions of Windows:

   % free = min (% free of user DS, % free of GDI DS)

               (64K - current DS size) + free blocks in Heap
   % free DS = --------------------------------------------- * 100
                     64K - (size of statics and stack)

Or, in other words:

                 Current free heap assuming 64K
   % free DS =  -------------------------------- * 100
                   Maximum heap assuming 64K


Under Windows 3.1, an application can call the GetFreeSystemResouces function with the GFSR_SYSTEMRESOURCES value to obtain the percentage of free system resources. Because this function is not present in Windows 3.0, an application that runs in both environments must explicitly import this function from the user dynamic-link library (DLL) when the application runs under Windows 3.1.

Under Windows 3.0, an application can use the GetHeapSpaces function to determine the percentage of free system resources as follows:

   DWORD cbFree;
   WORD  wFreeK, wHeapK;
   WORD  wUserPercent, wGDIPercent, wSysPercent;

   cbFree = GetHeapSpaces(GetModuleHandle("USER"));
   wFreeK = LOWORD(cbFree) / 1024;
   wHeapK = HIWORD(cbFree) / 1024;
   wUserPercent = MulDiv(wFreeK, 100, wHeapK);

   cbFree = GetHeapSpaces(GetModuleHandle("GDI"));
   wFreeK = LOWORD(cbFree) / 1024;
   wHeapK = HIWORD(cbFree) / 1024;
   wGDIPercent = MulDiv(wFreeK, 100, wHeapK);

   wSysPercent = min(wUserPercent, wGDIPercent);

Because this technique does not provide accurate results under Windows 3.1, an application must determine the version of Windows with which it is running and perform the calculation accordingly.


Additional reference words: 3.00 3.10
KBCategory: kbprg
KBSubcategory: UsrSys
Keywords : kb16bitonly


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: July 22, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.