views:

862

answers:

1

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?

+10  A: 

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".

Clyde
Clyde: Thx. I need to adopt the "chunk"-method. I will try to find some code-examples how to do it. I imagine that I take the contents of the buffer, save it in a file, then I clear the buffer and so on. Correct?
moster67
It depends on how you're actually doing the POP protocol. If you're creating a raw connection and sending and receiving the text, then yes, you'll just get to the part where you're downloading the attachment and do it a bit at a time...
Clyde
However, if you're using a 3rd party library to connect to the POP server, you'll need to see how they support downloading attachments. Any good one will provide an API for downloading the file as a stream. Otherwise you'll need a new API :-).
Clyde
When you do this, be sure to provide the application with an EASY way select the download location (ie, memory card). Without that, you will fill up the primary storage of the device in no time.
plinth
Actually I am trying both solutions - own code and a third-party library. I will try your suggestions and hopefully I can sort it out. Thx
moster67
plinth: interesting point! I will take that into consideration. Thx.
moster67
+1 excellent metaphor!
scraimer