How to get Windows physical RAM using python?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mark

    How to get Windows physical RAM using python?

    Thanks.

  • Mark

    #2
    Re: How to get Windows physical RAM using python?

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

    Mark wrote:[color=blue]
    > Thanks.
    >[/color]

    Comment

    • Martin v. Löwis

      #3
      Re: How to get Windows physical RAM using python?

      Mark wrote:
      [color=blue]
      > OK, How to check the amount of Windows physical RAM using python?[/color]

      You should call the GlobalMemorySta tus(Ex) function. To my knowledge,
      there is no Python wrapper for it, yet, so you would need to write one.

      Regards,
      Martin

      Comment

      • Dan Bishop

        #4
        Re: How to get Windows physical RAM using python?

        Mark <nbdy9@hotmail. com.nospam> wrote in message news:<bg92o2$on r$1@news3.bu.ed u>...[color=blue]
        > OK, How to check the amount of Windows physical RAM using python?[/color]

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

        memTotals = os.popen('mem | find "total"').readl ines()
        conventionalMem ory = int(memTotals[0].split()[0])
        extendedMemory = int(memTotals[1].split()[0])

        Comment

        • Gisle Vanem

          #5
          Re: How to get Windows physical RAM using python?

          "Dan Bishop" <danb_83@yahoo. com> wrote:
          [color=blue]
          > The easiest way is to parse the output from $WINDIR/system32/mem.exe .
          >
          > memTotals = os.popen('mem | find "total"').readl ines()
          > conventionalMem ory = int(memTotals[0].split()[0])
          > extendedMemory = int(memTotals[1].split()[0])[/color]

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

          --gv


          Comment

          • Thomas Heller

            #6
            Re: How to get Windows physical RAM using python?

            "Martin v. Löwis" <martin@v.loewi s.de> writes:
            [color=blue]
            > Mark wrote:
            >[color=green]
            >> OK, How to check the amount of Windows physical RAM using python?[/color]
            >
            > You should call the GlobalMemorySta tus(Ex) function. To my knowledge,
            > there is no Python wrapper for it, yet, so you would need to write one.[/color]

            Here's a ctypes wrapper for the GlobalMemorySta tus function. If you have
            more than 2GB of ram, you should use GlobalMemorySta tusEx 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.[color=blue][color=green][color=darkred]
            >>> from ctypes import *
            >>> from ctypes.wintypes import DWORD
            >>>
            >>> SIZE_T = c_ulong
            >>>
            >>> class _MEMORYSTATUS(S tructure):[/color][/color][/color]
            .... _fields_ = [("dwLength", DWORD),
            .... ("dwMemoryLengt h", DWORD),
            .... ("dwTotalPhy s", SIZE_T),
            .... ("dwAvailPhy s", SIZE_T),
            .... ("dwTotalPageFi le", SIZE_T),
            .... ("dwAvailPageFi le", SIZE_T),
            .... ("dwTotalVirtua l", SIZE_T),
            .... ("dwAvailVirtua lPhys", SIZE_T)]
            .... def show(self):
            .... for field_name, field_type in self._fields_:
            .... print field_name, getattr(self, field_name)
            ....[color=blue][color=green][color=darkred]
            >>> memstatus = _MEMORYSTATUS()
            >>> windll.kernel32 .GlobalMemorySt atus(byref(mems tatus))[/color][/color][/color]
            2147483647[color=blue][color=green][color=darkred]
            >>> memstatus.show( )[/color][/color][/color]
            dwLength 32
            dwMemoryLength 47
            dwTotalPhys 535609344
            dwAvailPhys 281993216
            dwTotalPageFile 907055104
            dwAvailPageFile 720285696
            dwTotalVirtual 2147352576
            dwAvailVirtualP hys 2117312512[color=blue][color=green][color=darkred]
            >>>[/color][/color][/color]

            Thomas

            Comment

            • Seo Sanghyeon

              #7
              Re: How to get Windows physical RAM using python?

              Mark wrote:[color=blue]
              > OK, How to check the amount of Windows physical RAM using python?[/color]

              Martin v. Lowis wrote:[color=blue]
              > You should call the GlobalMemorySta tus(Ex) function. To my knowledge,
              > there is no Python wrapper for it, yet, so you would need to write one.[/color]

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

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

              class MEMORYSTATUS(St ructure):
              _fields_ = [
              ('dwLength', DWORD),
              ('dwMemoryLoad' , DWORD),
              ('dwTotalPhys', DWORD),
              ('dwAvailPhys', DWORD),
              ('dwTotalPageFi le', DWORD),
              ('dwAvailPageFi le', DWORD),
              ('dwTotalVirtua l', DWORD),
              ('dwAvailVirtua l', DWORD),
              ]

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

              ---- in your code[color=blue][color=green][color=darkred]
              >>> from winmem import winmem
              >>> m = winmem()
              >>> print '%d MB physical RAM left.' % (m.dwAvailPhys/1024**2)[/color][/color][/color]
              90 MB physical RAM left.[color=blue][color=green][color=darkred]
              >>>[/color][/color][/color]


              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

              Comment

              • Tim Golden

                #8
                Re: How to get Windows physical RAM using python?

                BranimirPetrovi c@yahoo.com (Branimir Petrovic) wrote in message news:<b11d1191. 0307301601.6745 0ca3@posting.go ogle.com>...[color=blue]
                > "Martin v. Löwis" <martin@v.loewi s.de> wrote in message news:<bg920a$9p t$05$1@news.t-online.com>...[color=green]
                > > 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[/color]
                >
                > "Easy" way to get to physical RAM on Windows is via WMI objects.
                >[/color]

                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.TotalPhysical Memory, "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

                Comment

                • Bengt Richter

                  #9
                  Re: How to get Windows physical RAM using python?

                  On Wed, 30 Jul 2003 23:04:56 +0200, =?ISO-8859-1?Q?=22Martin_v =2E_L=F6wis=22? = <martin@v.loewi s.de> wrote:
                  [color=blue]
                  >Mark wrote:
                  >[color=green]
                  >> OK, How to check the amount of Windows physical RAM using python?[/color]
                  >
                  >You should call the GlobalMemorySta tus(Ex) function. To my knowledge,
                  >there is no Python wrapper for it, yet, so you would need to write one.
                  >
                  >Regards,
                  >Martin
                  >[/color]
                  ====< memorystatus.c >============== =============== =============== =
                  /*
                  ** memorystatus.c
                  ** Version 0.01 20030731 10:45:12 Bengt Richter bokr@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_me mstat(PyObject *self, PyObject *args)
                  {
                  PyObject *rv;
                  MEMORYSTATUS ms;
                  GlobalMemorySta tus( &ms );

                  if (!PyArg_ParseTu ple(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.dwTotalPageF ile, // bytes of paging file
                  ms.dwAvailPageF ile, // free bytes of paging file
                  ms.dwTotalVirtu al, // user bytes of address space
                  ms.dwAvailVirtu al // free user bytes
                  );
                  return rv;
                  }

                  /* List of functions defined in the module */
                  static struct PyMethodDef memorystatus_mo dule_methods[] = {
                  {"memstat", memorystatus_me mstat, METH_VARARGS, doc_memstat},
                  {NULL, NULL} /* sentinel */
                  };


                  /* Initialization function for the module (*must* be called initmemorystatu s) */
                  static char doc_memorystatu s[] = "Get win32 memory status numbers (see memstat method)";

                  DL_EXPORT(void)
                  initmemorystatu s(void)
                  {
                  PyObject *m, *d, *x;

                  /* Create the module and add the functions */
                  m = Py_InitModule(" memorystatus", memorystatus_mo dule_methods);
                  d = PyModule_GetDic t(m);
                  x = PyString_FromSt ring(doc_memory status);
                  PyDict_SetItemS tring(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\memo rystatus>type \util\mkpydll.c md
                  @cl -LD -nologo -Id:\python22\in clude %1.c -link -LIBPATH:D:\pyth on22\libs -export:init%1

                  [10:56] C:\pywk\ut\memo rystatus>mkpydl l memorystatus
                  memorystatus.c
                  Creating library memorystatus.li b and object memorystatus.ex p

                  [10:56] C:\pywk\ut\memo rystatus>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.[color=blue][color=green][color=darkred]
                  >>> import memorystatus
                  >>> help(memorystat us)[/color][/color][/color]
                  Help on module memorystatus:

                  NAME
                  memorystatus - Get win32 memory status numbers (see memstat method)

                  FILE
                  c:\pywk\ut\memo rystatus\memory status.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.d ll'
                  __name__ = 'memorystatus'

                  [color=blue][color=green][color=darkred]
                  >>> memorystatus.me mstat()[/color][/color][/color]
                  [0, 334929920, 271536128, 942825472, 861339648, 2147352576, 2129780736]

                  Warning: Just now did this. Not tested beyond what you see!!

                  Regards,
                  Bengt Richter

                  Comment

                  • Bengt Richter

                    #10
                    Re: How to get Windows physical RAM using python?

                    On Fri, 01 Aug 2003 09:02:03 +1000, Mark Hammond <mhammond@skipp inet.com.au> wrote:
                    [color=blue]
                    >Why not submit that as a patch to win32all <wink>?
                    >
                    >Mark.
                    >[/color]
                    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\Pyt hon\win32all-152.exe

                    Regards,
                    Bengt Richter

                    Comment

                    Working...