title: Visual Basic Physical Memory
body: <p>I'm trying to get the amount of physical memory (Total RAM) installed on my PC through Visual Basic.The problem is that I get as a return "0 bytes".Further I'll also try yo get the percentage of use,the amount of free RAM,Total paging,free page and a graph showing the usage like resource monitor for RAM,in Windows.The problem is that I can't get the correct amount of free RAM first to advance with the others.</p>
  
  <p>What I'm doing wrong?
  Thank you.</p>
  
  <p>This is my code:</p>
  
  <pre><code>Option Strict On
  Option Explicit On
  Imports System.Math
  Imports System.Management
  Imports System.Runtime.InteropServices
  Public Class Form1
      Inherits System.Windows.Forms.Form
  #Region " API "
      &lt;StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)&gt; _
      Private Structure MEMORYSTATUSEX
          Dim dwLength As Integer
          Dim dwMemoryLoad As Integer
          Dim ullTotalPhys As ULong
      End Structure
      Private memoryInfo As MEMORYSTATUSEX
      Private Declare Auto Sub GlobalMemoryStatusEx Lib "kernel32" (ByRef lpBuffer As MEMORYSTATUSEX)
  #End Region
  
  
  #Region " Variables "
  
      Private mullTotalRAM As ULong
  
  #End Region
  
  #Region " Form Events "
      Private Sub Form1_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
          ' set title
          Me.Text = My.Application.Info.Title &amp; " " &amp; My.Application.Info.Version.Major.ToString &amp; "." &amp; _
              My.Application.Info.Version.Minor.ToString
  
          Application.DoEvents()
          GetMemoryInfo()
          Timer1.Enabled = True
      End Sub
  
  #End Region
  
  #Region " Information Gathering and Display "
  
      Private Sub GetMemoryInfo()
  
          System.Windows.Forms.Application.DoEvents()
  
          ' set size of structure (required by this api call)
          memoryInfo.dwLength = Marshal.SizeOf(memoryInfo)
          GlobalMemoryStatusEx(memoryInfo)
  
          mullTotalRAM = memoryInfo.ullTotalPhys
  
          txtRAM.Text = FormatBytes(mullTotalRAM)
  
      End Sub
  
  #End Region
  
  #Region " Update Timer "
  
      Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)
  
          GetMemoryInfo()
  
          Application.DoEvents()
  
      End Sub
  
  #End Region
  
  
  #Region " Formatting Routines "
  
      Private Function FormatBytes(ByVal ullBytes As ULong) As String
          Dim dblTemp As Double
  
          Try
              Select Case ullBytes
                  Case Is &gt;= 1073741824 'GB
                      dblTemp = CDbl(ullBytes / 1073741824)
                      Return FormatNumber(dblTemp, 2) &amp; " GB"
                  Case 1048576 To 1073741823
                      dblTemp = CDbl(ullBytes / 1048576) 'MB
                      Return FormatNumber(dblTemp, 0) &amp; " MB"
                  Case 1024 To 1048575
                      dblTemp = CDbl(ullBytes / 1024) 'KB
                      Return FormatNumber(dblTemp, 0) &amp; " KB"
                  Case 0 To 1023
                      dblTemp = ullBytes ' bytes
                      Return FormatNumber(dblTemp, 0) &amp; " bytes"
                  Case Else
                      Return ""
              End Select
          Catch
              Return ""
          End Try
  
      End Function
  
  
  #End Region
  
  
      Private Sub ramaTotalRAM_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ramaTotalRAM.Enter
  
      End Sub
  
      Private Sub txtRAM_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtRAM.TextChanged
  
      End Sub
  End Class
  </code></pre>
  
  <p>I've resolved this problem.Now I'm wondering if it is possible to make something like this:
  <a href="http://s18.postimage.org/7zn5adst3/Memory.jpg" rel="nofollow">http://s18.postimage.org/7zn5adst3/Memory.jpg</a> .How can I do it.thank you very much.</p>