Jump to content
  • Advertisement
Sign in to follow this  
DrGUI

[.net] Finding Available Memory

This topic is 4796 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi! I want to know what the best way is to get the amount of free memory! Should I use the COM function or the WMI in .NET? If the answer is WMI should I be using the 'Win32_OperatingSystem' class? Cheers! ~Will rate for a pet monkey~

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
in what language?

Share this post


Link to post
Share on other sites

Don't know if you can use .Net classes, maybe in .NET 2.0 or some WMI classes. But here's something to help you get started:


using System;
using System.Data;
using System.Runtime.InteropServices;

/// <summary>
/// This class extracts current available memory
/// </summary>
///
[StructLayout(LayoutKind.Sequential)]
public struct MEMORYSTATUS
{
public uint dwLength;
public uint dwMemoryLoad;
public uint dwTotalPhys;
public uint dwAvailPhys;
public uint dwTotalPageFile;
public uint dwAvailPageFile;
public uint dwTotalVirtual;
public uint dwAvailVirtual;
}

public class Memory
{

[DllImport("kernel32")]
static extern void GlobalMemoryStatus(ref MEMORYSTATUS pSI);

[DllImport("kernel32")]
static extern uint GetDiskFreeSpace(
string lpRootPathName,
ref uint lpSectorsPerCluster,
ref uint lpBytesPerSector,
ref uint lpNumberOfFreeClusters,
ref uint lpTotalNumberOfClusters);


private const int MEGABYTE = (1024 * 1024);

public Memory()
{
//
// TODO: Add constructor logic here
//
}

/// <summary>
/// Returns a table with current memory configuration
/// </summary>
/// <param name="guid">A global identifier linking this memory to an inventory</param>
/// <returns>Datatable containing memory rows</returns>
public static DataTable getMemory(Guid guid)
{
// opening connection and deleting all contents
DataTable dt = new DataTable("Memory");
dt.Columns.Add("mem_id", Type.GetType("System.Guid"));
dt.Columns.Add("inv_id", Type.GetType("System.Guid"));
dt.Columns.Add("mem_ram", Type.GetType("System.Int32"));
dt.Columns.Add("mem_dsk", Type.GetType("System.Int32"));
dt.Columns.Add("mem_dskfree", Type.GetType("System.Int32"));

DataRow dr = dt.NewRow();
dr["mem_id"] = Guid.NewGuid();
dr["inv_id"] = guid;

// getting memory information
MEMORYSTATUS memStatus = new MEMORYSTATUS();
GlobalMemoryStatus(ref memStatus);

uint sectors = 0, bytes = 0, freeC = 0, totalC = 0;
GetDiskFreeSpace(@"C:\", ref sectors, ref bytes, ref freeC, ref totalC);
dr["mem_ram"] = memStatus.dwTotalPhys / MEGABYTE ;
dr["mem_dsk"] = ((long)totalC * sectors * bytes) / MEGABYTE;
dr["mem_dskfree"] = ((long)freeC * sectors * bytes) / MEGABYTE;

dt.Rows.Add(dr);
return dt;

}

}

Share this post


Link to post
Share on other sites
Thanks!

I've used GetGlobalMemoryStatus in VB6, but I'd really prefer a more .NET way...like we said, I'll have to look at WMI classes on MSDN. I think WMI only works on Windows 2000 but I think the latest DirectX also has that requirement so that'll probably be fine!

Can anyone confirm whether DirectX June release works only on Win 2k and above? I still stuck with December though because I still have Win 2k grrr

Cheers

Share this post


Link to post
Share on other sites
I would personally go with WMI, This will also allow you to connect to remote machines (Even if you have to use another users access). As all systems that use .net are able to use WMI you would not have a problem there.

Share this post


Link to post
Share on other sites
.NET already has that implemented, just use the System.Diagnostics.PerformanceCounter class:


PerformanceCounter pc = new PerformanceCounter( "Memory", "Available MBytes" );
try
{
Console.WriteLine( "Available RAM: " + pc.NextValue().ToString() + "MB" );
}
finally
{
pc.Close();
}






PerformanceCounter uses unmanaged resources, thats why i used a try/finally block, so it gets properly disposed when its done.

Share this post


Link to post
Share on other sites
Quote:
Original post by Mykre
I would personally go with WMI, This will also allow you to connect to remote machines (Even if you have to use another users access). As all systems that use .net are able to use WMI you would not have a problem there.

Thanks! Does .NET run only on 2000 and above? Just wondering...but I'll check up on that myself thanks!

Thanks a2ps, I was considering performance counters; that will save me some time finding out the particular counter though! Maybe it was the performance counters working only on XP then!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!