简体   繁体   中英

Process Memory increase WPF application

I'm creating a WPF program for editing PowerPoint presentation using Syncfusion library but whenever I run the program I see the memory increase when navigating from a page to another. I'm using WPF Application with a window that navigates between multiple pages.

How to encounter this problem actually?

Thank you.

These photos show where the leak is located :

泄漏1

泄漏2

泄漏3

PS: Please focus on the InitializeNonUITasks method in home.cs because that's where the leak is.

Home Page XAML:

<Page x:Class="ProjectSABX.Pages.Home"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  xmlns:local="clr-namespace:ProjectSABX.Pages"
  xmlns:syncfusion="clr-namespace:Syncfusion.Windows;assembly=Syncfusion.Shared.Wpf"
  xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
  mc:Ignorable="d"
  Width="1175" Height="660" KeyDown="Window_KeyDown"
  Title="Home">
<Grid ShowGridLines="False" Margin="2,-2,-2,2">
    <Grid Grid.Row="0" ShowGridLines="False">
        <Grid.Background>
            <SolidColorBrush Color="White" Opacity="0.5" />
        </Grid.Background>
        <Grid.RowDefinitions>
            <RowDefinition Height="2.5*"></RowDefinition>
        </Grid.RowDefinitions>
        <Rectangle Height="52" Grid.Row="0"  VerticalAlignment="Top" Fill="#FF80A20D"></Rectangle>
        <TextBlock Name="titleBarTextBlock" Text="Aperçu" Height="40" Foreground="White" FontFamily="Segoe UI" FontWeight="Normal" Padding="15" FontSize="17.75"  VerticalAlignment="Top" Grid.Row="0"  HorizontalAlignment="Center"></TextBlock>
        <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0 15 10 0" Grid.Column="3" Grid.Row="0">
        </StackPanel>
        <Image Source="/Images/ViewerIcons/ppt__logo for header.png" Grid.Column="0" HorizontalAlignment="Left" Margin="15" VerticalAlignment="Top" Height="30"></Image>
    </Grid>

    <Grid Grid.Row="1" ShowGridLines="False">
        <Grid.RowDefinitions>
            <RowDefinition Height="0*" />
            <RowDefinition Height="445*" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="59*"></RowDefinition>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="2*" />
            <ColumnDefinition Width="8*" />
        </Grid.ColumnDefinitions>
        <Border ClipToBounds="True" BorderBrush="Black" BorderThickness=".7" Grid.Row="1" Grid.Column="0" Grid.RowSpan ="3" Margin="0,51,3,0">
            <ScrollViewer Name="scrollViewer" Focusable="False" CanContentScroll="False" BorderBrush="Black" BorderThickness="1" HorizontalScrollBarVisibility="Auto" Grid.Row="1" Grid.Column="0" Grid.RowSpan ="3">
                <Grid ShowGridLines="False">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="1*" />
                        <ColumnDefinition Width="9*" />
                    </Grid.ColumnDefinitions>
                    <StackPanel Name="stackpanel" Grid.Column="1" Width="170" />
                </Grid>
            </ScrollViewer>
        </Border>

        <Image Name ="centerSlideImage" Grid.Row="1" Grid.Column="1" Margin="0,57,5,19" />

        <Canvas Name="loadingIndicatorCanvas" Visibility="Collapsed" HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Row="1" Grid.Column="1" Margin="-250,-100,0,0">
            <Canvas Canvas.Left="21.75" Canvas.Top="14" Height="81.302" Width="80.197">
                <Canvas.Resources>
                    <Style TargetType="Ellipse">
                        <Setter Property="Width" Value="5" />
                        <Setter Property="Height" Value="5" />
                        <Setter Property="Fill" Value="#FF9BD226" />
                    </Style>
                </Canvas.Resources>

                <Ellipse x:Name="_00" Canvas.Left="24.75" Canvas.Top="50" />
                <Ellipse x:Name="_01" Canvas.Top="36" Canvas.Left="29.5" />
                <Ellipse x:Name="_02" Canvas.Left="43.5" Canvas.Top="29.75" />
                <Ellipse x:Name="_03" Canvas.Left="57.75" Canvas.Top="35.75" />
                <Ellipse x:Name="_04" Canvas.Left="63.5" Canvas.Top="49.75" />
                <Ellipse x:Name="_05" Canvas.Left="57.75" Canvas.Top="63.5" />
                <Ellipse x:Name="_06" Canvas.Left="43.75" Canvas.Top="68.75" />
                <Ellipse x:Name="_07" Canvas.Top="63.25" Canvas.Left="30" />
                <Ellipse Stroke="{x:Null}" Width="39.5" Height="39.5" Canvas.Left="31.75" Canvas.Top="37" Fill="{x:Null}" />
            </Canvas>
        </Canvas>

        <Grid Grid.Row="3" Grid.Column="1" ShowGridLines="False" Margin="0, -10">
            <Grid.RowDefinitions>
                <RowDefinition Height="0.5*" />
                <RowDefinition Height="20*" />
                <RowDefinition Height="10*" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="42*" />
                <ColumnDefinition Width="45*" />
                <ColumnDefinition Width="44*" />
                <ColumnDefinition Width="45*" />
                <ColumnDefinition Width="238*" />
                <ColumnDefinition Width="30*" />
                <ColumnDefinition Width="40*" />
                <ColumnDefinition Width="46*" />
                <ColumnDefinition Width="30*" />
                <ColumnDefinition Width="45*" />
            </Grid.ColumnDefinitions>
            <Image Name="openDocument" Grid.Row="1" Grid.Column="1" Height="30" Width="30" Source="/Images/ViewerIcons/ppt__open.png" MouseDown="openDocument_MouseDown" MouseEnter="openDocument_MouseEnter" MouseLeave="openDocument_MouseLeave" Margin="0,5,7,4"></Image>
            <Image Name="createPdfDocument" Grid.Row="1" Grid.Column="3" Height="30" Width="30" Source="/Images/ViewerIcons/ppt_pdf.png" MouseDown="createPdfDocument_MouseDown" MouseEnter="createPdfDocument_MouseEnter" MouseLeave="createPdfDocument_MouseLeave" Margin="0,5,8,4"></Image>
            <DockPanel Grid.Row="1" Grid.Column="6" Grid.ColumnSpan="3">
                <Image Name="prevDocument"  Height="30" Width="30" Source="/Images/ViewerIcons/ppt__back.png" MouseDown="prevDocument_MouseDown" MouseEnter="prevDocument_MouseEnter" MouseLeave="prevDocument_MouseLeave"></Image>
                <Label Name="lblPageDisplay" Height="30" MinWidth="30" FontFamily="Segoe UI" FontWeight="Bold" FontSize="18" VerticalAlignment="Center" VerticalContentAlignment="Center" Padding="35,0,0,0" Foreground="#646464" />
                <Image Name="NextDocumentIconImage" Height="30" Width="30" Source="/Images/ViewerIcons/ppt__for.png"  MouseDown="NextDocument_MouseDown" MouseEnter="NextDocument_MouseEnter" MouseLeave="NextDocument_MouseLeave" Margin="-10,0"></Image>
            </DockPanel>
        </Grid>
    </Grid>
