The ComputerInfo class in the .NET Framework is a little hidden gem which represents the useful information about your computer. This class is defined in the Microsoft.VisualBasic assembly which might be the main reason C# developers are not aware of it. In this article we are going to learn how to use the Computerinfo class to display the memory utilization information on the page.

ComputerInfo Class:

Apart from the TotalPhysicalMemory and AvailablePhysicalMemory the ComputerInfo class also exposes properties for TotalVirtualMemory and AvailableVirtualMemory. There are many ways of displaying the information on the page but we are going to use Ajax since we want to update the information at regular intervals.

We have created a simple web service which exposes getMemoryStatistics method. The purpose of the method is to return the memory statistics in the form of a string. The StringBuilder class is used to concatenate all the information together. The memory is returned as number of bytes in the form of ULong data type.  The implementation is shown below:



Apart from returning the TotalPhysicalMemory and AvailablePhysicalMemory we are also returning the percentage of memory consumption.

Using the getMemoryStatistics Method:

Next step is to create a client which can use the getMemoryStatistics method to display the information to the user. Our client is a simple ASP.NET web application. We will be using the ASP.NET Ajax framework for our asynchronous calls.

The client implementation is shown below:



The window.setInterval method is responsible for calling the getMemoryStatistics function at regular intervals. The JavaScript function getMemoryStatistics calls the server side method which returns the memory information in the form of string. A simple DIV control is used to display the result on the screen.  The screenshot below shows the result:



The memory consumption percentage will increase as you open different applications and will go down as the resources are released. This will be discussed in the screencast in the future.

Conclusion:

In this article we learned how to display memory utilization information using the ComputerInfo class contained in the Microsoft.VisualBasic assembly.  In the future articles we will discuss how to show memory consumption as per running application.

[Download Sample]

Screencast