Visual Basic System Services
GlobalMemoryStatus: System Memory Information
     
Posted:   Monday March 6, 1998
Updated:   Monday December 26, 2011
     
Applies to:   VB4-32, VB5, VB6
Developed with:   VB5, Windows 98
OS restrictions:   None
Author:   VBnet - Randy Birch
     
 Prerequisites
None.

In the 16-bit world of Windows, the global memory and user information was obtained using the GetFreeSpace and GetFreeSystemResources APIs.  Under Win32, these were dropped and replaced with the GlobalMemoryStatus API.

This demo is a quickie, showing how to retrieve the memory information of the system and display it in a label control array.  Combined with other system information-based calls, a concise summary of the users current environment can be displayed.

 BAS Module Code
None:

 Form Code
To create this project, add a command button (Command1) and two control arrays of seven labels each to the form (Label1(0)-(6) for the API results, and Label2(0)-(6) for the line descriptions). Add the following code:

Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ©1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Type MEMORYSTATUS
    dwLength As Long
    dwMemoryLoad As Long
    dwTotalPhys As Long
    dwAvailPhys As Long
    dwTotalPageFile As Long
    dwAvailPageFile As Long
    dwTotalVirtual As Long
    dwAvailVirtual As Long
End Type

Private Declare Sub GlobalMemoryStatus Lib "kernel32" _
   (lpBuffer As MEMORYSTATUS)



Private Sub Form_Load()

   Dim MS As MEMORYSTATUS
   
   MS.dwLength = Len(MS)
   GlobalMemoryStatus MS
   
  'divide the memory variables by 1024 (nkb) 
  'to obtain the size in kilobytes
   Label1(0).Caption = Format$(MS.dwMemoryLoad, "###,###,###,###") & " % used"
   Label1(1).Caption = Format$(MS.dwTotalPhys / 1024, "###,###,###,###") & " Kbyte"
   Label1(2).Caption = Format$(MS.dwAvailPhys / 1024, "###,###,###,###") & " Kbyte"
   Label1(3).Caption = Format$(MS.dwTotalPageFile / 1024, "###,###,###,###") & " Kbyte"
   Label1(4).Caption = Format$(MS.dwAvailPageFile / 1024, "###,###,###,###") & " Kbyte"
   Label1(5).Caption = Format$(MS.dwTotalVirtual / 1024, "###,###,###,###") & " Kbyte"
   Label1(6).Caption = Format$(MS.dwAvailVirtual / 1024, "###,###,###,###") & " Kbyte"

End Sub


Private Sub Command1_Click()

   Unload Me
   
End Sub
 Comments
Save & run the project.  The current system memory status will be displayed in the labels. The information returned by the GlobalMemoryStatus function is volatile .... there is no guarantee that two sequential calls to this function will return the same information. The MSDN describes the various members of the MEMORYSTATUS structure as:

dwLength
The size in bytes of the MEMORYSTATUS data structure. You do not need to set this member before calling the GlobalMemoryStatus function; the function sets it.

dwMemoryLoad
Specifies a number between 0 and 100 that gives a general idea of current memory utilization, in which 0 indicates no memory use and 100 indicates full memory use.

dwTotalPhys
Indicates the total number of bytes of physical memory.

dwAvailPhys
Indicates the number of bytes of physical memory available.

dwTotalPageFile
Indicates the total number of bytes that can be stored in the paging file. Note that this number does not represent the actual physical size of the paging file on disk.

dwAvailPageFile
Indicates the number of bytes available in the paging file.

dwTotalVirtual
Indicates the total number of bytes that can be described in the user mode portion of the virtual address space of the calling process.

dwAvailVirtual
Indicates the number of bytes of unreserved and uncommitted memory in the user mode portion of the virtual address space of the calling process.


 
 

PayPal Link
Make payments with PayPal - it's fast, free and secure!

 
 
 
 

Copyright ©1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy

 

Hit Counter