473,597 Members | 2,341 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to get Windows physical RAM using python?

Thanks.

Jul 18 '05 #1
9 15517
OK, How to check the amount of Windows physical RAM using python?

Mark wrote:
Thanks.


Jul 18 '05 #2
Mark wrote:
OK, How to check the amount of Windows physical RAM using python?


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

Jul 18 '05 #3
Mark <nb***@hotmail. com.nospam> wrote in message news:<bg******* ***@news3.bu.ed u>...
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"').readl ines()
conventionalMem ory = int(memTotals[0].split()[0])
extendedMemory = int(memTotals[1].split()[0])
Jul 18 '05 #4
"Dan Bishop" <da*****@yahoo. com> wrote:
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])


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

--gv
Jul 18 '05 #5
"Martin v. Löwis" <ma****@v.loewi s.de> writes:
Mark wrote:
OK, How to check the amount of Windows physical RAM using python?


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.


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.
from ctypes import *
from ctypes.wintypes import DWORD

SIZE_T = c_ulong

class _MEMORYSTATUS(S tructure): .... _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)
.... memstatus = _MEMORYSTATUS()
windll.kernel32 .GlobalMemorySt atus(byref(mems tatus)) 2147483647 memstatus.show( ) dwLength 32
dwMemoryLength 47
dwTotalPhys 535609344
dwAvailPhys 281993216
dwTotalPageFile 907055104
dwAvailPageFile 720285696
dwTotalVirtual 2147352576
dwAvailVirtualP hys 2117312512


Thomas
Jul 18 '05 #6
Mark wrote:
OK, How to check the amount of Windows physical RAM using python?
Martin v. Lowis wrote: 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.


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
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
Jul 18 '05 #7
Br************* *@yahoo.com (Branimir Petrovic) wrote in message news:<b1******* *************** ****@posting.go ogle.com>...
"Martin v. Löwis" <ma****@v.loewi s.de> wrote in message news:<bg******* ******@news.t-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.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
Jul 18 '05 #8
On Wed, 30 Jul 2003 23:04:56 +0200, =?ISO-8859-1?Q?=22Martin_v =2E_L=F6wis=22? = <ma****@v.loewi s.de> wrote:
Mark wrote:
OK, How to check the amount of Windows physical RAM using python?


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

====< memorystatus.c >============== =============== =============== =
/*
** memorystatus.c
** Version 0.01 20030731 10:45:12 Bengt Richter bo**@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.
import memorystatus
help(memorystat us) 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'

memorystatus.me mstat()

[0, 334929920, 271536128, 942825472, 861339648, 2147352576, 2129780736]

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

Regards,
Bengt Richter
Jul 18 '05 #9
On Fri, 01 Aug 2003 09:02:03 +1000, Mark Hammond <mh******@skipp inet.com.au> 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\Pyt hon\win32all-152.exe

Regards,
Bengt Richter
Jul 18 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

35
7746
by: Vamsi Mudrageda | last post by:
I am kind of new to Python, and after trying and using wxPython, I found it kind of lacking in easy-to-read documentation, speed at loading, and GUI response-time. So I am looking for an another GUI toolkit that is cross-platform for Python, and am leaning toward PyQt (PyGTK is kind of dull looking in comparison). Unfortunately, although TrollTech says Qt is cross-platform, its license strategy has me a bit confused. So here is to...
5
1765
by: John | last post by:
I am creating an application that I would like to have the user type in their User ID, password and domain, and it do Windows Authentication to verify they are a valid user. Can someone provide me with any assistance on this by sample code, or a pointer to a site that goes over this? Thanks. JR
6
7233
by: Novice Experl | last post by:
I'd like to write a simple application that interfaces with the parallel port, and changes the data on it according to keyboard input. I hope I can get it to run under windows xp and / or windows 2000. How can I do this? What do I need to know? It doesn't look like the standard library (the one under my pillow) has that feature. In addition, I've heard that with newer versions of windows don't let you communicate with the port directly,...
7
9526
by: Claudio Grondi | last post by:
Googling for keywords like "direct access sector harddrive Python module Windows" seems to give no useful results. Any hints(best if cross-platform)? Claudio
17
2772
by: Bruce Jin | last post by:
I wonder how many people are using db2 on Windows? I know db2 is native to AS400 which has about 800,000 installations. Thanks!
54
8953
by: Sathyaish | last post by:
I am trying to print to the printer. I am using a VC++ 6.0 compiler on Win 2K, but I get an error saying that stdprn is not defined. Why is that? Is it because of Windows, I am guessing? Do I have to use only the GDI32 functions for printing under Windows? Can I not use the stdprn under Windows to print from memory to the window?
7
20694
by: Mark | last post by:
Hi, I am creating application in VB 2005. and when I print report it adds extra 0.45 cm margin on left and top, and the reason for this is physical margins of printer. Is it possible to change printer's physical margins using VB coding? Cheers -- Osmotion Blue
1
2215
by: mikelujan | last post by:
Hi, Our application starts an external application using System.Diagnostics.Process class and the Start() method, as per code snippet below. This application run as a Windows service, and must start several instances of the same application, like multiple Windows Calculators for instance. We are experiencing difficulties starting applications after a certain number have been started. We changed our app to work as a Console...
0
7971
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7893
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8381
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8040
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8259
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6698
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5847
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
2408
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1495
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.