Windows手机中的异常内存

时间:2016-02-05 05:32:38

标签: c# windows-phone-8.1 windows-phone out-of-memory heap-memory

我正在使用Lumia 640进行开发。 我在Windows Phone 8.1 Silver Light应用程序中获得了异常内存。
我试图处理流,从内存中删除位图,但仍面临这个问题。

当使用CameraCaptureTask收集超过5张图片时,它会抛出“内存超出异常”

我在这里构建我的演示代码。

  private void Button_Click(object sender, RoutedEventArgs e)
    {
        CameraCaptureTask cameraCaptureTask = new CameraCaptureTask();
        cameraCaptureTask.Show();
        cameraCaptureTask.Completed += new EventHandler<PhotoResult>(this.CameraCaptureTaskCompleted);
    }

    private async void CameraCaptureTaskCompleted(object sender, PhotoResult e)
    {
        if (e.TaskResult == TaskResult.OK)
        {

            Debug.WriteLine("[ApplicationCurrentMemoryUsage](MB) : \n" + (DeviceStatus.ApplicationCurrentMemoryUsage / 1024 / 1024));

            Image attachedImage = new Image();

            Stream fileStream = e.ChosenPhoto;

            string strFolderName = "Capture";

            IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication();

            var fileName = Guid.NewGuid() + ".JPG";
            if (!isf.DirectoryExists(strFolderName))
            {
                isf.CreateDirectory(strFolderName);
            }

            if (isf.FileExists(strFolderName + "\\" + fileName))
            {
                isf.DeleteFile(strFolderName + "\\" + fileName);
            }
            IsolatedStorageFileStream isfs = isf.CreateFile(strFolderName + "\\" + fileName);

            int bytesRead = 0;
            var bufferSize = Convert.ToInt32(fileStream.Length);
            var fileBytes = new byte[fileStream.Length];
            while ((bytesRead = await fileStream.ReadAsync(fileBytes, 0, bufferSize)) > 0)
            {
                isfs.Write(fileBytes, 0, bytesRead);
            }

            BitmapImage bitImage = new BitmapImage();
            bitImage.SetSource(fileStream);

            bitImage.DecodePixelType = DecodePixelType.Physical;
            bitImage.CreateOptions = BitmapCreateOptions.BackgroundCreation;
            bitImage.CreateOptions = BitmapCreateOptions.DelayCreation;


            double dblRation = (double)bitImage.PixelWidth / bitImage.PixelHeight;

            //if (bitImage.PixelWidth > bitImage.PixelHeight)
            //{
            //    bitImage.DecodePixelWidth = 200;

            //    bitImage.DecodePixelHeight = 200 / (int)dblRation;
            //}
            //else
            //{
            //    bitImage.DecodePixelHeight = 200;

            //    bitImage.DecodePixelWidth = (int)(200 * dblRation);
            //}


            attachedImage.Source = bitImage;

            GC.Collect();

            GC.WaitForPendingFinalizers();
            fileStream.Flush();
            fileStream.Dispose();
            fileStream.Close();
            bitImage = null;
            fileStream = null;
            fileBytes = null;


            sp.Children.Add(attachedImage);
            Debug.WriteLine("[ApplicationCurrentMemoryUsage](MB) : \n" + (DeviceStatus.ApplicationCurrentMemoryUsage / 1024 / 1024));
        }
    }

这只是演示代码。当我选择第一个图像时,它将从8 MB上升到37 MB。但在实际应用中,它将从64 MB上升到112 MB。

请帮我解决一下。

0 个答案:

没有答案