views:

299

answers:

2

Hi Guys,

We are continually receiving OutOfMemory exceptions when trying to download documents via a web service. We are storing the documents byte array data as part of a serialized mesage object and the original documents are all ~500kb in size. The only other thing in the message's object graph are two string properties for correlation.

We have tried retrieving the document compressed and uncompressed with the same issues. Now I believe that the exception is been raised when trying to deserialize the message and not while downloading the stream.

I know this is a vague question, but do any of you have any idea what could be causing this? The amount of data transferred here seems very small and there is little else happening on the device at the time of transfer.

A: 

In order to get OutOfMemory exception the code either has a bug that consumes memory until the device has no more, or the device generally doesn't have enough memory to handle even 500Kb variations.

You say "while trying to download documents" maybe you keep them in memory and you get the exception.

Petros
+3  A: 

Memory is VERY tight on Windows Mobile devices -- it really is a world of pain to work with. First of all, your total memory for a process is (if I remember right) 32 MB. Now that gets filled up not just with in-code memory allocations, but also loaded code from DLLs, the EXE and such.

Now when you're downloading the 500KB into a byte array, it doesn't just need 500KB. It needs 500KB of CONTIGUOUS MEMORY, something it's very unlikely to find once the app has been running for a short while.

My recommendation would be to download the files directly with an HttpRequest rather than the web service. Maybe the web service can pass back the URL that you need or something like that. But download the file directly and you can process it in chunks -- read 8K, then write that 8K to disk, then read another 8K, etc.

Clyde
Thanks for the advice, very helpful. Is there any way of increasing the Per-Process memory allocation?
Owen
No, unfortunately not. At least not in Windows Mobile 5.0 which is where I have experience. You'll just need to structure your app in such a way that you don't need large chunks of memory at once.Actually, one other thing to try would be to allocate a big byte array at the applciation start...
Clyde
...wait, never mind. If you're doing a web service call I don't think you can use a "ref byte[] buffer". It always creates it's own.
Clyde