Thread: How can I see the available memory ?

  1. #1
    Registered User
    Join Date
    Nov 2009
    Location
    GR
    Posts
    12

    How can I see the available memory ?

    Hello my dear friends

    I want to write a simple C++ program that returns 2 things:
    Used Memory
    Free Available Memory

    The reason I am doing this, is because I want to avoid the use of Task Manager. So I am looking for advice on how to do it. I was searching around the net and I found How to: Investigate Memory Usage for Processes . To speak the truth, I didn't understand much .

    or ::GetProcessMemoryInfo function ?

    I think that this is a simple program to be codded . Hm ?
    Last edited by Dr.Paneas; 03-03-2010 at 07:04 AM.

  2. #2
    Registered User
    Join Date
    Nov 2009
    Location
    GR
    Posts
    12
    I am trying to compile this :

    Code:
    #include <windows.h>
    #include <stdio.h>
    #include <psapi.h>
    #pragma comment(linker, "/DEFAULTLIB:psapi.lib")
    
    void PrintMemoryInfo( DWORD processID )
    {
        HANDLE hProcess;
        PROCESS_MEMORY_COUNTERS pmc;
    
        // Print the process identifier.
    
        printf( "\nProcess ID: %u\n", processID );
    
        // Print information about the memory usage of the process.
    
        hProcess = OpenProcess(  PROCESS_QUERY_INFORMATION |
                                        PROCESS_VM_READ,
                                        FALSE, processID );
        if (NULL == hProcess)
            return;
    
        if ( GetProcessMemoryInfo( hProcess, &pmc, sizeof(pmc)) )
        {
            printf( "\tPageFaultCount: 0x%08X\n", pmc.PageFaultCount );
            printf( "\tPeakWorkingSetSize: 0x%08X\n",
                      pmc.PeakWorkingSetSize );
            printf( "\tWorkingSetSize: 0x%08X\n", pmc.WorkingSetSize );
            printf( "\tQuotaPeakPagedPoolUsage: 0x%08X\n",
                      pmc.QuotaPeakPagedPoolUsage );
            printf( "\tQuotaPagedPoolUsage: 0x%08X\n",
                      pmc.QuotaPagedPoolUsage );
            printf( "\tQuotaPeakNonPagedPoolUsage: 0x%08X\n",
                      pmc.QuotaPeakNonPagedPoolUsage );
            printf( "\tQuotaNonPagedPoolUsage: 0x%08X\n",
                      pmc.QuotaNonPagedPoolUsage );
            printf( "\tPagefileUsage: 0x%08X\n", pmc.PagefileUsage );
            printf( "\tPeakPagefileUsage: 0x%08X\n",
                      pmc.PeakPagefileUsage );
        }
    
        CloseHandle( hProcess );
    }
    
    int main( )
    {
        // Get the list of process identifiers.
    
        DWORD aProcesses[1024], cbNeeded, cProcesses;
        unsigned int i;
    
        if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
            return 1;
    
        // Calculate how many process identifiers were returned.
    
        cProcesses = cbNeeded / sizeof(DWORD);
    
        // Print the memory usage for each process
    
        for ( i = 0; i < cProcesses; i++ )
            PrintMemoryInfo( aProcesses[i] );
    
    	return 0;
    }
    but in Code::Blocks I get 2 errors:
    C:\Users\panos\Documents\OUTOFSCHOOL\LESSON_1\memo ry.o:memory.cpp.text+0x5b)||undefined reference to `_GetProcessMemoryInfo@12'|

    C:\Users\panos\Documents\OUTOFSCHOOL\LESSON_1\memo ry.o:memory.cpp.text+0x179)||undefined reference to `_EnumProcesses@12'|
    ||=== Build finished: 2 errors, 0 warnings ===|


    ===============

    I found out googling that I have to enter this: -lpsapi in the cmd. But how can I ?
    Last edited by Dr.Paneas; 03-03-2010 at 07:33 AM.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    With Code::Blocks in the Project Options or Settings (whichever it is called) which should have a Linker options / Commandline arguments to linker or a similar tab.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

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