How to get Windows physical RAM using python?

Mark Hammond mhammond at skippinet.com.au
Fri Aug 1 01:02:03 CEST 2003


Why not submit that as a patch to win32all <wink>?

Mark.

Bengt Richter wrote:
> On Wed, 30 Jul 2003 23:04:56 +0200, =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?= <martin at v.loewis.de> wrote:
> 
> 
>>Mark wrote:
>>
>>
>>>OK, How to check the amount of Windows physical RAM using python?
>>
>>You should call the GlobalMemoryStatus(Ex) function. To my knowledge,
>>there is no Python wrapper for it, yet, so you would need to write one.
>>
>>Regards,
>>Martin
>>
> 
> ====< memorystatus.c >=============================================
> /*
> ** memorystatus.c
> ** Version 0.01 20030731 10:45:12 Bengt Richter bokr at oz.net
> **
> */
> 
> #include "Python.h"
> #include <windows.h>
> 
> static char doc_memstat[] =
>     "Returns list of 7 integers:\n"
>     "   [0]: percent of memory in use\n"
>     "   [1]: bytes of physical memory\n"
>     "   [2]: free physical memory bytes\n"
>     "   [3]: bytes of paging file\n"
>     "   [4]: free bytes of paging file\n"
>     "   [5]: user bytes of address space\n"
>     "   [6]: free user bytes\n";
> 
> static PyObject *
> memorystatus_memstat(PyObject *self, PyObject *args)
> {
>     PyObject *rv;
>     MEMORYSTATUS ms;
>     GlobalMemoryStatus( &ms );
> 
>     if (!PyArg_ParseTuple(args, "")) /* No arguments */
>         return NULL;
>     rv = Py_BuildValue("[i,i,i,i,i,i,i]",
>         ms.dwMemoryLoad,    // percent of memory in use 
>         ms.dwTotalPhys,     // bytes of physical memory 
>         ms.dwAvailPhys,     // free physical memory bytes 
>         ms.dwTotalPageFile, // bytes of paging file 
>         ms.dwAvailPageFile, // free bytes of paging file 
>         ms.dwTotalVirtual,  // user bytes of address space 
>         ms.dwAvailVirtual   // free user bytes 
>     ); 
>     return rv;
> }
> 
> /* List of functions defined in the module */
> static struct PyMethodDef memorystatus_module_methods[] = {
>     {"memstat", memorystatus_memstat, METH_VARARGS, doc_memstat},
>     {NULL, NULL}                 /* sentinel */
> };
> 
> 
> /* Initialization function for the module (*must* be called initmemorystatus) */
> static char doc_memorystatus[] = "Get win32 memory status numbers (see memstat method)";
> 
> DL_EXPORT(void)
> initmemorystatus(void)
> {
>     PyObject *m, *d, *x;
> 
>     /* Create the module and add the functions */
>     m = Py_InitModule("memorystatus", memorystatus_module_methods);
>     d = PyModule_GetDict(m);
>     x = PyString_FromString(doc_memorystatus);
>     PyDict_SetItemString(d, "__doc__", x);
>     Py_XDECREF(x);
> }
> ===================================================================
> 
> You may find a little .cmd file like this (tailored to your system) handy:
> 
> [10:55] C:\pywk\ut\memorystatus>type \util\mkpydll.cmd
> @cl -LD -nologo -Id:\python22\include %1.c -link -LIBPATH:D:\python22\libs -export:init%1
> 
> [10:56] C:\pywk\ut\memorystatus>mkpydll memorystatus
> memorystatus.c
>    Creating library memorystatus.lib and object memorystatus.exp
> 
> [10:56] C:\pywk\ut\memorystatus>python
> 
> (I'll indent this one space to avoid spurious quote highlights)
> 
>  Python 2.2.2 (#37, Oct 14 2002, 17:02:34) [MSC 32 bit (Intel)] on win32
>  Type "help", "copyright", "credits" or "license" for more information.
>  >>> import memorystatus
>  >>> help(memorystatus)
>  Help on module memorystatus:
> 
>  NAME
>      memorystatus - Get win32 memory status numbers (see memstat method)
> 
>  FILE
>      c:\pywk\ut\memorystatus\memorystatus.dll
> 
>  FUNCTIONS
>      memstat(...)
>          Returns list of 7 integers:
>          [0]: percent of memory in use
>          [1]: bytes of physical memory
>          [2]: free physical memory bytes
>          [3]: bytes of paging file
>          [4]: free bytes of paging file
>          [5]: user bytes of address space
>          [6]: free user bytes
> 
>  DATA
>      __file__ = 'memorystatus.dll'
>      __name__ = 'memorystatus'
> 
> 
>  >>> memorystatus.memstat()
>  [0, 334929920, 271536128, 942825472, 861339648, 2147352576, 2129780736]
> 
> Warning: Just now did this. Not tested beyond what you see!!
> 
> Regards,
> Bengt Richter





More information about the Python-list mailing list