Get the Current Memory Status
 
by Sreejath

Introduction
In this article we'll see how we can obtain the Memory status of the system. I.e. the total installed RAM on the system, how much is available at the moment, total virtual memory, available virtual memory and so on. 

Note: This article assumes that you are familiar with the fundamentals of API programming. If however, API is a new concept to you, please do go through the first two articles of this series, which will help you to get a thorough grounding in this topic.

Create a Standard EXE project in VB.
Copy this code into the General Declarations section of the form that is added to the project by default:

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" Alias "GlobalMemoryStatus" (lpBuffer As MEMORYSTATUS)

Here we first declare a user defined type structure, MEMORYSTATUS. It contains eight members all of the type Long. A variable of this Type is passed ByRef as argument to the GlobalMemoryStatus API function. (See Articles #1 and #2 for more info on ByVal, ByRef and other such creatures associated with API programming.

The GlobalMemoryStatus function retrieves information about current available memory. The function returns information about both physical and virtual memory. The function populates it with the memory information parameters, which we can then determine by examining the appropriate variable.

Now, add the following code to any suitable event in your app where you want to display the memory status:

Dim memInfo As MEMORYSTATUS
GlobalMemoryStatus memInfo

MsgBox "Total memory (in bytes): " & memInfo.dwTotalPhys

MsgBox "Available memory (in bytes): " & memInfo.dwAvailPhys

Analysis
Here we first declare a variable of the type MEMORYSTATUS. Then we pass this variable as argument to the GlobalMemoryStatus function which we declared in the General Declarartion Section. This function "populates" this structure with the values of the various parameters at that instant.
From these we access two parameters and display them using a message box.
Note that the MEMORYSTATUS type has many more members, which can be used to obtained detailed info about the current memory status of the system.

The dwTotalVirtual member gives the total virtual memory of the system. It'd be a waste of time to explain all of them here as any decent API guide will contain this info. Be sure to explore them and do mail me (or post a comment here) if you need any help.

With a little work (and a few progress bars and a timer) you can design a memory status indicator that is all yours. 
Do this:

· Set the Timer's interval property to a suitable value.
· Set each progress bar's maximum value property to the Total value for that particular quantity (say dwTotalPhys or dwTotalVirtual). 
· In the Timer's OnTimer event handler, call the GlobalMemoryStatus function passing it a variable of the type MEMORYSTATUS as the argument.
· Now set the Value property of the various status bars to the Available values of the various parameters (dwAvailPhys or dwAvailVirtual) or Total - Available if you want to display the used portion.
That's it! YVO Memstat is ready to run. (YVO = Your Very Own, just in case you are wondering)
Do keep one thing in mind however; progress bars are somewhat heavy, in terms of memory. So your resource meter itself could end up consuming considerable resources on its own. Fear not, this too can be overcome to a certain extent if we use the API to create the progress bars. 
We'll discuss how to accomplish this in a future article.

Summary
In this article we saw how to use the GlobalMemoryStatus API function to obtain information about the memory status of the system. Please do keep in mind that, we’ve only covered a small portion of what can be achieved with this function. But the above example should be enough to get you started. If you have any doubts, questions or clarifications, please do feel free to mail me.