Sunday, June 14, 2009

How To get Total Memory of Computer

In this post I will show you how you can get the Total Ram of your computer. This example is in VB.net, after explaining the code I will tell you how you can use this code in your C# language and get the total memory of the computer. To get the total memory of the computer what you have to do is to import the Microsoft.VisualBasic.Devices namespace in your file and then use the ComputerInfo class of that namespace. The ComputerInfo class Provides properties for getting information about the computer's memory, loaded assemblies, name, and operating system. Here are some of the useful properties which are used to get the total physical and virtual memory.
Property Name
Description
AvailablePhysicalMemoryGets the total amount of free physical memory for the computer
AvailableVirtualMemoryGets the total amount of the computer's free virtual address space.
TotalPhysicalMemoryA double-precision floating point number data type
TotalVirtualMemoryGets the total amount of virtual address space available for the computer.
Here is the code which is used to get the Available Physical Memory, Available Virtual Memory, Total Physical Memory and the Total Virtual Memory.
Sub Main()
Dim computerInfo As New ComputerInfo

Console.WriteLine("Available Physical Memory: {0}", computerInfo.AvailablePhysicalMemory)
Console.WriteLine("Available Virtual Memory: {0}", computerInfo.AvailableVirtualMemory)
Console.WriteLine("Total Physical Memory: {0}", computerInfo.TotalPhysicalMemory)
Console.WriteLine("Total Virtual Memory: {0}", computerInfo.TotalVirtualMemory)

End Sub
And here is the output of the above code. Which display the Available Physical Memory, Available Virtual Memory, Total Physical Memory and the Total Virtual Memory.


If you want to use this code in your C# language what you have to do is to import the Microsoft.VisualBasic.Devices name space by using the using Statement in you file and then you will have the ComputerInfo class accessible in you code and you can use that class to get the information.

All and any comments / bugs / suggestions are welcomed!


2 comments:

Anonymous said...

Thanks Asim, it is very helpful for me and very descriptive.

Anonymous said...

Excelent