I know. It's been nearly a year since I posted anything. Ouch. Bad John.
What inspires a new post? Virtual memory. One of my favorite subjects, but unfortunately, also a common stumbling block on Windows CE, especially on feature-rich Pocket PCs. I've been debugging several stress cases recently that were impacted by virtual memory pressures and I thought I should remind folks about one common source of unnecessary pain.
When you build applications within Platform Builder or the Windows Mobile Adaptation Kit, the default thread stack is 64kB of reserved virtual memory (the physical RAM is committed one page at a time). This is a pretty reasonable limit for most things on an embedded device. However, when you build your applications in Visual Studio, the default thread stack is 1MB of reserved virtual memory. This is often much more than needed and consumes precious virtual memory real estate. Windows CE processes are limited to 32 MB of virtual address space, unless you employ specific techniques to allocate larger blocks outside the process space. If your application only has one or two threads this may not be a big deal, but if your app is heavily multi-threaded, you can very quickly run out of virtual address space on a CE-based device.
The better option is to set the default stack within the VS options back to 64k (see photo) and then allocate larger stacks only if necessary. The way to create a thread with a larger than default stack on Windows CE is to call CreateThread with the STACK_SIZE_PARAM_IS_A_RESERVATION set in dwCreationFlags and dwStackSize set to how large you would like your stack to be. That value will be rounded up to the next 64kB boundary. The physical RAM will still be committed just a page at a time as needed.
I'd personally like to change the VS default to a much smaller value, but that could introduce unexpected backwards-compatibility issues for folks that couldn't be caught at build time. I'm curious to hear feedback though of whether anyone targeting CE-based devices in Visual Studio is relying on the 1MB default? Or if you are changing the stack sizes already? Or just don't care since your apps only have a couple threads anyway?