1

So I'm trying to get the available and the total RAM of my PC via python. That's what I have by now:

def get_memory_status():
    kernel32 = ctypes.windll.kernel32
    c_ulong = ctypes.c_ulong
    class MEMORYSTATUS(ctypes.Structure):
        _fields_ = [
            ("dwLength", c_ulong),
            ("dwMemoryLoad", c_ulong),
            ("dwTotalPhys", c_ulong),
            ("dwAvailPhys", c_ulong),
            ("dwTotalPageFile", c_ulong),
            ("dwAvailPageFile", c_ulong),
            ("dwTotalVirtual", c_ulong),
            ("dwAvailVirtual", c_ulong)
        ]
    memoryStatus = MEMORYSTATUS()
    memoryStatus.dwLength = ctypes.sizeof(MEMORYSTATUS)
    kernel32.GlobalMemoryStatus(ctypes.byref(memoryStatus))

    return (memoryStatus.dwAvailPhys, memoryStatus.dwTotalPhys)

avail, total = get_memory_status()
print avail + " " + total

If I execute this, I always get the same values for the available and the total RAM. When i ask for dwMemoryLoad I get the same value that is shown in Window's Task Manager as 'Physical Memory' which is the percentage of used RAM (which is not 0). But I want the precise number of bytes. Am I doing something wrong?

I can't use any extra librarys, if I could, I would already have done.

patsimm
  • 476
  • 6
  • 19

1 Answers1

3

According to the note at the GlobalMemoryStatus MSDN entry:

On Intel x86 computers with more than 2 GB and less than 4 GB of memory, the GlobalMemoryStatus function will always return 2 GB in the dwTotalPhys member of the MEMORYSTATUS structure. Similarly, if the total available memory is between 2 and 4 GB, the dwAvailPhys member of the MEMORYSTATUS structure will be rounded down to 2 GB. If the executable is linked using the /LARGEADDRESSAWARE linker option, then the GlobalMemoryStatus function will return the correct amount of physical memory in both members.

And, right at the top of the article, there is:

[GlobalMemoryStatus can return incorrect information. Use the GlobalMemoryStatusEx function instead.]

Btw, this function is present in the pywin32 library.

ivan_pozdeev
  • 33,874
  • 19
  • 107
  • 152