Click here to Skip to main content
Click here to Skip to main content

Full-Featured Task Manager for Windows Mobile

By , 17 Feb 2008
 

Introduction

There are many task managers for Windows Mobile out there, but I've never seen one that shows how much memory is being used by each process. I found this odd until I started digging into why. Microsoft hasn't provided the standard APIs for querying for a process' memory usage because memory works differently on WM. Fortunately, they did provide the Toolhelp32 APIs. This article implements a Task Manager for Windows Mobile that utilizes the Toolhelp32 library to take a snapshot of the heap and tally the memory usage for every process.

Background

The company I work for recently implemented a new mobile email solution. I found my phone/PPC's memory was frequently around 1MB even though the Task Manager that comes with WM didn't list any running processes. I decided to write a quick utility to walk active processes, and found the built-in task manager was only displaying processes which had a main window. I turned that utility into version 0.5 which displayed all processes, let me kill them, etc. I still hadn't answered my question about which process was chewing up my memory and if it was associated with the new email solution. I was frustrated by the lack of APIs for finding memory usage statistics. I needed to know how much memory the processes associated with my company's email solution was using, so I dug deeper. What I've published here is my final solution.

Using the Code

I won't spend much time describing how the application as a whole works, it's pretty standard. There is a file named Toolhelp32.cs which contains all the P/Invoke signatures needed to use the Toolhelp32 library. Below is the block of code that does the grunt work.

uint GetMemUsage(uint ProcId)
{
  uint MemUsage = 0;
  IntPtr hHeapSnapshot = 
   Toolhelp32.CreateToolhelp32Snapshot(Toolhelp32.TH32CS_SNAPHEAPLIST, ProcId);
  if (hHeapSnapshot != INVALID_HANDLE_VALUE)
  {
    Toolhelp32.HEAPLIST32 HeapList = new Toolhelp32.HEAPLIST32();
    HeapList.dwSize = (uint)Marshal.SizeOf(HeapList);
    if (Toolhelp32.Heap32ListFirst(hHeapSnapshot, ref HeapList))
    {
      do 
      {
        Toolhelp32.HEAPENTRY32 HeapEntry = new Toolhelp32.HEAPENTRY32();
        HeapEntry.dwSize = (uint)Marshal.SizeOf(HeapEntry);
        if (Toolhelp32.Heap32First(hHeapSnapshot, ref HeapEntry, 
                    HeapList.th32ProcessID, HeapList.th32HeapID))
        {
          do
          {
            MemUsage += HeapEntry.dwBlockSize;
          } while (Toolhelp32.Heap32Next(hHeapSnapshot, ref HeapEntry));
        }
      } while (Toolhelp32.Heap32ListNext(hHeapSnapshot, ref HeapList));
    } 
    Toolhelp32.CloseToolhelp32Snapshot(hHeapSnapshot);
  }
  return MemUsage;
}

CreateToolhelp32Snapshot creates a snapshot of processes, threads, heaps, and modules of a process. I pass in the TH32CS_SNAPHEAPLIST, telling it I want information about the heap list. Once I have the snapshot, I walk the heaplists with the Heap32ListFirst and Heap32ListNext calls. For each list, I walk the heap with Heap32First and Heap32Next, and add up the block sizes for each entry in each list. Voila, the total allocated blocks for a process.

Points of Interest

There are lots of great articles out there on memory management for WM. I wish I had saved them so I could post the links here. All I can say is memory management on WM, while not nearly as robust as for a desktop OS, is much more complicated. Read-up before attempting anything more than Hello World for WM. And yes, the new email solution was the culprit.

History

  • Initial version posted 02/14/2008 8:50PM (EST).

License

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

About the Author

Brian P. Adams

United States United States
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionReal memory usage ?membersir_zealot@go2.pl19-Aug-09 3:12 
Hello. I found this article very interesting but is it real memory ussage. I have downloaded MobileTaskManager for WidnowsCE and it shows different values. I think there should be some substraction from this memory ussage? Further more I am not able to see memory usage of NK.EXE process. I found that I may not have rights to see it. Is it possible ?
 
Thanks.
QuestionKill a process ??memberGamePlanner17-Jul-09 5:57 
When I try to kill a process, it comes to ArgumentException since the processid gets expired. So it tells us that the process by that name/id no longer exists. Actually that exe still keeps running but probably with a different process id. Even when I try to kill the ProcessEntry itself via ProcessEntry.Kill method, it doesn't do anything (doesn't even raise any exception). Do you have any idea what might be going on?
 
I have a Windows Mobile 6 standard running on t-mobile HTC dash Excalibur smartphone. I have signed my exe file with a priv. developer certificate so I'll assume it has the security permissions.
 
Thanks
GeneralErrormemberPavanPareta22-Nov-08 0:07 
While Buid application i m got Error
 
Error 1 'MobileTaskManager.frmMobileTaskManager.TaggedListViewSubItem.Tag.get' must declare a body because it is not marked abstract or extern
 
Manager.cs 21 22 MobileTaskManager
 

public uint Tag { get; set; }
 
Pavan Pareta

GeneralRe: ErrormemberBrian P. Adams1-Jun-09 6:19 
Need to use VS2008
GeneralRe: regarding codememberMarijn Stevens10-Jul-08 13:39 
In case you made sure all the resources are free again than you shouldn't see a difference. In case you do see a difference it can be your application, but also other applications.
 
A house is just a place to keep your stuff while you go out and get more stuff. - George Carlin

GeneralBuilding the project with VS2005 and removing OpenNETCFmemberJeoff Hines15-May-08 2:55 
I hacked the project to build under VS2005 and I removed the dependencies to OpenNETCF by using code from another project.
Brian, are you interested? I would like to send you the changes and maybe you can use them.
GeneralRe: Building the project with VS2005 and removing OpenNETCFmemberGary Dobbins6-Jun-08 4:54 
I'd be interested in that mod - just tried to run it on a Treo700w, but it crashes .NETCF, so a native compilable would be most welcome.
GeneralRe: Building the project with VS2005 and removing OpenNETCFmemberJeoff Hines9-Jun-08 3:13 
I am not sure how to post my changes. Can you offer any advice?
 
Also, the program still uses .NETCF and C#; it is not a native project. I only removed the references to the OpenNETCF library (see http://www.opennetcf.com/)
 
As for your crash, did you copy the OpenNETCF.XYZ.dll assemblies to the same folder as the EXE? I did not do that and got crashes on my device when I ran MobileTaskManager.exe at first, which is one of the reasons I removed the dependencies.
GeneralVS2005 versionmemberfbausch10-Mar-08 6:10 
would be nice to have a version which would build on both VS2005 and VS2008.
GeneralNice onemembergore0118-Feb-08 0:22 
Thank you for this code.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130617.1 | Last Updated 17 Feb 2008
Article Copyright 2008 by Brian P. Adams
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid