Entwickler-Ecke

Open Source Units - MemoryInfo - Abfragen von Informationen über den Speicher


Chryzler - Di 23.10.07 15:26
Titel: MemoryInfo - Abfragen von Informationen über den Speicher
MemoryInfo - Abfragen von Informationen über den Speicher

MemoryInfo ist eine einfach zu benutzende Klasse, mit der es möglich ist, folgende Eigenschaften des Arbeitsspeichers auszulesen (alle Größenangaben in Bytes):
Macht also nix anderes, als GlobalMemoryStatus [http://msdn2.microsoft.com/en-us/library/aa366586.aspx], nur als .NET-Klasse schön verpackt. Ist also nicht wirklich eine große Leistung, ich konnte nur keine handliche Klasse für .NET finden, die genau das macht was ich will. :)

Zur Benutzung braucht man eigentlich nicht viel sagen, Instanz erstellen und die Update-Methode aufrufen, und dann kann man auch schon die Werte die man braucht auslesen. Wenn man gleich nach dem Konstruktor-Aufruf updaten möchte, reicht auch ein true als Konstruktor-Parameter:


C#-Quelltext
1:
2:
3:
4:
5:
MemoryInfo memoryInfo = new MemoryInfo(true);

Console.WriteLine("{0} Prozent des Speichers belegt.", memoryInfo.MemoryLoad);
Console.WriteLine("{0} MB physischer Speicher frei.", (memoryInfo.availPhys / 1024) / 1024);
Console.WriteLine("{0} MB virtueller Speicher frei.", (memoryInfo.availVirtual / 1024) / 1024);


Und jetzt noch die Klasse:


C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
using System.Runtime.InteropServices;

namespace System
{
    public class MemoryInfo
    {
        [StructLayout(LayoutKind.Sequential)]
        internal class MEMORYSTATUS
        {
            internal int length;
            internal int memoryLoad;
            internal uint totalPhys;
            internal uint availPhys;
            internal uint totalPageFile;
            internal uint availPageFile;
            internal uint totalVirtual;
            internal uint availVirtual;
        }

        [DllImport("kernel32.dll", SetLastError = true)]
        internal static extern bool GlobalMemoryStatus(MEMORYSTATUS buffer);

        MEMORYSTATUS memorystatus = new MEMORYSTATUS();

        public MemoryInfo()
        {
        }

        public MemoryInfo(bool Update)
        {
            if (Update)
                this.Update();
        }

        public bool Update()
        {
            return GlobalMemoryStatus(memorystatus);
        }

        public int MemoryLoad
        {
            get
            {
                return memorystatus.memoryLoad;
            }
        }

        public uint TotalPhys
        {
            get
            {
                return memorystatus.totalPhys;
            }
        }

        public uint AvailPhys
        {
            get
            {
                return memorystatus.availPhys;
            }
        }

        public uint TotalPageFile
        {
            get
            {
                return memorystatus.totalPageFile;
            }
        }

        public uint AvailPageFile
        {
            get
            {
                return memorystatus.availPageFile;
            }
        }

        public uint TotalVirtual
        {
            get
            {
                return memorystatus.totalVirtual;
            }
        }

        public uint AvailVirtual
        {
            get
            {
                return memorystatus.availVirtual;
            }
        }
    }
}