3

I am creating a Windows Phone 8 application, I have a Windows Phone 8 with 512 MB RAM, When I run the application on the emulator, it runs very well, but when I check on Windows Phone 8 device, I am getting exception

System.OutOfMemoryException

When there still remains lot of free memory. See my code below:

private IsolatedStorageFileStream isoVideoFile;
string isoVideoFileName = "Movie.mp4";

using (isoVideoFile = new IsolatedStorageFileStream(isoVideoFileName,
FileMode.OpenOrCreate, FileAccess.ReadWrite,
IsolatedStorageFile.GetUserStoreForApplication()))
{
   using (MemoryStream stream = new MemoryStream())
   {
     isoVideoFile.Write(stream.GetBuffer(), 0, (int)stream.Position);
   } 
     byte[] binaryData = new Byte[isoVideoFile.Length];

     long bytesRead = isoVideoFile.Read(binaryData, 0, (int)isoVideoFile.Length);
     string videofile = Convert.ToBase64String(binaryData, 0, binaryData.Length);                               
 }
8
  • 1
    Do you really want to place all the file content into the memory? Can't you convert the file chunk by chunk? Jun 17, 2014 at 9:56
  • Yeah. VIDEO file assumes a video file. How large is it? I Would strongly suggest not reading it like this - use smaller buffers (512kb) and just move chunk by chunk.
    – TomTom
    Jun 17, 2014 at 9:57
  • The fact that your device has x free MB does not mean that you can actually allocate x MB. There is this thing called memory fragmentation. Also, there is likely a per-app limit.
    – dandan78
    Jun 17, 2014 at 9:57
  • 1
    So you load a whole movie into memory stream, then discard the stream, then read the movie again into an array of bytes (ignoring how many bytes were actually read) and then you convert that to a Base64 string. No wonder you run out of memory.
    – GSerg
    Jun 17, 2014 at 10:01
  • 1
    Ih ate to say, but - dude- this is not "new to scenario and platform", this is "programming for not total beginners".
    – TomTom
    Jun 17, 2014 at 11:01

1 Answer 1

0

You assume it is "no memory". It really is "not enough memory in one piece". LOH fragmentation (Large Object Heap) is a known problem.

Loading a file like that is terrible inefficient.

First:

new MemoryStream()

That is TONS of reallocations of the array. Initialize it to the size of the array beforehand.

But better:

Move the data in 512kb chunks, so you are not such a memory hog. No need to load it all into memory at once.

2
  • thank you man, but can you show me by code, I do not know,thanks for ur quick reply!! Jun 17, 2014 at 10:08
  • Yes, I can, but I generally do not provide "ready, I did your work for you" solution out of principle. I eblieve people should use their brain to work through the documentation once they get a hint what the problem is.
    – TomTom
    Jun 17, 2014 at 11:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.