Type a page name and press Enter. You'll jump to the page if it exists, or you can create it if it doesn't.
To create a page in a module other than kernel32, prefix the name with the module name and a period.
GlobalMemoryStatus (kernel32)
.
C# Signature:
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern bool GlobalMemoryStatusEx( [In,Out] MEMORYSTATUSEX lpBuffer); //Used to use ref with comment below
// but ref doesn't work.(Use of [In, Out] instead of ref
//causes access violation exception on windows xp
//comment: most probably caused by MEMORYSTATUSEX being declared as a class
//(at least at pinvoke.net). On Win7, ref and struct work.
// Alternate Version Using "ref," And Works With Alternate Code Below.
// Also See Alternate Version Of [MEMORYSTATUSEX] Defined As A Structure.
[return: MarshalAs( UnmanagedType.Bool )]
[DllImport( "kernel32.dll", CharSet = CharSet.Auto, EntryPoint = "GlobalMemoryStatusEx", SetLastError = true )]
static extern bool _GlobalMemoryStatusEx( ref MEMORYSTATUSEX lpBuffer );
VB.Net Signature:
Private Declare Function GlobalMemoryStatusEx Lib "kernel32" Alias "GlobalMemoryStatusEx" ( _
<[In](), Out()>ByVal lpBuffer As MEMORYSTATUSEX _
) As <MarshalAs(UnmanagedType.Bool)> Boolean
The CLR offers us no way to tell if memory is getting tight. Many think that this API provides the best solution. This is mentioned by Jeffrey Richter in his book 'CLR via C#' ISBN: 0-7356-2163-2. It is useful in determining if your system is under excessive memory load by looking at the dwMemoryLoad member of the MEMORYSTATUSEX structure. If this value is > 80 (per Mr. Richter in his discussion of Garbage Collection), it is an indication that you might want to consider converting some strong references into weak references. Remember that a weakreference type will be collected when Generation 0 is full, so it is not a good technique for caching (as many seem to think).
Tips & Tricks:
If you encapsulate this in a lower level assembly and wrap this call in a higher level assembly then it will be much easier to change your system when/if Microsoft decides to provide this via a Managed call (or callback). You could also get total memory (and perform a full GC) by calling (as shown in this example):
long tm = GC.GetTotalMemory(true);
or simply:
GC.Collect(GC.MaxGeneration);
Sample Code:
private void DisplayMemory()
{
// Consumer of the NativeMethods class shown below
public bool isMemoryTight()
{
if (_MemoryLoad > MEMORY_TIGHT_CONST )
return true;
else
return false;
}
public uint MemoryLoad
public uint MemoryLoad
{
get { return _MemoryLoad; }
internal set { _MemoryLoad = value; }
}
public NativeMethods() {
msex = new MEMORYSTATUSEX();
if (GlobalMemoryStatusEx(msex)) {
_MemoryLoad = msex.dwMemoryLoad;
//etc.. Repeat for other structure members
public NativeMethods() {
msex = new MEMORYSTATUSEX();
if (GlobalMemoryStatusEx(msex)) {
}
else
// Use a more appropriate Exception Type. 'Exception' should almost never be thrown
throw new Exception("Unable to initalize the GlobalMemoryStatusEx API");
}
_MemoryLoad = msex.dwMemoryLoad;
//etc.. Repeat for other structure members
[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( MEMORYSTATUSEX ));
}
else
// Use a more appropriate Exception Type. 'Exception' should almost never be thrown
throw new Exception("Unable to initalize the GlobalMemoryStatusEx API");
}
}
[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( MEMORYSTATUSEX ));
}
}
// Alternate Wrapper Method
/// <summary>Retrieves information about the system's current usage of both physical and virtual memory.</summary>
/// <param name="msex">Memory information structure that will be populated by this method</param>
/// <returns>True if the wrapped native call was successfull, false if not. Use
/// <see cref="Marshal.GetLastWin32Error"/> for additional error information</returns>
public static bool GlobalMemoryStatus( ref MEMORYSTATUSEX msex )
{
msex.dwLength = (uint)Marshal.SizeOf( typeof( MEMORYSTATUSEX ) );
return( _GlobalMemoryStatusEx( ref msex ) );
}
// Alternate Wrapper Method
/// <summary>Retrieves information about the system's current usage of both physical and virtual memory.</summary>
/// <param name="msex">Memory information structure that will be populated by this method</param>
/// <returns>True if the wrapped native call was successfull, false if not. Use
/// <see cref="Marshal.GetLastWin32Error"/> for additional error information</returns>
public static bool GlobalMemoryStatus( ref MEMORYSTATUSEX msex )
{
msex.dwLength = (uint)Marshal.SizeOf( typeof( MEMORYSTATUSEX ) );
return( _GlobalMemoryStatusEx( ref msex ) );
}
Alternative Managed API:
var info = new Microsoft.VisualBasic.Devices.ComputerInfo();
Debug.WriteLine(info.TotalPhysicalMemory);
Debug.WriteLine(info.AvailablePhysicalMemory);
Debug.WriteLine(info.TotalVirtualMemory);
Debug.WriteLine(info.AvailableVirtualMemory);
Alternative Managed API:
var info = new Microsoft.VisualBasic.Devices.ComputerInfo();
Debug.WriteLine(info.TotalPhysicalMemory);
Debug.WriteLine(info.AvailablePhysicalMemory);
Debug.WriteLine(info.TotalVirtualMemory);
Debug.WriteLine(info.AvailableVirtualMemory);
The GlobalMemoryStatusEx API
5/26/2016 9:13:28 AM - -93.84.28.162
MEMORYSTATUSEX is used by GlobalMemoryStatusEx
7/31/2010 12:12:25 AM - Damon Carr-72.43.165.29
Please edit this page!
Do you have...
helpful tips or sample code to share for using this API in managed code?
corrections to the existing content?
variations of the signature you want to share?
additional languages you want to include?
Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).