【问题标题】:Windows Phone 8.1 memory issueWindows Phone 8.1 内存问题
【发布时间】:2015-11-13 17:53:29
【问题描述】:

我有 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); 在本机代码中释放内存,这没关系。但是我不明白,为什么byte的数组没有释放?

附:我可以使用本机代码转储数据,但我想了解 GC 的工作原理(我已经阅读过 msdn)。

【问题讨论】:

标签: c# c++ windows-phone-8.1 c++-cx


【解决方案1】:

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;
}

一切都很好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-07
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多