</Grid>

Home Page C#:

    public Home()
    {
        InitializeComponent();

        pdfBackroundWorker.DoWork += pdfBackroundWorker_DoWork;
        pdfBackroundWorker.RunWorkerCompleted += pdfBackroundWorker_RunWorkerCompleted;
        this.Background = new SolidColorBrush(Color.FromRgb(244, 244, 244));
        filePath = "/Data/Template.pptx";
        
        InitializeNonUITasks();
        InitializeUITasks(filePath);
        

    }

        
    private void openDocument_MouseDown(object sender, MouseButtonEventArgs e)
    {
        Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
        dlg.Filter = "PowerPoint Presentations|*.pptx";
        Nullable<bool> result = dlg.ShowDialog();
        if (result == true)
        {
            string filename = dlg.FileName;
            filePath = filename;
            slideNumber = 1;
            displayBackroundWorker.RunWorkerAsync();
        }
    }
    private void InitializeNonUITasks()
    {
        try
        {
            presentation = Presentation.Open(filePath);
        }
        catch (Exception exp)
        {
            MessageBox.Show("This PowerPoint Presentation cannot be opened properly, please contact support");
            return;
        }
        slideImageSources.Clear();
        thumbnailImageSource.Clear();
        printImages.Clear();
        currentSlideNumber = 0;
        presentation.ChartToImageConverter = new ChartToImageConverter();
        presentation.ChartToImageConverter.ScalingMode = Syncfusion.OfficeChart.ScalingMode.Best;
        try
        {
            foreach (ISlide slide in presentation.Slides)
            {
                using (System.Drawing.Image image = slide.ConvertToImage(Syncfusion.Drawing.ImageType.Bitmap))
                {
                    System.Drawing.Image.GetThumbnailImageAbort myCallback = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
                    printImages.Add(image.Clone() as System.Drawing.Image);
                    System.Drawing.Image newImage = image.GetThumbnailImage(170, 100, myCallback, System.IntPtr.Zero);
                    using (Stream ms = new MemoryStream())
                    {
                        newImage.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                        var decoder = BitmapDecoder.Create(ms, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
                        thumbnailImageSource.Add(decoder.Frames[0]);
                    }

                    using (Stream ms = new MemoryStream())
                    {
                        image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                        var decoder = BitmapDecoder.Create(ms, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
                        slideImageSources.Add(decoder.Frames[0]);
                    }
                }
            }
        }
        catch (Exception)
        {
            MessageBox.Show("This PowerPoint Presentation cannot be converted to images properly, please contact support");
            return;
        }
    }

MainWindow XAML:

<Frame Grid.Column="1" Grid.Row="1" NavigationUIVisibility="Hidden" x:Name="Main" Source="./Pages/Home.xaml">
        <Frame.Background>
            <SolidColorBrush Color="#FFB0B0B0" Opacity="0.49" />
        </Frame.Background>
    </Frame>

MainWindow.cs:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        DataContext = this;
        InitializeComponent();
    }
    private void ButtonClickHomePage(object sender, RoutedEventArgs e)
    {
        Main.Content = new Home();
    }
 }

