Turning off the BSTR cache in Windows CE

By default, whenever you use BSTRs OLE will keep the memory cached in order to aid performance.  There are scenarios where you may want to disable this.  First would be if you're on a device with not a lot of memory and don't want it being spent in the cache.  Another is when you're debugging into a memory leak, in which case caches make your life much harder.

On desktop windows, you can disable the cache by setting the environment variable OANOCACHE=1 (https://support.microsoft.com/kb/139071).  CE doesn't have environment variables, so that leaves us the registry.  You need to put your process into the MULTI_SZ registry list below.  You don't have to reboot the device in order to make this change take effect, since OLE reads this at process startup time.  This is a CE6.0 and later feature only.

[HKEY_LOCAL_MACHINE\Software\Microsoft\OLE]
"NoBstrCache"=multi_sz:”process1.exe”,”process2.exe”,...

Processes not listed in the reg key behave as before, namely they use the BSTR cache.  By default you should not disable the BSTR cache unless you have a good reason as it is critical for performance in BSTR heavy apps.  My former boss wanted to remove it to save RAM, but when he did I think it degraded VBScript performance by 50 times or something.

This is yet another jspaith enlightened selfishness feature.  This has helped a lot of teams in MS and I hope it can help our customers, but the person it probably helps most is me.  Now I have an easy registry key to give people to convince them that COM isn't leaking their memory :).

[Author: John Spaith]