R – .net Out of Memory exceptions in Windows Mobile – how to overcome this problem

compact-frameworkemailmemory-managementnetwindows-mobile

I am currently writing a small application in Windows Mobile using CF.NET.

The application is very similar in its behaviour to an e-mail application i.e. I am using POP3 to download messages and attachments from my mail-server account to store on the storage-card for further processing with a MIME-tool.

My problem is downloading large attachments because at a certain point I get an OutOfMemory-exception. I found out some interesting things about this by reading the following question and answers published here on SO the other day. Since my attachments can amount to 4-5MB (mp3-songs), I realize that I have serious problems. However, at the same time Pocket Outlook is able to download mp3-songs up to 4-5MB without any problems (using a WIFI-connection) so there must be a way to do it!

At the moment, I am saving the download-buffer (converted into string) into a simple string-variable. I tried adding the content of the buffer into a stringbuilder but I still get OutOfMemory-exceptions.

What strategy/technique could I adapt to overcome this problem?

Best Answer

Any time you're working with larger amounts of data, you want to make sure you don't hold the whole thing in memory at once.

You're download pipe is a stream, so you can load the data in chunks. When writing the file to disk you have a stream, so you can write it a chunk at a time.

Think of unloading a tractor trailer into a warehouse. With your stringbuilder solution, you're basically saying, "I'm going to stand at the back of the trailer and I want you to load the entire contents of the trailer into my arms, then I'll take it into the warehouse". You'll be crushed by the load! What you want instead is, "hand me just enough that I can actually carry, then I'll take it into the warehouse, and then come back for the next load".