Added a RAM sensor for used memory.
3 This Source Code Form is subject to the terms of the Mozilla Public
4 License, v. 2.0. If a copy of the MPL was not distributed with this
5 file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 Copyright (C) 2012 Michael Möller <mmoeller@openhardwaremonitor.org>
11 using System.Runtime.InteropServices;
13 namespace OpenHardwareMonitor.Hardware.RAM {
14 internal class GenericRAM : Hardware {
16 private Sensor loadSensor;
17 private Sensor usedMemory;
18 private Sensor availableMemory;
20 public GenericRAM(string name, ISettings settings)
21 : base(name, new Identifier("ram"), settings)
23 loadSensor = new Sensor("Memory", 0, SensorType.Load, this, settings);
24 ActivateSensor(loadSensor);
26 usedMemory = new Sensor("Used Memory", 0, SensorType.Data, this,
28 ActivateSensor(usedMemory);
30 availableMemory = new Sensor("Available Memory", 1, SensorType.Data, this,
32 ActivateSensor(availableMemory);
35 public override HardwareType HardwareType {
37 return HardwareType.RAM;
41 public override void Update() {
42 NativeMethods.MemoryStatusEx status = new NativeMethods.MemoryStatusEx();
43 status.Length = checked((uint)Marshal.SizeOf(
44 typeof(NativeMethods.MemoryStatusEx)));
46 if (!NativeMethods.GlobalMemoryStatusEx(ref status))
49 loadSensor.Value = 100.0f -
50 (100.0f * status.AvailablePhysicalMemory) /
51 status.TotalPhysicalMemory;
53 usedMemory.Value = (float)(status.TotalPhysicalMemory
54 - status.AvailablePhysicalMemory) / (1024 * 1024 * 1024);
56 availableMemory.Value = (float)status.AvailablePhysicalMemory /
60 private class NativeMethods {
61 [StructLayout(LayoutKind.Sequential)]
62 public struct MemoryStatusEx {
64 public uint MemoryLoad;
65 public ulong TotalPhysicalMemory;
66 public ulong AvailablePhysicalMemory;
67 public ulong TotalPageFile;
68 public ulong AvailPageFile;
69 public ulong TotalVirtual;
70 public ulong AvailVirtual;
71 public ulong AvailExtendedVirtual;
74 [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
75 [return: MarshalAs(UnmanagedType.Bool)]
76 internal static extern bool GlobalMemoryStatusEx(
77 ref NativeMethods.MemoryStatusEx buffer);