Page Index Toggle Pages: 1 Send TopicPrint
Normal Topic SVG / PDF Exporters memory usage (Read 573 times)
MikeB
YaBB Newbies
*
Offline


I Love MindFusion!

Posts: 10
Joined: Apr 24th, 2024
SVG / PDF Exporters memory usage
May 7th, 2024 at 12:41pm
Print Post  
I have do more tests with svg exporter (the same problem is for pdf exporter) trying to export more complicated diagrams and i have observed an memory ussage issue.
With each call diagram_DoubleClicked the memory usage increases and after the finished export operation memory is not being freed.

Code (C++)
Select All
    public partial class MainWindow : Window
    {
        public List<Diagram> diagrams { get; set; }

        public MainWindow()
        {
            InitializeComponent();

            var rand = new Random();
            diagrams = new List<Diagram>();

            for (int j = 0; j < 40; j++)
            {
                var diag = new Diagram();

                for (int i = 0; i < 100; i++)
                {
                    var shapeNode = new ShapeNode()
                    {
                        Shape = new Shape(new ElementTemplate[]
                        {
                            new LineTemplate(0, 0, 100, 0),
                            new LineTemplate(100, 0, 100, 100),
                            new LineTemplate(100, 100, 0, 100),
                        }, FillRule.EvenOdd),
                    };

                    shapeNode.AddLabel(new NodeLabel() { Text = "Test" });

                    diag.Nodes.Add(shapeNode);
                    diag.Nodes.Last().Move(rand.Next(0, 1000), rand.Next(0, 1000));
                }

                diagrams.Add(diag);
            }
        }

        private void diagram_DoubleClicked(object sender, DiagramEventArgs e)
        {
            var exporter = new PdfExporter();

            foreach (var diag in diagrams)
                exporter.Export(diag, @"D:\temp\diagram.svg");
        }
    }
 

  

memory_usage.jpg ( 14 KB | 14 Downloads )
memory_usage.jpg
Back to top
 
IP Logged
 
Slavcho
YaBB Moderator
*****
Offline


tech.support

Posts: 3228
Joined: Oct 19th, 2005
Re: SVG / PDF Exporters memory usage
Reply #1 - May 8th, 2024 at 11:39am
Print Post  
This build fixes exporter leaks -

https://mindfusion.eu/_temp/wpfdiag_expmem.zip

First export still allocates a lot of memory because ShapeNodes cache their geometries when first drawn. Subsequent exports should stay stable.

Regards,
Slavcho
Mindfusion
  

Untitled_029.png ( 18 KB | 16 Downloads )
Untitled_029.png
Back to top
 
IP Logged
 
MikeB
YaBB Newbies
*
Offline


I Love MindFusion!

Posts: 10
Joined: Apr 24th, 2024
Re: SVG / PDF Exporters memory usage
Reply #2 - May 8th, 2024 at 11:56am
Print Post  
It's better.... but not perfect.
Why memory allocated to ShapeNodes cache is not being freed after saving the output file ?
  
Back to top
 
IP Logged
 
Slavcho
YaBB Moderator
*****
Offline


tech.support

Posts: 3228
Joined: Oct 19th, 2005
Re: SVG / PDF Exporters memory usage
Reply #3 - May 8th, 2024 at 12:15pm
Print Post  
That cache is not specific to PDF exporter and should be reused both for on-screen rendering and different exports. After second export you can only see some WeakReferences added between heap snapshots, which .NET should free when it needs the memory. So that's as close to perfect as possible according to our developer Smiley
  

Untitled_030.png ( 82 KB | 14 Downloads )
Untitled_030.png
Back to top
 
IP Logged
 
Slavcho
YaBB Moderator
*****
Offline


tech.support

Posts: 3228
Joined: Oct 19th, 2005
Re: SVG / PDF Exporters memory usage
Reply #4 - May 8th, 2024 at 12:17pm
Print Post  
If you really need to free that memory after full rendering of offscreen diagrams, you could try exporting their copies instead, or after export clear each diagram and reload from a file or your data. Otherwise you'll have the window -> diagrams array -> nodes -> cached geometries references from root preventing GC from reclaiming the added cache.
  
Back to top
 
IP Logged
 
MikeB
YaBB Newbies
*
Offline


I Love MindFusion!

Posts: 10
Joined: Apr 24th, 2024
Re: SVG / PDF Exporters memory usage
Reply #5 - May 8th, 2024 at 6:24pm
Print Post  
Slavcho wrote on May 8th, 2024 at 12:15pm:
That cache is not specific to PDF exporter and should be reused both for on-screen rendering and different exports. After second export you can only see some WeakReferences added between heap snapshots, which .NET should free when it needs the memory. So that's as close to perfect as possible according to our developer Smiley


Look at my example..... i don't need the List<Diagram> on screen rendering. I want to export the list to PDF. The exporter should allocate memory needed to render diagrams, produce output file and free the cache and memory that needed to do his work.
« Last Edit: May 9th, 2024 at 6:00am by MikeB »  
Back to top
 
IP Logged
 
Slavcho
YaBB Moderator
*****
Offline


tech.support

Posts: 3228
Joined: Oct 19th, 2005
Re: SVG / PDF Exporters memory usage
Reply #6 - May 9th, 2024 at 7:58am
Print Post  
Maybe it should, but we've been developing this as UI control that has one or two on-screen instances, that users might occasionally export to PDF. Most of the WeakReferences that grow the heap are WPF internals anyway from what we can tell, and .NET will recycle them when memory needed.

If you are looking to create a PDF generation service for large number of off-screen diagrams with lower memory footprint, you could try the diagramming.dll from our ASP.NET and WinForms controls (it's shared between them); this same test never takes more than 50MB with it -

Code
Select All
public partial class Form1 : Form
{
	public Form1()
	{
		InitializeComponent();

		var rand = new Random();
		diagrams = new List<Diagram>();

		for (int j = 0; j < 40; j++)
		{
			var diag = new Diagram();

			for (int i = 0; i < 100; i++)
			{
				var shapeNode = new ShapeNode()
				{
					Shape = new Shape(new ElementTemplate[]
					{
						new LineTemplate(0, 0, 100, 0),
						new LineTemplate(100, 0, 100, 100),
						new LineTemplate(100, 100, 0, 100),
					}, System.Drawing.Drawing2D.FillMode.Alternate),
				};

				shapeNode.AddLabel(new NodeLabel() { Text = "Test" });

				diag.Nodes.Add(shapeNode);
				diag.Nodes.Last().Move(rand.Next(0, 1000), rand.Next(0, 1000));
			}

			diag.ResizeToFitItems(5);
			diagrams.Add(diag);
		}
	}

	public List<Diagram> diagrams { get; set; }

	private void button1_Click(object sender, EventArgs e)
	{
		var exporter = new PdfExporter();

		foreach (var diag in diagrams)
			exporter.Export(diag, "diagram.pdf");
	}
} 



https://www.nuget.org/packages/MindFusion.Diagramming/

Regards,
Slavcho
Mindfusion
« Last Edit: May 9th, 2024 at 10:33am by Slavcho »  
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint