簡體   English   中英

Windows Phone 8.1內存問題

[英]Windows Phone 8.1 memory issue

我有8個大塊內存(每個28mb)。 它們存儲在非托管(本機)代碼中。 我想將它們轉儲到文件中。 但在我轉儲它們之后,它們沒有被釋放,我有230mb忙碌的內存,這使得無法繼續運行我的應用程序。 進一步拋出OutOfMemory。 這里是我的轉儲代碼:

//* Dump input images
for (int i = 0; i < frames.Length; i++)
{
    StorageFile file = await localFolder.CreateFileAsync(
        string.Format("FRAME_{0}.yuv", i), 

    CreationCollisionOption.ReplaceExisting);

    using (var stream = await file.OpenStreamForWriteAsync())
    {
        byte[] image = SwapHeap.copyFromHeap(frames[i].ImagePtr, (int)frames[i].Width * (int)frames[i].Height * 3 / 2);
        await stream.WriteAsync(image, 0, image.Length);
        await stream.FlushAsync();
        image = null;
    }
}
...
System.GC.Collect();

和本地代碼從int指針獲取byte[]

Array<uint8>^ SwapHeap::copyFromHeap(const int ptr, int length) 
{
    Array<uint8>^ res = ref new Array<uint8>(length);
    memcpy(res->Data, (byte*)ptr, length);
    return res;
}

我使用free((byte*)ptr);在本機代碼中釋放內存free((byte*)ptr); 沒關系 但我無法理解,為什么byte數組不會被釋放?

PS我可以使用本機代碼轉儲數據,但我想了解GC的工作原理(msdn我已經讀過)。

看起來像Stream類中的問題。 據我所知,從不同的測試中,它鎖定了byte數組,它被寫入流。 並且它沒有關閉或using塊外部釋放。 如果使用FileIO而不是Stream並將轉儲代碼更改為:

// Dump input images
for (int i = 0; i < frames.Length; i++)
{
    StorageFile file = await Windows.Storage.KnownFolders.PicturesLibrary.CreateFileAsync(
        string.Format("FRAME_{0}.yuv", i), CreationCollisionOption.ReplaceExisting);

    byte[] image = SwapHeap.copyFromHeap(frames[i].ImagePtr, (int)frames[i].Width * (int)frames[i].Height * 3 / 2);
    await FileIO.WriteBytesAsync(file, image);
    image = null;
    file = null;
}

一切順利。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM