devxlogo

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

Charlie has over a decade of experience in website administration and technology management. As the site admin, he oversees all technical aspects of running a high-traffic online platform, ensuring optimal performance, security, and user experience.

See also  Seven Service Boundary Mistakes That Create Technical Debt

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.