Potential memory leak in 2014.2.1924 (WPF)

Stimulsoft Reports.WPF discussion
Post Reply
sandynith
Posts: 64
Joined: Thu Dec 12, 2013 4:03 am

Potential memory leak in 2014.2.1924 (WPF)

Post by sandynith »

Hi,

I am trying the latest prerelease build 2014.2.1924 for rendering the charts (e.g. StiLineSeries) which has improved performance for the charts.
However I noticed that if you run multiple big reports few time then you run out of memory quickly and this problem was NOT there with 2014.1 full release build.

After debugging, I noticed that after disposing off the report the memory is not released by Stimulsoft components and looks like the KeyEventHandler is still referenced.
I used red gate ANTS memory profiler to detect this and attaching the retention graph showing the same.

Using reflector I drilled down the assemblies and looks like in StiWpfViewerControl class in the ControlLoaded method you guys register for KeyEventHandler and but no when is un-registering it, which is causing this problem and the chain in retention graph is not breaking and hence GC is not able to claim memory.

Code: Select all

             //The StiWpfViewerControl class
               void IComponentConnector.Connect(int connectionId, object target)
		{
			switch (connectionId)
			{
			case 1:
				this.viewerControl = (StiWpfViewerControl)target;
				this.viewerControl.SizeChanged += new SizeChangedEventHandler(this.UserControl_SizeChanged);
				this.viewerControl.PreviewDrop += new System.Windows.DragEventHandler(this.Viewer_PreviewDrop);
				this.viewerControl.Loaded += new RoutedEventHandler(this.ControlLoaded);
				return;
                     .........
                     .........

               //Registering for the KeyEventHandler but couldn't find anyone un-registering it.
		private void ControlLoaded(object sender, RoutedEventArgs e)
		{
			Window parentWindow = this.GetParentWindow(this);
			parentWindow.KeyDown += new System.Windows.Input.KeyEventHandler(this.WindowOnKeyDown);
		}

Could you please look into this problem and advice?
Providing a sample project showing problem will be time consuming for me.

Thanks,
Sandeep
Attachments
Retention Graph.JPG
Retention Graph.JPG (80.81 KiB) Viewed 5861 times
Last edited by sandynith on Fri Oct 10, 2014 2:17 am, edited 2 times in total.
ReubenB
Posts: 1
Joined: Thu Oct 09, 2014 3:23 am

Re: Potential memory leak

Post by ReubenB »

Hey,

I've noticed this memory leak too, I am certain it is caused by the WindowOnKeyDown event handler.
I tried removing the event handler on the StiWpfViewerControl unloaded event using reflection and it fixes the memory leak.
Like so,

Code: Select all

        private void unloadEventHandler(object o, RoutedEventArgs a)
        {
            MethodInfo mi = typeof(StiWpfViewerControl).GetMethod("WindowOnKeyDown", BindingFlags.NonPublic | BindingFlags.Instance);
            Delegate d = mi.CreateDelegate(typeof(KeyEventHandler), m_reportPreview);
            m_parentWindow.RemoveHandler(Keyboard.KeyDownEvent, d);

            m_reportPreview.Unloaded -= unloadEventHandler;
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            m_reportPreview = this.GetPartFromTemplate<StiWpfViewerControl>("ReportPreview");
            m_reportPreview.Unloaded += unloadEventHandler;
            ...
        }
But that's a terribly dirty hack. The StiWpfViewerControl should be removing the event handler itself at some point.

Cheers,
Reuben.
HighAley
Posts: 8430
Joined: Wed Jun 08, 2011 7:40 am
Location: Stimulsoft Office

Re: Potential memory leak

Post by HighAley »

Hello.

We have made the Dispose() method of StiWpfViewerControl public. You could use it to solve memory leak problem.
This method will be available in our next major release.

Thank you.
sandynith
Posts: 64
Joined: Thu Dec 12, 2013 4:03 am

Re: Potential memory leak

Post by sandynith »

Hi Aleksey,
Ideally you should unhook the event handler at some point, so it could be the Dispose method as well.
So, do you mean that you will unhook the event in the Dispose method and we should call the Dispose method?

Thanks,
Sandeep
sandynith
Posts: 64
Joined: Thu Dec 12, 2013 4:03 am

Re: Potential memory leak in 2014.2.1924 (WPF)

Post by sandynith »

Hi Aleksey,
Today I updated to WPF 2014.2 full release and can verify that calling of Dispose method of StiWpfViewerControl does unhook the event and now that part of memory is getting freed. Its quite a relief. Thanks for that :-)

However I noticed that even after disposing off memory still there are Sti DataRows and DataTable hanging around. We do the following to clear the Sti components:
+ call Dispose of StiWpfViewerControl
+ report.DataStore.Clear(); //report is StiReport
report.Dictionary.DataStore.Clear();
report.Dispose();

Even after doing this the data rows are still present and being referenced by the StiChartInteractionHelper.cachedChart and cachedChartGeom as shown in the attached retention graph.
Because of this huge amount of memory couldn't be freed even after disposing off the report and hence we quickly run out of memory if report is run multiple times.

I am not sure if there is any way to not use the cached chart, or what are the benefits of using it etc. (Interaction or DrillDown is not enabled on my report Charts)
I noticed that there is StiChartInteractionHelper.ResetChartGeomCache() public function - but not sure if its safe to call it.

Could you please advice here?

Thanks,
Sandeep
Attachments
StiChartInteractionHelper.cachedChart not letting the DataRows to get free.
StiChartInteractionHelper.cachedChart not letting the DataRows to get free.
Retention Graph 2014.chartCache.png (75.21 KiB) Viewed 5796 times
HighAley
Posts: 8430
Joined: Wed Jun 08, 2011 7:40 am
Location: Stimulsoft Office

Re: Potential memory leak in 2014.2.1924 (WPF)

Post by HighAley »

Hello.

Thank you for your huge help to improve our product.
We have made some changes following your advices.
Please, check our next prerelease build.

Thank you.
sandynith
Posts: 64
Joined: Thu Dec 12, 2013 4:03 am

Re: Potential memory leak in 2014.2.1924 (WPF)

Post by sandynith »

Hi Aleksey,

Thanks for your efforts and addressing the issues. I will check the next pre-release build to see how it goes.
Cheers,
Sandeep
HighAley
Posts: 8430
Joined: Wed Jun 08, 2011 7:40 am
Location: Stimulsoft Office

Re: Potential memory leak in 2014.2.1924 (WPF)

Post by HighAley »

Hello.

Let us know if you need any additional help.

Thank you.
sandynith
Posts: 64
Joined: Thu Dec 12, 2013 4:03 am

Re: Potential memory leak in 2014.2.1924 (WPF)

Post by sandynith »

Hi Aleksey,

I checked our reports using latest pre-release build WPF 2014.2.2001 and can confirm that now we don't see the memory leaks as reported in this post.
Now disposing off a report properly release memory and it looks pretty good. Thanks for addressing this issue.

Cheers,
Sandeep
Andrew
Posts: 4108
Joined: Fri Jun 09, 2006 3:58 am

Re: Potential memory leak in 2014.2.1924 (WPF)

Post by Andrew »

Hello,

Thank you for letting us know about this!
Post Reply