Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Mobile

How to get individual process memory usage statistics in Windows Mobile

4.50/5 (2 votes)
29 Oct 2010CPOL 30.9K  
Is GetMemoryStatus() not granular enough for you? Want to know how much memory is being used by each process in your Windows Mobile device? Then you want CeGetProcVMInfo(). This provides the same memory usage information you see in Task-Manager.


If you have platform builder, include pkfuncs.h. If not, do some Internet searching to get the definitions you need. (It really just wraps a call to the kernel ioctl IOCTL_KLIB_GETPROCMEMINFO)


Windows Mobile has 32 process slots, we call it once for each possible slot. However, slot 0 is a duplicate of our current process slot and will just return an error code and slot 1 is for "execute in place" DLLs, so we can skip those. If it returns FALSE that probably just means there's no process running in that slot.
DWORD total = 0;
for( int idx = 2; idx < 33; ++idx )
{
    PROCVMINFO vmi = { 0 };
    if( ::CeGetProcVMInfo( idx, sizeof( PROCVMINFO ), &vmi ) )
    {
        NKDbgPrintfW( L"%d: %d bytes\r\n", idx, vmi.cbRwMemUsed );
        total += vmi.cbRwMemUsed;
    }
}
NKDbgPrintfW( L"Total: %d bytes\r\n", total );


For me, this prints:
    2:  5,812,224 bytes
    3:    327,680 bytes
    4:    610,304 bytes
    5:  1,409,024 bytes
    6:  1,028,096 bytes
    7:    765,952 bytes
    8:     36,864 bytes
    9:    450,560 bytes
   10:    438,272 bytes
   11:     69,632 bytes
   12:    245,760 bytes
   13:    253,952 bytes
   14:    831,488 bytes
   15:    122,880 bytes
   16:    606,208 bytes
   17:    139,264 bytes
   18:    266,240 bytes
   19:  1,007,616 bytes
   20:  1,273,856 bytes
   21:    180,224 bytes
   22:     73,728 bytes
Total: 15,949,824 bytes

If you want to relate that process index to something useful (like the name of the executable), you can use GetProcessIDFromIndex() and match it to the th32ProcessID member of the PROCESSENTRY32 structure returned by the ToolHelper API.

Enjoy!

-PaulH

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)