Determine memory usage

The GlobalMemoryStatus API function returns detailed information about the current load on the physical and virtual memory. You can use the following code to display the current amount of free memory:

    Dim ms As MEMORYSTATUS    Dim msg As String        ' query for memory data    ms.dwLength = Len(ms)    GlobalMemoryStatus ms    ' create a result message.    msg = "Total physical memory = " & GetKbytes(ms.dwTotalPhys) & vbCrLf & _        "Available physical memory = " & GetKbytes(ms.dwAvailPhys) & vbCrLf & _        "Memory Load = " & ms.dwMemoryLoad & "%" & vbCrLf & _        "Total Page file = " & GetKbytes(ms.dwTotalPageFile) & vbCrLf & _        "Available Page file = " & GetKbytes(ms.dwAvailPageFile) & vbCrLf & _        "Total Virtual memory = " & GetKbytes(ms.dwTotalVirtual) & vbCrLf & _        "Available Virtual memory = " & GetKbytes(ms.dwAvailVirtual)    MsgBox msg, vbInformation, "Memory information"

The GetKbytes function is a simple routine that converts a number of bytes into Kbytes, and adds thousand separators:

' Format a memory amoung as a Kbyte or MegabyteFunction GetKbytes(ByVal amount As Long) As String    ' convert to Kbytes    amount = amount  1024    GetKbytes = Format(amount, "###,###,###K")End Function

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: