Math Percentage

TyB

Well-known member
Joined
May 14, 2009
Messages
102
Programming Experience
3-5
I am trying to get a percentage that just does not seem to work.

I am using WMI to get the Total Physical memory of my system and then the Free Physical Memory. Then get the percentage.

2147483648 - Total Memory
838028 - Free memory

According to Task Manager my physical memory is 59%

This is the code I am using but I can not get 59% or 41% that is free.

temp = FM/TM

temp = temp * 100


percent = FM * temp

Any help would be appreciated.

Thanks Ty
 
I am trying to get a percentage that just does not seem to work.

I am using WMI to get the Total Physical memory of my system and then the Free Physical Memory. Then get the percentage.

2147483648 - Total Memory
838028 - Free memory

According to Task Manager my physical memory is 59%

This is the code I am using but I can not get 59% or 41% that is free.

temp = FM/TM

temp = temp * 100


percent = FM * temp

Any help would be appreciated.

Thanks Ty

Here is a tested and tried formula but I also do not come to 59% and looking at your numbers anyone can see the the FM against the TM can definitely not be 59%. Maybe you typed the numbers wrong but anyway here is the formula.

percent = (FM*100)/TM and it gives me 0.03902372. Let me know what you think.
 
mmmmmm..

Manster,
Thanks for the help. Soemthing is just not right so I thought I would post all my code so it can be tried to see what others get.

First this gets the total memory of the PC.
VB.NET:
    Private Function GetTotalMemory() As Long
        Dim lngtemp As Long
        Dim lngReturn As Long

        Try
            Dim searcher As New ManagementObjectSearcher( _
                "root\CIMV2", _
                "SELECT * FROM Win32_PhysicalMemory")

            For Each queryObj As ManagementObject In searcher.Get()

                lngtemp = queryObj("Capacity")
                lngReturn = lngReturn + lngtemp

            Next
        Catch err As ManagementException
            MessageBox.Show("An error occurred while querying for WMI data: " & err.Message)
        End Try

        Return lngReturn
    End Function

This will get the Free memory.
VB.NET:
    Private Function GetPhysicalMemoryUsed() As Long
        Dim lngReturn As Long
        Dim lngtemp As Long
        Try
            Dim searcher As New ManagementObjectSearcher( _
                "root\CIMV2", _
                "SELECT * FROM Win32_OperatingSystem")

            For Each queryObj As ManagementObject In searcher.Get()

                lngtemp = queryObj("FreePhysicalMemory")

                lngReturn = lngReturn + lngtemp

            Next
        Catch err As ManagementException
            MessageBox.Show("An error occurred while querying for WMI data: " & err.Message)
        End Try


        Return lngReturn
    End Function

This is my calling and processing code.
VB.NET:
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim intData1 As Long
        Dim intData2 As Long
        Dim inttemp As Decimal
        Dim strpercent As String
        Dim intReturn As Long

        intData1 = GetTotalMemory()

        System.Diagnostics.Debug.WriteLine(intData1)

        intReturn = intData1 / 1073741824

        ListBox1.Items.Add(intReturn & " Gigs")

        intData2 = GetPhysicalMemoryUsed()

        System.Diagnostics.Debug.WriteLine(intData2)

        inttemp = (intData2 * 100) / intData1

        strpercent = FormatPercent(inttemp, , TriState.UseDefault)

        ListBox1.Items.Add("Free physical memory - " & strpercent)
    End Sub

Thanks Ty
 
Last edited:
VB.NET:
Dim free = My.Computer.Info.AvailablePhysicalMemory
Dim total = My.Computer.Info.TotalPhysicalMemory

MessageBox.Show((free / total).ToString("p0"), "Available Memory")
 
Well that was simple enough

Thanks!

Although I wonder why I cannot get the same from the WMI code.

Ty
 
Thanks!

Although I wonder why I cannot get the same from the WMI code.

Ty
Those numbers are in different units, one is bytes and the other is kilobytes.
 
Back
Top