Click here to Skip to main content
6,822,613 members and growing! (19,332 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » Mobile Development » System     Intermediate License: The Code Project Open License (CPOL)

Full-Featured Task Manager for Windows Mobile

By Brian P. Adams

A full-featured Task Manager for Windows Mobile.
C# (C#1.0, C#2.0, C#3.0), .NETCF, Win32, Visual-Studio (VS2008), Dev
Posted:17 Feb 2008
Updated:17 Feb 2008
Views:28,867
Bookmarked:49 times
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
10 votes for this article.
Popularity: 4.80 Rating: 4.80 out of 5
1 vote, 10.0%
1

2

3

4
9 votes, 90.0%
5

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


Member

Location: United States United States

Other popular Mobile Development articles:

  • Writing Your Own GPS Applications: Part 2
    In part two of the series, the author of "GPS.NET" teaches developers how to write GPS applications suitable for the real world by mastering GPS precision concepts. Source code includes a working NMEA interpreter and sample high-precision application in C# and VB.NET.
  • Writing Your Own GPS Applications: Part I
    What is it that GPS applications need to be good enough to use for in-car navigation? Also, how does the process of interpreting GPS data actually work? In this three-part series, I will cover both topics and give you the skills you need to write a commercial-grade GPS application.
  • Learn How to Find GPS Location on Any SmartPhone, and Then Make it Relevant
    A step by step tutorial for getting GPS from any SmartPhone, even without GPS built in, and then making location useful.
  • Windows Mobile, iPhone, Android - Marketplace Comparison
    Detailed comparison between Windows Mobile Marketplace, Apple's iPhone AppStore and Android Market from developer point of view.
  • iPhone UI in Windows Mobile
    It's an interface that works with transparency effects. As a sample I used an interface just like the iPhone one. In this tutorial I am explaining how simple is working with transparency on Windows Mobile.
Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 11 of 11 (Total in Forum: 11) (Refresh)FirstPrevNext
GeneralReal memory usage ? Pinmembersir_zealot@go2.pl4:12 19 Aug '09  
GeneralKill a process ?? PinmemberGamePlanner6:57 17 Jul '09  
GeneralError PinmemberPavanPareta1:07 22 Nov '08  
GeneralRe: Error PinmemberBrian P. Adams7:19 1 Jun '09  
GeneralMessage Removed PinmemberMember 29629136:38 16 Jun '08  
GeneralRe: regarding code PinmemberMarijn Stevens14:39 10 Jul '08  
GeneralBuilding the project with VS2005 and removing OpenNETCF PinmemberJeoff Hines3:55 15 May '08  
GeneralRe: Building the project with VS2005 and removing OpenNETCF PinmemberGary Dobbins5:54 6 Jun '08  
GeneralRe: Building the project with VS2005 and removing OpenNETCF PinmemberJeoff Hines4:13 9 Jun '08  
GeneralVS2005 version Pinmemberfbausch7:10 10 Mar '08  
GeneralNice one Pinmembergore011:22 18 Feb '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads.

PermaLink | Privacy | Terms of Use
Last Updated: 17 Feb 2008
Editor: Smitha Vijayan
Copyright 2008 by Brian P. Adams
Everything else Copyright © CodeProject, 1999-2010
Web22 | Advertise on the Code Project