Edit1: I tried to use the dispose() method for each object that implemented IDisposable, When navigating at first, the memory process decreases. then when I navigate from page to page it increases again.

泄漏4

泄漏5

泄漏6

泄漏7

It is hard to pinpoint the issue like this, but I do have some suggestions:

Usually it's about calling Dispose on all objects that implement the IDisposable interface.

  • check all object for the IDisposable interface and call Dispose() if it's available. Eg: newImage.Dispose() (or use using )
  • Also, all the disposable objects in the lists printImages , thumbnailImageSource etc, should be disposed as well.
  • Also, I believe the decoder should be disposed as well, as well as the presentation , although I can be mistaken.

This will give you a good starting point - there might be more, but you should rule out and fix this first.

The loop is a likely suspect to be causing the memory increase.

Here's more info on IDisposable . Also, if you have implemented the IDisposable interface yourself - it's prety common to make a mistake with that as well. In that case you should double check it.

We are unable to reproduce the mentioned issue at our end using the given code example. For your reference, we have attached the sample which we have tried at our end to reproduce the mentioned issue.

Sample Link https://www.syncfusion.com/downloads/support/directtrac/general/ze/PresentationSample-1588894092

In this sample, the maximum memory usage is about less than 500MB. So, kindly provide us a modified sample of the above to reproduce the mentioned issue, so that we can proceed further to analyze and update you with the appropriate details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM