tags:

views:

526

answers:

1

Hello,

I'm working on a simple ASP .NET health checker and I've run into a few obstacles.

1) I need to be able to get the full non-paged memory usage from a remote machine (on same network). I've tried using System.Diganostics.Process.NonpagedSystemMemorySize64 however I've come to realize that the kernel's nonpaged usage is going to be missing from that total. Here is a quick sample of what I was doing:

Process[] myprocess = Process.GetProcesses("computername");

foreach (Process p in myprocess)
{
nonpaged += p.NonpagedSystemMemorySize64;
}

2) I can overcome that locally by using System.Diagnostics.PerformanceCounter however you can only access the API for that class locally. Is there another class that would suit my needs?

Any help would be appreciated.

A: 

One solution I used for this to grab machine diagnostics previously was to use DLLImport.

See P-Invoke

Hope this helps

Pete

In answer to your comment

When using DLL import you have to declare the function wrapper yourself. In the code below you can see the public static extern void which is saying to the compiler that it is an external call to a function called GlobalMemoryStatus which is located in the DLLImported kernel32.dll. The MemoryStatus struct which is an output parameter of the functionis populated inside the kernel32 dll and is returned back fully populated.

Copy this into your code and read the comments they should help you to understand it.

/// <summary>
        /// Populates a memory status struct with the machines current memory status.
        /// </summary>
        /// <param name="stat">The status struct to be populated.</param>
        [DllImport("kernel32.dll")]
        public static extern void GlobalMemoryStatus(out MemoryStatus stat);


        /// <summary>
        /// The memory status struct is populated by the GlobalMemoryStatus external dll call to Kernal32.dll.
        /// </summary>
        public struct MemoryStatus
        {
            public uint Length;
            public uint MemoryLoad;
            public uint TotalPhysical;
            public uint AvailablePhysical;
            public uint TotalPageFile;
            public uint AvailablePageFile;
            public uint TotalVirtual;
            public uint AvailableVirtual;
        } 

// copy the guts of this method and add it to your own method.
public void InspectMemoryStatus()
{

MemoryStatus status = new MemoryStatus();
            GlobalMemoryStatus(out status);

    Debug.WriteLine(status.TotalVirtual);
}

This should allow you to get the memory diagnostics of the machine.

Peter
Thanks Pete,However there's no property of GlobalMemoryStatus or GlobalMemoryStatusex for non-paged usage or the total kernel and/or kernel paged usage unless I'm missing something.
jw0rd
As you can see above the MemoryStatus struct has all of the info on the memory and page file and virtual memory usage etc.
Peter