Advertisement

Amount of RAM left...

Started by July 21, 2001 05:14 PM
3 comments, last by PyroMeistar 23 years, 7 months ago
In Visual C++, is there any function that returns the amount of physical memory left? --I don''t want to end up doing successive malloc() until it returns NULL...--
Good news for you, there is.

Use the structure MEMORYSTATUS, and the function GlobalMemoryStatus()

or

MEMORYSTATUSEX and GlobalMemoryStatusEx() ONLY ON Windows2000 or later!!! (They do not work with Win9x)


Here's an example on how to use it. It's almost too easy, really.
It requires the header Winbase.h, which is included in Windows.h

      MEMORYSTATUS	MemStat;MEMORYSTATUSEX  MemStatEx;GlobalMemoryStatus(&MemStat);MemStatEx.dwLength = sizeof(MemStatEx);GlobalMemoryStatus(&memStatEx);  



The MEMORYSTATUS and MEMORYSTATUSEX structures are as follows:


  // First MEMORYSTATUS      typedef struct _MEMORYSTATUS {   DWORD dwLength;          DWORD dwMemoryLoad;   SIZE_T dwTotalPhys;   SIZE_T dwAvailPhys;   SIZE_T dwTotalPageFile;   SIZE_T dwAvailPageFile;   SIZE_T dwTotalVirtual;   SIZE_T dwAvailVirtual; } MEMORYSTATUS, *LPMEMORYSTATUS; // And then MEMORYSTATUSEXtypedef struct _MEMORYSTATUSEX {  DWORD dwLength;   DWORD dwMemoryLoad;   DWORDLONG ullTotalPhys;   DWORDLONG ullAvailPhys;   DWORDLONG ullTotalPageFile;   DWORDLONG ullAvailPageFile;   DWORDLONG ullTotalVirtual;   DWORDLONG ullAvailVirtual;   DWORDLONG ullAvailExtendedVirtual;} MEMORYSTATUSEX, *LPMEMORYSTATUSEX;       


Here's a description of each member from MSDN

Again MEMORYSTATUS first

Members
dwLength
Size of the MEMORYSTATUS data structure, in bytes. You do not need to set this member before calling the GlobalMemoryStatus function; the function sets it.
dwMemoryLoad
Windows NT 4.0 and earlier: The percentage of approximately the last 1000 pages of physical memory that is in use.
Windows 2000 and later: The approximate percentage of total physical memory that is in use.
dwTotalPhys
Total size of physical memory, in bytes.
dwAvailPhys
Size of physical memory available, in bytes.
dwTotalPageFile
Size of the committed memory limit, in bytes.
dwAvailPageFile
Size of available memory to commit, in bytes.
dwTotalVirtual
Total size of the user mode portion of the virtual address space of the calling process, in bytes.
dwAvailVirtual
Size of unreserved and uncommitted memory in the user mode portion of the virtual address space of the calling process, in bytes.

Then MEMORYSTATUSEX

Members
dwLength
Size of the MEMORYSTATUSEX data structure, in bytes. You must set this member before calling the GlobalMemoryStatusEx function.
dwMemoryLoad
Number between 0 and 100 that gives a general idea of current memory utilization, in which 0 indicates no memory use and 100 indicates full memory use.
ullTotalPhys
Total size of physical memory, in bytes.
ullAvailPhys
Size of physical memory available, in bytes.
ullTotalPageFile
Size of the committed memory limit, in bytes.
ullAvailPageFile
Size of available memory to commit, in bytes.
ullTotalVirtual
Total size of the user mode portion of the virtual address space of the calling process, in bytes.
ullAvailVirtual
Size of unreserved and uncommitted memory in the user mode portion of the virtual address space of the calling process, in bytes.
ullAvailExtendedVirtual
Size of unreserved and uncommitted memory in the extended portion of the virtual address space of the calling process, in bytes.

I hope this is what you were looking for!

Feel free to email me.

Edited by - Lord Karnus on July 21, 2001 6:48:29 PM
[email=JValentine_13@hotmail.com]contact[/email]
Advertisement
Wow! Couldn''t ask for more! Thanks!
Thats some very handy stuff!

Anyone know if there anything similar to check available videocard memory (thru opengl or whatever)?

[size="1"]
Here''s one that I''ve been pondering over how to catch the type of CPU one has, I thought about including this in an app of mine but I changed my mine when i thought about the varety of Processors out there.

This topic is closed to new replies.

Advertisement