blah blah blah is here! blah blah » Close

up0down
link

My app needs to know how much RAM the machine it's running on has. How can this information be aquired?
Thanks in advance!

last answered 2 years ago

1 answers

up0down
link

I think you might be forced to do a Win32API call.

up0down
link

I don't mind using an API, but as to which one to use or which function I do not know.

up0down
link

Try this one:
Private Declare Sub GlobalMemoryStatus Lib "kernel32" (lpBuffer As MEMORYSTATUS)
Used like this: (vb 6 code)
Dim MemStat As MEMORYSTATUS
'retrieve the memory status
GlobalMemoryStatus MemStat
MsgBox "You have" + Str$(MemStat.dwTotalPhys / 1024) + " Kb total memory and" + Str$(MemStat.dwAvailPageFile / 1024) + " Kb available PageFile memory."

up0down
link

What the hell does "Dim" and "Sub" mean??
only kidding ;)

up0down
link

The GlobalMemoryStatus function included in kernel32.dll yields very strange results. For example it claims that my 512MB RAM machine has just over 2.3 million terabytes of RAM (where one terabyte equals 1,099,511,627,776 bytes). Any suggestions?

up0down
link

Yes, patent that RAM compression technology, QUICK! :-)
Actually, if you're sure your math is correct, no I don't have any suggestions. I didn't even see if it worked in .NET, I got some old code that worked in VB6 and assumed.
You could write a new algorythm, I guess, if the return form GlobalMemoryStatus is consistant. You'd have to check it on a few machines with different amouts of RAM.

up0down
link

The algorythm idea seemed like a good one; I tweeked the numbers until the function produced an accurate result on my 512MB computer, but then took it over to a 256MB computer and the program spat out 431MB. Is the amount of RAM stored in the registry somewhere? Any other means of finding it?

up0down
link

The windows create memory in our computer, not only for the RAM, also for the free space in our discs( you know that). Could it be that toose values are some how related to this factor??
Just an ideia

up0down
link

GlobalMemoryStatus only returns the Total memory in the system: Physical + Virtual

up0down
link

So how can I find the physical RAM alone on the system? (Sorry this question is dragging on so long; I appreciate your patience.)

up0down
link

I don't know if you have solved your problem, but I do have it too.
I want simply get the available physical memory using c#.net.
If you have any useful information, please respond.
Thanks in advance.

up0down
link

using System.Runtime.InteropServices;
...
public struct MemoryStatus {
public uint Length; //Length of struct
public uint MemoryLoad; //Value from 0-100 represents memory usage
public uint TotalPhysical;
public uint AvailablePhysical;
public uint TotalPageFile;
public uint AvailablePageFile;
public uint TotalVirtual;
public uint AvailableVirtual;
}
...
[DllImport("kernel32.dll")]
public static extern void GlobalMemoryStatus(out MemoryStatus stat);

up0down
link

You could use a WMI query, the property you need is TotalPhysicalMemory which is in the Win32_ComputerSystem class.

up0down
link

I think this has been answered already, but as per adam404 has indicated, you can do something like this (at least in c# 2005 beta)
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.VisualBasic.Devices;
namespace PhysicalMemory
{
class Program
{
static void Main(string[] args)
{
ComputerInfo myCompInfo = new ComputerInfo();
Console.WriteLine("Physical Memory {0}", myCompInfo.TotalPhysicalMemory );
}
}
}
(Note that you have to add a reference to Microsoft.VisualBasic to the project)
Boogie

This post was imported from csharpfriends, if you have a similiar question please ask it again.

All previous members have been migrated, hope you enjoy the new platform!

Feedback