Home > DeveloperSection > Forums > Problem in getting the physical memory of system
Rajesh Goswami

Total Post:29

Points:203
Posted on    October-21-2011 3:18 AM

 C# C# 
Ratings:


 3 Reply(s)
 2670  View(s)
Rate this:
Hi,
There is some problem related to get the information about the system and total physical memory in system

Thanks in advance!


Chris Anderson
Chris Anderson

Total Post:65

Points:455
Posted on    October-21-2011 3:28 AM

Hi,

You can use the given code to retrieve the system memory in C#:

using System.Management;

ManagementObjectSearcher Search = new ManagementObjectSearcher("Select * From Win32_ComputerSystem");
            foreach (ManagementObject Mobject in Search.Get())
            {
                double Ram_Bytes = (Convert.ToDouble(Mobject["TotalPhysicalMemory"]));
                MessageBox.Show((Ram_Bytes / (1024 * 1024 * 1024)).ToString());
            }

Modified On Oct-21-2011 05:05:26 AM

Chris Anderson
Chris Anderson

Total Post:65

Points:455
Posted on    October-21-2011 5:07 AM

Hi,

You can also use the following code in order to retrieve physical memory (RAM) :

            double totalCapacity = 0;
            ObjectQuery objectQuery = new ObjectQuery("select * from Win32_PhysicalMemory");
            ManagementObjectSearcher searcher = new ManagementObjectSearcher(objectQuery);
            ManagementObjectCollection vals = searcher.Get();

            foreach (ManagementObject val in vals)
            {
                totalCapacity += System.Convert.ToDouble(val.GetPropertyValue("Capacity"));
            }

            Console.WriteLine("Total Machine Memory = " + totalCapacity.ToString() + " Bytes");
            Console.WriteLine("Total Machine Memory = " + (totalCapacity / 1024) + " KiloBytes");
            Console.WriteLine("Total Machine Memory = " + (totalCapacity / 1048576) + "    MegaBytes");
            Console.WriteLine("Total Machine Memory = " + (totalCapacity / 1073741824) + " GigaBytes");

Rajesh Goswami

Total Post:29

Points:203
Posted on    October-21-2011 8:04 AM

Thanks Rohit,

Now I got real solution again thanks.

Don't want to miss updates? Please click the below button!

Follow MindStick