web-dev-qa-db-fra.com

Comment obtenez-vous le montant total de RAM l'ordinateur a?

En utilisant C #, je souhaite obtenir la quantité totale de RAM de mon ordinateur . Avec le PerformanceCounter, je peux obtenir la quantité de RAM disponible en définissant:

counter.CategoryName = "Memory";
counter.Countername = "Available MBytes";

Mais je n'arrive pas à trouver un moyen d'obtenir la quantité totale de mémoire. Comment pourrais-je m'y prendre?

Mettre à jour:

MagicKat: J'ai vu cela lorsque je cherchais, mais cela ne fonctionne pas - "Il vous manque une assemblée ou une référence?". J'ai cherché à ajouter cela aux références, mais je ne le vois pas là.

67
Joel

La manière p/invokeEDIT: Changé en GlobalMemoryStatusEx pour donner des résultats précis (heh)

  [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
  private class MEMORYSTATUSEX
  {
     public uint dwLength;
     public uint dwMemoryLoad;
     public ulong ullTotalPhys;
     public ulong ullAvailPhys;
     public ulong ullTotalPageFile;
     public ulong ullAvailPageFile;
     public ulong ullTotalVirtual;
     public ulong ullAvailVirtual;
     public ulong ullAvailExtendedVirtual;
     public MEMORYSTATUSEX()
     {
        this.dwLength = (uint)Marshal.SizeOf(typeof(NativeMethods.MEMORYSTATUSEX));
     }
  }


  [return: MarshalAs(UnmanagedType.Bool)]
  [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  static extern bool GlobalMemoryStatusEx([In, Out] MEMORYSTATUSEX lpBuffer);

Ensuite, utilisez comme:

ulong installedMemory;
MEMORYSTATUSEX memStatus = new MEMORYSTATUSEX();
if( GlobalMemoryStatusEx( memStatus))
{ 
   installedMemory = memStatus.ullTotalPhys;
}

Ou vous pouvez utiliser WMI (géré mais plus lentement) pour interroger "TotalPhysicalMemory" dans la classe "Win32_ComputerSystem".

Éditer code fixe par commentaire de joel-llamaduck.blogspot.com

54
Philip Rieck

Ajoutez une référence à Microsoft.VisualBasic et un using Microsoft.VisualBasic.Devices;.

La classe ComputerInfo contient toutes les informations dont vous avez besoin. 

167
MagicKat

Ajoutez une référence à Microsoft.VisualBasic.dll, comme mentionné ci-dessus. Obtenir la mémoire physique totale est aussi simple que cela (oui, je l’ai testée):

static ulong GetTotalMemoryInBytes()
{
    return new Microsoft.VisualBasic.Devices.ComputerInfo().TotalPhysicalMemory;
}
57
Ryan Lundy

Si vous utilisez Mono, sachez que Mono 2.8 (qui paraîtra plus tard cette année) aura un compteur de performance indiquant la taille de la mémoire physique sur toutes les plates-formes sur lesquelles tourne Mono (y compris Windows). Vous voudriez récupérer la valeur du compteur en utilisant cet extrait de code:

using System;
using System.Diagnostics;

class app
{
   static void Main ()
   {
       var pc = new PerformanceCounter ("Mono Memory", "Total Physical Memory");
       Console.WriteLine ("Physical RAM (bytes): {0}", pc.RawValue);
   }
}

Si vous êtes intéressé par le code C qui fournit le compteur de performance, vous pouvez le trouver ici .

29
grendel

Toutes les réponses ici, y compris celle acceptée, vous donneront le montant total de RAM disponible à utiliser. Et c'est peut-être ce que l'OP voulait.

Mais si vous souhaitez obtenir la quantité de installé RAM, vous voudrez appeler la fonction GetPhysicallyInstalledSystemMemory .

À partir du lien, dans la section Remarques:

La fonction GetPhysicallyInstalledSystemMemory récupère la quantité de RAM installée physiquement à partir des tables de microprogramme SMBIOS de l'ordinateur. Cela peut différer de la quantité signalée par la fonction GlobalMemoryStatusEx, qui définit le membre ullTotalPhys de la structure MEMORYSTATUSEX sur la quantité de mémoire physique disponible pour le système d'exploitation. La quantité de mémoire disponible pour le système d'exploitation peut être inférieure à la quantité de mémoire physiquement installée sur l'ordinateur, car le BIOS et certains pilotes peuvent réserver de la mémoire en tant que régions d'E/S pour la mémoire mappée. périphériques, rendant la mémoire indisponible pour le système d'exploitation et les applications.

Exemple de code:

[DllImport("kernel32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetPhysicallyInstalledSystemMemory(out long TotalMemoryInKilobytes);

static void Main()
{
    long memKb;
    GetPhysicallyInstalledSystemMemory(out memKb);
    Console.WriteLine((memKb / 1024 / 1024) + " GB of RAM installed.");
}
19
sstan

Une autre façon de faire consiste à utiliser les fonctions de requête .NET System.Management:

string Query = "SELECT Capacity FROM Win32_PhysicalMemory";
ManagementObjectSearcher searcher = new ManagementObjectSearcher(Query);

UInt64 Capacity = 0;
foreach (ManagementObject WniPART in searcher.Get())
{
    Capacity += Convert.ToUInt64(WniPART.Properties["Capacity"].Value);
}

return Capacity;
10
zgerd

vous pouvez simplement utiliser ce code pour obtenir ces informations, ajoutez simplement la référence 

using Microsoft.VisualBasic.Devices;

et simplement utiliser le code suivant 

    private void button1_Click(object sender, EventArgs e)
    {
        getAvailableRAM();
    }

    public void getAvailableRAM()
    {
        ComputerInfo CI = new ComputerInfo();
        ulong mem = ulong.Parse(CI.TotalPhysicalMemory.ToString());
        richTextBox1.Text = (mem / (1024*1024) + " MB").ToString();
    }
6
Nilan Niyomal

Vous pouvez utiliser WMI. J'ai trouvé un extrait.

Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _ 
& strComputer & "\root\cimv2") 
Set colComputer = objWMIService.ExecQuery _
("Select * from Win32_ComputerSystem")

For Each objComputer in colComputer 
  strMemory = objComputer.TotalPhysicalMemory
Next
4
CodeRot
// use `/ 1048576` to get ram in MB
// and `/ (1048576 * 1024)` or `/ 1048576 / 1024` to get ram in GB
private static String getRAMsize()
{
    ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
    ManagementObjectCollection moc = mc.GetInstances();
    foreach (ManagementObject item in moc)
    {
       return Convert.ToString(Math.Round(Convert.ToDouble(item.Properties["TotalPhysicalMemory"].Value) / 1048576, 0)) + " MB";
    }

    return "RAMsize";
}
3
Mehul Sant

Cette fonction (ManagementQuery) fonctionne sous Windows XP et versions ultérieures:

private static string ManagementQuery(string query, string parameter, string scope = null) {
    string result = string.Empty;
    var searcher = string.IsNullOrEmpty(scope) ? new ManagementObjectSearcher(query) : new ManagementObjectSearcher(scope, query);
    foreach (var os in searcher.Get()) {
        try {
            result = os[parameter].ToString();
        }
        catch {
            //ignore
        }

        if (!string.IsNullOrEmpty(result)) {
            break;
        }
    }

    return result;
}

Usage:

Console.WriteLine(BytesToMb(Convert.ToInt64(ManagementQuery("SELECT TotalPhysicalMemory FROM Win32_ComputerSystem", "TotalPhysicalMemory", "root\\CIMV2"))));
1
Lance

.NIT a une limite à la quantité de mémoire à laquelle il peut accéder. Il y a un pourcentage, puis 2 Go en xp était le plafond dur.

Vous pourriez avoir 4 Go, et cela tuerait l'application quand elle atteindrait 2 Go.

De plus, en mode 64 bits, vous pouvez utiliser un pourcentage de la mémoire en dehors du système. Je ne sais donc pas si vous pouvez demander la totalité ou si cela est spécifiquement protégé.

0
DevelopingChris

Compatible avec .Net et Mono (testé avec Win10/FreeBSD/CentOS)

Utilisation du code source ComputerInfo et PerformanceCounters pour Mono et comme sauvegarde pour .Net:

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Security;

public class SystemMemoryInfo
{
    private readonly PerformanceCounter _monoAvailableMemoryCounter;
    private readonly PerformanceCounter _monoTotalMemoryCounter;
    private readonly PerformanceCounter _netAvailableMemoryCounter;

    private ulong _availablePhysicalMemory;
    private ulong _totalPhysicalMemory;

    public SystemMemoryInfo()
    {
        try
        {
            if (PerformanceCounterCategory.Exists("Mono Memory"))
            {
                _monoAvailableMemoryCounter = new PerformanceCounter("Mono Memory", "Available Physical Memory");
                _monoTotalMemoryCounter = new PerformanceCounter("Mono Memory", "Total Physical Memory");
            }
            else if (PerformanceCounterCategory.Exists("Memory"))
            {
                _netAvailableMemoryCounter = new PerformanceCounter("Memory", "Available Bytes");
            }
        }
        catch
        {
            // ignored
        }
    }

    public ulong AvailablePhysicalMemory
    {
        [SecurityCritical]
        get
        {
            Refresh();

            return _availablePhysicalMemory;
        }
    }

    public ulong TotalPhysicalMemory
    {
        [SecurityCritical]
        get
        {
            Refresh();

            return _totalPhysicalMemory;
        }
    }

    [SecurityCritical]
    [DllImport("Kernel32", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern void GlobalMemoryStatus(ref MEMORYSTATUS lpBuffer);

    [SecurityCritical]
    [DllImport("Kernel32", CharSet = CharSet.Auto, SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool GlobalMemoryStatusEx(ref MEMORYSTATUSEX lpBuffer);

    [SecurityCritical]
    private void Refresh()
    {
        try
        {
            if (_monoTotalMemoryCounter != null && _monoAvailableMemoryCounter != null)
            {
                _totalPhysicalMemory = (ulong) _monoTotalMemoryCounter.NextValue();
                _availablePhysicalMemory = (ulong) _monoAvailableMemoryCounter.NextValue();
            }
            else if (Environment.OSVersion.Version.Major < 5)
            {
                var memoryStatus = MEMORYSTATUS.Init();
                GlobalMemoryStatus(ref memoryStatus);

                if (memoryStatus.dwTotalPhys > 0)
                {
                    _availablePhysicalMemory = memoryStatus.dwAvailPhys;
                    _totalPhysicalMemory = memoryStatus.dwTotalPhys;
                }
                else if (_netAvailableMemoryCounter != null)
                {
                    _availablePhysicalMemory = (ulong) _netAvailableMemoryCounter.NextValue();
                }
            }
            else
            {
                var memoryStatusEx = MEMORYSTATUSEX.Init();

                if (GlobalMemoryStatusEx(ref memoryStatusEx))
                {
                    _availablePhysicalMemory = memoryStatusEx.ullAvailPhys;
                    _totalPhysicalMemory = memoryStatusEx.ullTotalPhys;
                }
                else if (_netAvailableMemoryCounter != null)
                {
                    _availablePhysicalMemory = (ulong) _netAvailableMemoryCounter.NextValue();
                }
            }
        }
        catch
        {
            // ignored
        }
    }

    private struct MEMORYSTATUS
    {
        private uint dwLength;
        internal uint dwMemoryLoad;
        internal uint dwTotalPhys;
        internal uint dwAvailPhys;
        internal uint dwTotalPageFile;
        internal uint dwAvailPageFile;
        internal uint dwTotalVirtual;
        internal uint dwAvailVirtual;

        public static MEMORYSTATUS Init()
        {
            return new MEMORYSTATUS
            {
                dwLength = checked((uint) Marshal.SizeOf(typeof(MEMORYSTATUS)))
            };
        }
    }

    private struct MEMORYSTATUSEX
    {
        private uint dwLength;
        internal uint dwMemoryLoad;
        internal ulong ullTotalPhys;
        internal ulong ullAvailPhys;
        internal ulong ullTotalPageFile;
        internal ulong ullAvailPageFile;
        internal ulong ullTotalVirtual;
        internal ulong ullAvailVirtual;
        internal ulong ullAvailExtendedVirtual;

        public static MEMORYSTATUSEX Init()
        {
            return new MEMORYSTATUSEX
            {
                dwLength = checked((uint) Marshal.SizeOf(typeof(MEMORYSTATUSEX)))
            };
        }
    }
}
0
Soroush Falahati

Personne n'a encore mentionné GetPerformanceInfo . Les signatures PInvoke sont disponibles.

Cette fonction rend disponibles les informations suivantes sur l’ensemble du système:

  • CommitTotal
  • CommitLimit
  • CommitPeak
  • Physique total
  • PhysiqueDisponible
  • SystemCache
  • KernelTotal
  • KernelPaged
  • KernelNonpaged
  • Taille de la page
  • HandleCount
  • ProcessCount
  • ThreadCount

PhysicalTotal correspond à ce que recherche le PO, bien que la valeur soit le nombre de pages. Pour convertir en octets, multipliez par la valeur PageSize renvoyée.

0
Roman Starkov