Memory available

This is a discussion on Memory available within the Windows Programming forums, part of the Platform Specific Boards category; Does anyone have a suggestion as to how I would go about determining the total physical memory, available physical memory, ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    26

    Memory available

    Does anyone have a suggestion as to how I would go about determining the total physical memory, available physical memory, total virtual memory, and available virtual memory?

    I have been trying to implement the GlobalMemoryStatusEX, but I continue to run into a lot of errors.

    I was just looking for something better.

    Thanks.

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    I What errors do you get? Use GlobalMemoryStatus()

    The following works ok;


    Code:
    #include <windows.h>
    #include <iostream>
    using std::cout;
    using std::endl;
    
    
    int main(){
    
    MEMORYSTATUS ms;
    
    GlobalMemoryStatus(&ms);
     
    cout << "Total Phisical Memory - " << ms.dwTotalPhys << endl;
    
    cout << "Total Available Phisical Memory - " << ms.dwAvailPhys  << endl;
    
    cout << "Total Virtual Memory - " << ms.dwTotalVirtual << endl;
    
    cout << "Total Available Virtual Memory - " << ms.dwAvailVirtual  << endl;
    
    
    
    return 0;
    
    }

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    26

    GlobalMemoryStatus

    Thanks very much for your help. I did try your code and it compiled with no errors, but I am worried about this function returning the correct values. I was reading on the MSDN web site under GlobalMemoryStatus that there is a possibility that this function could return incorrect info if the machine has more than 4GB of memory. Also, they suggest to use the GlobalMemoryStatusEX for Windows 2000 and that is what I try to do, but I continue to receive errors.

    For example, the compiler error:

    MEMORYSTATUSEX: undeclared identifier
    GlobalMemoryStatusEx: undeclared identifier
    ms: undeclared identifier

    I feel if these were taken care of the program would work.

  4. #4
    Registered User
    Join Date
    Feb 2002
    Posts
    26
    Also, when I use the GlobalMemoryStatus function, I get incorrect values returned for my virtual memory. The physical memory is pretty accurate.

  5. #5
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    Look into this thread for GlobalMemoryStatus.
    I cannot find the GlobalMemoryEx function in my helpfiles.
    there is a GlobalMemoryVlm, but you don't need this.

    Update:
    GlobalMemoryEx exists, but is not neccessary if you don't work with server systems. "Windows 95/98/Me: Unsupported." means your program will not run on these systems if you use this function.
    :Update


    http://www.cprogramming.com/cboard/s...threadid=13000


    dwLength
    The size in bytes of the MEMORYSTATUS data structure. You do not need to set this member before calling the GlobalMemoryStatus function; the function sets it.

    dwMemoryLoad
    Specifies a 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.

    dwTotalPhys
    Indicates the total number of bytes of physical memory.

    dwAvailPhys
    Indicates the number of bytes of physical memory available.

    dwTotalPageFile
    Indicates the total number of bytes that can be stored in the paging file. Note that this number does not represent the actual physical size of the paging file on disk.

    dwAvailPageFile
    Indicates the number of bytes available in the paging file.

    dwTotalVirtual
    Indicates the total number of bytes that can be described in the user mode portion of the virtual address space of the calling process.

    dwAvailVirtual
    Indicates the number of bytes of unreserved and uncommitted memory in the user mode portion of the virtual address space of the calling process.


    TotalVirtual should be around 2 GB.


    This is a normal result from the above mentioned threads program on my PC ( 512 MB RAM )

    [code]
    44 % of Memory in use.

    536309760 bytes of physical memory.
    295657472 bytes of physical memory available.

    906985472 bytes of pagefile memory.
    685002752 bytes of pagefile memory available.
    Note that this number does not represent the actual physical size of the paging file on disk.

    2147352576 bytes of virtual memory.
    2141839360 bytes of virtual memory available.
    Press any key to continue
    [code]

    I was reading on the MSDN web site under GlobalMemoryStatus that there is a possibility that this function could return incorrect info if the machine has more than 4GB of memory.
    Is that really a problem ? Does your program target server platforms with gigabytes of RAM ?
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  6. #6
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Originally posted by ghe1
    Also, when I use the GlobalMemoryStatus function, I get incorrect values returned for my virtual memory. The physical memory is pretty accurate.
    Is it wrong or is it not what you expected?

    For example, on Desktop Windows PCs, each process is allocated a 4GB address space. ....out of this address space, the only legally accessable range to your program is the usermode codespace available by your process.....on Windows NT based systems, this is the memory range 0x0010000 to 0x7FFEFFFF....or in decimal - 65,536 to 2,147,418,111 which is a total difference of 2,147,352,575 which is the same as the figure returned by nvoigt's return value for virtual memory (out by 1 byte....but what the hell!)

    Its more probable that you have misinterpreted the meaning of this value that the possibility that it is wrong

  7. #7
    Registered User
    Join Date
    Feb 2002
    Posts
    26
    I used what nvoigt had suggested and this is my output:

    68 % of Memory in use.

    511.418 Mbytes of physical memory
    163.039 Mbytes of physical memory available

    2047.875 Mbytes of virtual memory
    2042.672 Mbytes of virtual memory available

    Under computer management, system information I am seeing this memory:

    Total Physical 523,692 KB
    Available Physcial 155,532 KB

    Total Virtual 1,801,592 KB
    Available Virutal 1,051,136 KB

    Just let me know if I am not correctly understanding the memory values.

  8. #8
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,230
    Well one's in KB the other's in MB...haven't bothered converting them, but why don't you try?

  9. #9
    Registered User
    Join Date
    Feb 2002
    Posts
    26
    I have converted them and I see that my phyical memory is very similar, but the virtual memory is off.

    CONVERSIONS:

    Computer Management:

    Total Physical 511.418 MB
    Available Physical 151.886 MB

    Total Virtual 1759.367 MB
    Available Virtual 1026.500 MB

    OUTPUT:
    68 % of Memory in use.

    511.418 Mbytes of physical memory
    163.039 Mbytes of physical memory available

    2047.875 Mbytes of virtual memory
    2042.672 Mbytes of virtual memory available

  10. #10
    Registered User
    Join Date
    Feb 2002
    Posts
    26
    Fordy,

    I see what you mean now...I guess my results are okay. I just really didn't understand the concept but now I do.


    All,

    Thanks for all the help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 01-13-2008, 02:14 AM
  2. Question regarding Memory Leak
    By clegs in forum C++ Programming
    Replies: 29
    Last Post: 12-07-2007, 01:57 AM
  3. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  4. Shared Memory - shmget questions
    By hendler in forum C Programming
    Replies: 1
    Last Post: 11-29-2005, 02:15 AM
  5. What's the best memory (RAM) type?
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 12-15-2001, 12:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21