Kevin Ragsdale: Visual FoxPro 9.0

From A Hog To Just A Pig With A Simple Windows API Call

One of the first questions I got about the Desktop Alerts was, “How much memory and other system resources does this use?”

This was during the presentation in Chicago in December of 2006. My response was the typical, “Oh, not much at all. It’s very light.”

Well, I fired up Task Manager (in front of everyone in the room), and was shocked to find this tiny little app using 10MB of memory " even when it was just sitting there, doing nothing!

“Hmmm,” I thought, “This thing is a hog.”

Not a huge issue, especially compared to a Catastrophic Failure, so I put it on the backburner (which is to say I promptly forgot all about it — if only my apps could free up memory as easily as my brain does).

I recently worked on an app that uses the Microsoft Web Browser control on a Visual FoxPro form as the primary UI for the app. Works well for the most part, but checking memory usage in Task Manager showed a tremendous amount of resources being hogged by the app, even when the user is just sitting there looking at the screen and doing nothing.

“Hmmm,” I thought, “this thing is a hog, too.”

(I started looking at the app’s memory usage after a couple of users with “programming experience” mentioned it…)

Luckily for me, in January of this year Sergey Berezniker wrote about using the Windows API to lower the memory usage of your application.

Simple, straightforward, and very simple to implement.

I put it in the app, and was amazed by how much memory was saved by a simple function call. I went from about 60MB at application startup to about 8MB.

After 6 months in production, I’ve seen no problems reported about the app that would indicate a problem with this function, so yesterday I decided to look at adding it to the Desktop Alerts.

Here’s a quick sampling of the results (Windows XP Professional Service Pack 3):

1. AlertManager Initialized, waiting for something to do:

Before Change (Hog) After Change (Pig)

2. First Alert created and visible:

Before Change (Hog) After Change (Pig)

3. Three alerts visible on screen:

Before Change (Hog) After Change (Pig)

4. All alerts cleared, AlertManager waiting for next command:

Before Change (Hog) After Change (Pig)

 

So, with one simple Windows API call, we’ve reduced the memory usage by almost 80%, turning this gigantic hog into a smaller pig. I’ve seen similar results in Vista, though the memory usage in Vista was much lower to start with than in XP, so the results of the change aren’t quite as dramatic.

One interesting thing, though. See the User Name column in the screenshots? The user changed from Kevin Ragsdale to SYSTEM. So, not only has a hog become a pig, its name has been changed as well. In Windows Vista, the User Name stays put as Kevin Ragsdale.

Anyone that can explain this to me without causing my head to explode — feel free to explain in the comments.

This API function is available in Windows 2000 and higher so, if you have any OS stragglers you’ll want to bracket the function call with something like this:

IF VAL(OS(3)) >= 5    && Skip if earlier than Windows 2000
   ** Do your thing
ENDIF

And yes, I do have a ton of code “out there” that uses this bracket a lot…