SQL CE OutOfMemoryException consuming web service

2019-08-13 00:06发布

问题:

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.

回答1:

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.



回答2:

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.