【发布时间】: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