.net Out of Memory exceptions in Windows Mobile - how to overcome this problem?

試著忘記壹切 提交于 2019-12-11 19:08:01

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/678025/net-out-of-memory-exceptions-in-windows-mobile-how-to-overcome-this-problem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!