4 Replies Latest reply on Nov 5, 2013 5:15 PM by Jacob Picolet

    Find RAM Memory Usage

    Jacob Picolet

      Does anyone know how to gather the amount of RAM being used by your computer?  Whether it is total or just by SolidWorks I don't care.  I know you can use Environ$ to get certain information from the computer, but RAM usage has eluded me.  I wasn't sure if you could interface with the command prompt window with SolidWorks API?  I know using "wmic OS get FreePhyscialMemory"  and/or "wmic OS get FreeVirtualMemory" in the command prompt window return useful values. I'm trying to run some baseline tests using a macro, but feel like knowing the amount of RAM being used would be a big factor.

        • Re: Find RAM Memory Usage
          Artem Taturevych

          You need to use the GlobalMemoryStatus Windows API functions. Here are the references:

           

          http://www.a1vbcode.com/snippet-181.asp

          http://stackoverflow.com/questions/1404731/how-to-get-current-cpu-and-ram-usage-in-vb-6

          ____________________________________________________

          Regards,

          Artem Taturevych, Application Engineer at Intercad (Australia)

           

          translationXpert – add-in to translate SolidWorks models

          myIntercad an integrated tool for SolidWorks Professionals

          LinkedIn SolidWorks API Education Group

            • Re: Find RAM Memory Usage
              Jacob Picolet

              So I'm not quite sure what I'm doing wrong here(I pretty much copied and pasted most of it), but I'm not getting the values I should be.  The Memory Load, of all things, is the only thing that is correct.  After some reading several articles suggest using GlobalMemoryStatusEx, but that results in all zeros which means it failed. So not really for sure where to go here.

               

              I have 12GB of Physical Memory, roughly 8,882Mb of Avail Physical Mem, 24,569Mb max virtual mem, 20,233Mb avail virtual mem, and 4,336Mb in use virtual mem.

               

              API output.JPG

               

              Code:

               

              Dim swApp As Object

              Public Declare PtrSafe Sub GlobalMemoryStatus Lib "kernel32" (lpBuffer As MEMORYSTATUSEX)

               

               

              Public Type MEMORYSTATUSEX

                     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

               

               

              Global Mem As MEMORYSTATUSEX

               

               

              Sub main()

               

               

              Set swApp = Application.SldWorks

               

               

              'Add this code to get the values:

               

               

              Mem.dwLength = Len(Mem)

              GlobalMemoryStatus Mem

               

               

              'After calling the GlobalMemoryStatus API,

              'you can use the MEMORYSTATUS type to access

              'the memory information:

               

               

              'Mem.dwMemoryLoad contains percentage memory used

              'Mem.dwTotalPhys - total amount of physical memory

              'Mem.dwAvailPhys - available amount of physical memory

              'Mem.dwTotalPageFile - total amount of memory in swap file

              'Mem.dwAvailPageFile - available amount of memory in swap file

              'Mem.dwTotalVirtual - total amount of virtual memory

              'Mem.dwAvailVirtual - available amount of virtual memory

               

               

               

               

              MsgBox "Memory Load: " & Mem.dwMemoryLoad & ", Total Physical: " & Mem.dwTotalPhys & ", Available Physical: " & Mem.dwAvailPhys & ", TotalPageFile: " & Mem.dwTotalPageFile & ", Avail Page File: " & Mem.dwAvailPageFile & ", Total Virtual Memory: " & Mem.dwTotalVirtual & ", Total Avail Virtual: " & Mem.dwAvailVirtual

               

               

              End Sub

            • Re: Find RAM Memory Usage
              Jacob Picolet

              Simon was right about the LongLongs. Below is the adjusted code I used. Thanks for the help everyone.

               

              Dim swApp As Object

              Dim div As Double

              Public Declare PtrSafe Sub GlobalMemoryStatus Lib "kernel32" (lpBuffer As MEMORYSTATUSEX)

               

              Public Type MEMORYSTATUSEX

                     dwLength As Long

                     dwMemoryLoad As Long 'contains percentage memory used

                     dwTotalPhys As LongLong 'total amount of physical memory

                     dwAvailPhys As LongLong 'available amount of physical memory

                     dwTotalPageFile As LongLong 'total amount of memory in swap file

                     dwAvailPageFile As LongLong 'available amount of memory in swap file

                     dwTotalVirtual As LongLong 'total amount of virtual memory

                     dwAvailVirtual As LongLong 'available amount of virtual memory

              End Type

              Global Mem As MEMORYSTATUSEX

               

               

              Sub main()

               

              Set swApp = Application.SldWorks

              div = 1073741824 ' Conversion factor

               

              'Add this code to get the values:

              Mem.dwLength = Len(Mem)

              GlobalMemoryStatus Mem

               

              'After calling the GlobalMemoryStatus API,

              'you can use the MEMORYSTATUSEX type to access

              'the memory information:

               

              MsgBox "Memory Load: " & Mem.dwMemoryLoad & "%, Total Physical: " & Round(Mem.dwTotalPhys / div, 3) & "GBs, Available Physical: " & Round(Mem.dwAvailPhys / div, 3) & "GBs, TotalPageFile: " & Round(Mem.dwTotalPageFile / div, 3) & "GBs, Avail Page File: " & Round(Mem.dwAvailPageFile / div, 3) & "GBs, Total Virtual Memory: " & Round(Mem.dwTotalVirtual / div, 2) & ", Total Avail Virtual: " & Round(Mem.dwAvailVirtual / div, 2)

               

               

              End Sub