2

我遇到了 WPF 应用程序的性能问题。使用分析器,我可以看到在我的一个窗口中,随着时间的推移创建了一些实例并保持活动状态,因此使用的内存也在增长。

这是从分析器捕获的图表,其中包含活动实例的数量(红色)和内存消耗(粉红色)。

在此处输入图像描述

应用程序在这 15 分钟内没有收到用户的任何输入,并且正在创建的实例的类型是 WeakReference。在其他帖子中,我看到 WeakReference 的泄漏可能是由事件注册的错误使用引起的,但在这种情况下,我在配置文件会话期间只打开了一个窗口,所以我怀疑这可能是调度程序的问题。

我使用调度程序从网络摄像头捕获图像,进行一些处理并以这种方式在 WPF 窗口中显示处理后的图像:

private delegate void AnalyzeImage();
Image<Bgr, byte> image;
String imagesFolder = "..\\..\\"

public MyWindow(){
    InitializeComponent();
    AnalyzeImage fetcher = new AnalyzeImage(this.getCard);
    fetcher.BeginInvoke(null, null);
}


private void UpdateInterface()
{
    ListReadCards.Items.Add(cardName);
    imageBackup.Source = ImageProcessor.ToBitmapSource(image);
    AnalyzeImage fetcher = new AnalyzeImage(this.getCard);
    fetcher.BeginInvoke(null, null);
    if (card > cardsToRead)
    {
        finished = true;
        this.Close();
    }
}


public void getCard()
{
    Image<Gray, byte> thresholdImage;
    cardName = ImageProcessor.getCard(capture.QueryFrame(), out thresholdImage, out image, imagesFolder);
    this.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, new AnalyzeImage(UpdateInterface));
    if (cardName != null)
    {
        hand.Add(cardName);
        card++;
    }
}

已经尝试在 for 循环中执行ImageProcessor.getCard1ImageProcessor.ToBitmapSource亿次以检查这些调用中是否存在问题,但在这种情况下实例和内存不会增长。

4

0 回答 0