By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,968 Members | 1,871 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,968 IT Pros & Developers. It's quick & easy.

available memory

P: n/a
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

Nov 16 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Hi Kovan,

You could use the PerformanceCounter class from System.Diagnostics.

Cheers,
Allan.
"Kovan Akrei" <ko****@ifi.uio.no> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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

Nov 16 '05 #2

P: n/a
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

"Kovan Akrei" <ko****@ifi.uio.no> schreef in bericht
news:%2****************@tk2msftngp13.phx.gbl...
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

Nov 16 '05 #3

P: n/a
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","Retrei ves 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

"Allan Taunt" <al*************@NO-hotmail-SPAM.com> wrote in message
news:e0**************@TK2MSFTNGP09.phx.gbl...
Hi Kovan,

You could use the PerformanceCounter class from System.Diagnostics.

Cheers,
Allan.
"Kovan Akrei" <ko****@ifi.uio.no> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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


Nov 16 '05 #4

P: n/a
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.
"Kovan Akrei" <ko****@ifi.uio.no> wrote in message
news:uS**************@TK2MSFTNGP11.phx.gbl...
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","Retrei ves 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

"Allan Taunt" <al*************@NO-hotmail-SPAM.com> wrote in message
news:e0**************@TK2MSFTNGP09.phx.gbl...
Hi Kovan,

You could use the PerformanceCounter class from System.Diagnostics.

Cheers,
Allan.
"Kovan Akrei" <ko****@ifi.uio.no> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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



Nov 16 '05 #5

P: n/a
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

"Allan Taunt" <al*************@NO-hotmail-SPAM.com> wrote in message
news:uD**************@TK2MSFTNGP11.phx.gbl...
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.
"Kovan Akrei" <ko****@ifi.uio.no> wrote in message
news:uS**************@TK2MSFTNGP11.phx.gbl...
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","Retrei ves 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

"Allan Taunt" <al*************@NO-hotmail-SPAM.com> wrote in message
news:e0**************@TK2MSFTNGP09.phx.gbl...
Hi Kovan,

You could use the PerformanceCounter class from System.Diagnostics.

Cheers,
Allan.
"Kovan Akrei" <ko****@ifi.uio.no> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
> 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
>
>
>



Nov 16 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.