available memory

  • Thread starter Thread starter Kovan Akrei
  • Start date Start date
K

Kovan Akrei

Hi,
I wonder if it is possible to get hold of avaiable memory (only RAM) on a
machine through .Net class library? I do not want to call windws API.
I would like to use this to decide how many alive threads my program could
have each time the program runs. I use a number of threads in a simulation
tool.

Many thanks in advance.

Best regards
Kovan Akrei
 
Hi Kovan,

You could use the PerformanceCounter class from System.Diagnostics.

Cheers,
Allan.
 
Check out the System.Management namespace. You can get things like
FreePhysicalMemory, TotalVisibleMemorySize

Yves

// Example class
class Class1
{
[STAThread]
static void Main(string[] args)
{
ManagementClass memoryClass = new
ManagementClass("Win32_OperatingSystem");
ManagementObjectCollection memory = memoryClass.GetInstances();
foreach (ManagementObject mo in memory)
{
foreach (PropertyData pd in mo.Properties)
{
Console.WriteLine("{0} : {1}", pd.Name, pd.Value);
}
}
Console.ReadLine();
}
}

// END OF EXAMPLE CLASS
 
Hi,
Thanks for replying. I have looked at that class, but I cant figure out how
this class is able to read available memory. There seems to be no
persformanceCounterType suitable for my use.
I have declared the following counter, with some shortages pointed out in
comments

PerformanceCounter PC = new PerformanceCounter("Memory", "GetMemory");

if(!PerformanceCounterCategory.Exists("Orders")) {
CounterCreationData mem = new CounterCreationData();
mem.CounterName = "mem";
mem.CounterType = PerformanceCounterType.?; // What kind of
PerformanceCounterType should I use

CounterCreationData memAvailable = new CounterCreationData();
memAvailable.CounterName = "Availabe memory";
memAvailable.CounterType = PerformanceCounterType.?; // What kind of
CounterType should I use

CounterCreationDataCollection ccds = new
CounterCreationDataCollection();
ccds.Add(memAvailable);
ccds.Add(mem);

PerformanceCounterCategory.Create("Memory","Retreives available
memory",ccds);
}
else
{
Console.WriteLine("Category exists - Memory");
}

What should I do after this? to gather and retrive memory info? I assume
that I could use PC.NextValue to retrieve next availabe samle in this
performance counter. Is my assumption right?

regards
Kovan
 
Hi Kovan,

Simpler than that :-) You don't need to create a counter, use the built in
Memory, Available Bytes. ie:

PerformanceCounter pc = new PerformanceCounter("Memory","Available Bytes");
long availableMemory = Convert.ToInt64(pc.NextValue());
Console.WriteLine("Available Memory: {0}", availableMemory);

Cheers,
Allan.
 
Thanks Allan. That realy works :). By he way. Do you know how much memory
CLR allocates for a single thread (background and userthread) in a
multithreaded application? (I know that win32 OS allocates 1 MB of memory
for each process and are only able to support up to 2 GB om memory).

Kovan
 
Good stuff. As for the CLR mem allocation per thread - afraid I don't know
that one. Easiest option is probably just to run some tests and monitor
(PerformanceMonitor or some of the tools from www.sysinternals.com e.g.
Process Explorer could help here).

Perhaps someone else has an answer on this one?

Cheers,
Allan.
 
Back
Top