MathGroup Archive 2010

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Query available max memory (RAM)?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg114968] Re: Query available max memory (RAM)?
  • From: Alexey <lehin.p at gmail.com>
  • Date: Fri, 24 Dec 2010 04:13:46 -0500 (EST)
  • References: <ieklmr$4t4$1@smc.vnet.net>

On 19 =D0=B4=D0=B5=D0=BA, 13:10, Istv=C3=A1n Zachar <z... at freemail.hu> wrot=
e:
> Dear Group,
>
> is there a way to get data on the actual available max RAM in a
> Windows system? With SystemInformation and Environment I was unable to
> obtain this information.
>
> Istvan

You can use NETLink for this (it requires .NET version 2 or later to
be installed). You can get all memory-related information directly
from kernel32.dll (on 32-bit Windows systems). This is the fastest and
the most effective way.

For example, the following defines a function 'freePhysicalMemory'
that returns the current amount of free physical memory in bytes:

Needs["NETLink`"]

Module[{before(*,globalMemoryStatusEx,memorystatusex*)},
  before = LoadedNETTypes[];
  globalMemoryStatusEx = DefineDLLFunction[
    "[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
    public 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 ));}}
    [return: MarshalAs(UnmanagedType.Bool)]
    [DllImport(\"kernel32.dll\", CharSet=CharSet.Auto, \
SetLastError=true)]
    public static extern bool GlobalMemoryStatusEx([In, Out] \
MEMORYSTATUSEX lpBuffer);"
    ];
  memorystatusex = Complement[LoadedNETTypes[], before][[1, 1]];
  memorystatusex = memorystatusex <> "+MEMORYSTATUSEX";
  memorystatusex = NETNew[memorystatusex];
  Block[{ullAvailPhys},
   freePhysicalMemory :=
    If[globalMemoryStatusEx[memorystatusex],
     memorystatusex@ullAvailPhys, $Failed]]];

You can easily get all other memory-related information:

list = {{dwLength,
    "The size of the structure, in bytes. You must set this member \
before calling GlobalMemoryStatusEx."}, {dwMemoryLoad,
    "A number between 0 and 100 that specifies the approximate \
percentage of physical memory that is in use (0 indicates no memory \
use and 100 indicates full memory use)."}, {ullTotalPhys,
    "The amount of actual physical memory, in bytes."}, {ullAvailPhys,
     "The amount of physical memory currently available, in bytes. \
This is the amount of physical memory that can be immediately reused \
without having to write its contents to disk first. It is the sum of \
the size of the standby, free, and zero lists."}, {ullTotalPageFile,
    "The current committed memory limit for the system or the current
\
process, whichever is smaller, in bytes. To get the system-wide \
committed memory limit, call GetPerformanceInfo."}, {ullAvailPageFile,
     "The maximum amount of memory the current process can commit, in
\
bytes. This value is equal to or smaller than the system-wide \
available commit value. To calculate the system-wide available commit
\
value, call GetPerformanceInfo and subtract the value of CommitTotal \
from the value of CommitLimit."}, {ullTotalVirtual,
    "The size of the user-mode portion of the virtual address space \
of the calling process, in bytes. This value depends on the type of \
process, the type of processor, and the configuration of the \
operating system. For example, this value is approximately 2 GB for \
most 32-bit processes on an x86 processor and approximately 3 GB for \
32-bit processes that are large address aware running on a system \
with 4-gigabyte tuning enabled."}, {ullAvailVirtual,
    "The amount of unreserved and uncommitted memory currently in the
\
user-mode portion of the virtual address space of the calling \
process, in bytes."}, {ullAvailExtendedVirtual,
    "Reserved. This value is always 0."}};
globalMemoryStatusEx[memorystatusex];
{Tooltip[#[[1]], Style[#[[2]], 22]] & /@ list,
   memorystatusex /@ list[[All, 1]]} // Transpose // TableForm


  • Prev by Date: Re: Generic Button/Palette design pattern?
  • Next by Date: Re: Generic Button/Palette design pattern?
  • Previous by thread: Query available max memory (RAM)?
  • Next by thread: Re: Query available max memory (RAM)?