How to get Windows physical RAM using python?

Discussion in 'Python' started by Mark, Jul 30, 2003.

  1. Mark

    Mark Guest

    Thanks.
     
    Mark, Jul 30, 2003
    #1
    1. Advertising

  2. Mark

    Mark Guest

    OK, How to check the amount of Windows physical RAM using python?

    Mark wrote:
    > Thanks.
    >
     
    Mark, Jul 30, 2003
    #2
    1. Advertising

  3. 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
     
    =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=, Jul 30, 2003
    #3
  4. Mark

    Dan Bishop Guest

    Mark <> wrote in message news:<bg92o2$onr$>...
    > OK, How to check the amount of Windows physical RAM using python?


    The easiest way is to parse the output from $WINDIR/system32/mem.exe .

    memTotals = os.popen('mem | find "total"').readlines()
    conventionalMemory = int(memTotals[0].split()[0])
    extendedMemory = int(memTotals[1].split()[0])
     
    Dan Bishop, Jul 31, 2003
    #4
  5. Mark

    Gisle Vanem Guest

    "Dan Bishop" <> wrote:

    > The easiest way is to parse the output from $WINDIR/system32/mem.exe .
    >
    > memTotals = os.popen('mem | find "total"').readlines()
    > conventionalMemory = int(memTotals[0].split()[0])
    > extendedMemory = int(memTotals[1].split()[0])


    Duh! That program reports the memory available to 16-bit
    programs.

    --gv
     
    Gisle Vanem, Jul 31, 2003
    #5
  6. "Martin v. Löwis" <> writes:

    > 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.


    Here's a ctypes wrapper for the GlobalMemoryStatus function. If you have
    more than 2GB of ram, you should use GlobalMemoryStatusEx instead:

    Python 2.3 (#46, Jul 29 2003, 18:54:32) [MSC v.1200 32 bit (Intel)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> from ctypes import *
    >>> from ctypes.wintypes import DWORD
    >>>
    >>> SIZE_T = c_ulong
    >>>
    >>> class _MEMORYSTATUS(Structure):

    .... _fields_ = [("dwLength", DWORD),
    .... ("dwMemoryLength", DWORD),
    .... ("dwTotalPhys", SIZE_T),
    .... ("dwAvailPhys", SIZE_T),
    .... ("dwTotalPageFile", SIZE_T),
    .... ("dwAvailPageFile", SIZE_T),
    .... ("dwTotalVirtual", SIZE_T),
    .... ("dwAvailVirtualPhys", SIZE_T)]
    .... def show(self):
    .... for field_name, field_type in self._fields_:
    .... print field_name, getattr(self, field_name)
    ....
    >>> memstatus = _MEMORYSTATUS()
    >>> windll.kernel32.GlobalMemoryStatus(byref(memstatus))

    2147483647
    >>> memstatus.show()

    dwLength 32
    dwMemoryLength 47
    dwTotalPhys 535609344
    dwAvailPhys 281993216
    dwTotalPageFile 907055104
    dwAvailPageFile 720285696
    dwTotalVirtual 2147352576
    dwAvailVirtualPhys 2117312512
    >>>


    Thomas
     
    Thomas Heller, Jul 31, 2003
    #6
  7. Mark wrote:
    > OK, How to check the amount of Windows physical RAM using python?


    Martin v. Lowis wrote:
    > 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.


    So let's write one in Python in a minute!

    ---- winmem.py
    from ctypes import *
    from ctypes.wintypes import *

    class MEMORYSTATUS(Structure):
    _fields_ = [
    ('dwLength', DWORD),
    ('dwMemoryLoad', DWORD),
    ('dwTotalPhys', DWORD),
    ('dwAvailPhys', DWORD),
    ('dwTotalPageFile', DWORD),
    ('dwAvailPageFile', DWORD),
    ('dwTotalVirtual', DWORD),
    ('dwAvailVirtual', DWORD),
    ]

    def winmem():
    x = MEMORYSTATUS()
    windll.kernel32.GlobalMemoryStatus(byref(x))
    return x

    ---- in your code
    >>> from winmem import winmem
    >>> m = winmem()
    >>> print '%d MB physical RAM left.' % (m.dwAvailPhys/1024**2)

    90 MB physical RAM left.
    >>>



    Hail to ctypes!

    If you have never heard of ctypes, visit
    http://starship.python.net/crew/theller/ctypes/ and try it. You
    will love it.

    Seo Sanghyeon
     
    Seo Sanghyeon, Jul 31, 2003
    #7
  8. Mark

    Tim Golden Guest

    (Branimir Petrovic) wrote in message news:<>...
    > "Martin v. Löwis" <> wrote in message news:<bg920a$9pt$05$-online.com>...
    > > Python can't get you Windows physical RAM. You have to order RAM
    > > from a manufacturer or reseller if Windows needs more RAM :)
    > >
    > > Regards,
    > > Martin

    >
    > "Easy" way to get to physical RAM on Windows is via WMI objects.
    >


    And here's how you'd do it in Python:

    1) Get WMI module from http://tgolden.sc.sabren.com/wmi.html

    2) Do something like this:

    <code>
    import wmi

    computer = wmi.WMI ()
    for i in computer.Win32_ComputerSystem ():
    print i.Caption, "has", i.TotalPhysicalMemory, "bytes of memory"
    </code>

    Obviously you can fiddle around with Megabytes and Gigabytes and so on
    if you need to. The loop is a slight hack: you obviously only have one
    computer system, but this interface to WMI always returns a list
    (albeit of one value).

    HTH
    TJG
     
    Tim Golden, Jul 31, 2003
    #8
  9. On Wed, 30 Jul 2003 23:04:56 +0200, =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?= <> 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
    **
    */

    #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
     
    Bengt Richter, Jul 31, 2003
    #9
  10. On Fri, 01 Aug 2003 09:02:03 +1000, Mark Hammond <> wrote:

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

    You mean just posting to c.l.py doesn't go anywhere? ;-)

    Anyway, don't know how to submit path to win32all. Plus how do you test something like that?

    Already the 0 for percentage of memory in use that I got seemed funny, but maybe
    it's a percentage of total possible maxed-out page file virtual.

    Seems like it needs a little ageing at least? I thought someone might spot something.

    I meant to look into win32all, but this is all the further I got ;-/

    03-05-31 16:24 3,971,152 E:\DownLoad\Python\win32all-152.exe

    Regards,
    Bengt Richter
     
    Bengt Richter, Aug 1, 2003
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Robert Posey
    Replies:
    0
    Views:
    696
    Robert Posey
    Nov 26, 2003
  2. ashu
    Replies:
    1
    Views:
    483
  3. ashu
    Replies:
    2
    Views:
    641
    mysticlol
    Nov 6, 2006
  4. Xin Xiao

    Block RAM Distributed RAM

    Xin Xiao, Jan 7, 2008, in forum: VHDL
    Replies:
    8
    Views:
    1,511
    Duane Clark
    Jan 7, 2008
  5. Ognjen Bezanov
    Replies:
    1
    Views:
    624
    Carl Banks
    Nov 10, 2009
Loading...

Share This Page