OutOfMemoryException handling and Windows Media Player SDK



如何工作:

WindowsMediaPlayer windowsMediaPlayer = new WindowsMediaPlayer(); 
IWMPMediaCollection collection = windowsMediaPlayer.mediaCollection;
IWMPMedia newMedia = collection.add(path);  //causes OutOfMemoryException after some thousands  method's iterations

我已经尽量避免这样做了:

try
{
    newMedia = collection.add(path);
    return newMedia;
}
catch (Exception)
{
    collection = null;
    GC.Collect();
    GC.WaitForPendingFinalizers();
    WindowsMediaPlayer windowsMediaPlayer = new WindowsMediaPlayer();
    IWMPMediaCollection collectionNew = windowsMediaPlayer.mediaCollection;
    return CreateNewMedia(collectionNew, path);
}

但是这不起作用-我仍然在catch中得到无限异常循环。

你不能像普通的那样处理OutOfMemoryException。处理这个问题的原因可能只是以某种方式保存应用程序的状态,以便为应用程序的使用者提供一种恢复丢失数据的方法。我的意思是调用GC.Collect或其他什么没有意义,因为应用程序无论如何都会死亡,但是CLR请提前通知您。

所以要解决这个问题,检查你的应用程序的内存消耗,在32bit机器上必须是1.2GB左右的RAM,或者控制集合中元素的数量,不能超过,对于普通列表,32位和64位上的2GB内存。

最新更新