PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Mar 28, 2024 10:55 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: Sun Apr 07, 2013 1:41 am 
Offline

Joined: Sat Mar 23, 2013 11:48 pm
Posts: 11
I was doing some profiling of my web app that uses PDFsharp w/ GDI+ and found that StringFormat objects were being created at a very high frequency. In my case, 3 megabytes worth of such objects in just a few seconds, swamping all other objects by an order of magnitude. They weren't leaking, but they did incur a high GC cost.

Digging into the PDFsharp code it seems that any call to DrawString or MeasureString that does not supply an XStringFormat results in a call to one of the XStringFormats accessors, which creates a new XStringFormat and backing StringFormat object. Further, internally many methods call the short forms of MeasureString (e.g. XTextFormatter.Font), leading to many allocations even when the caller is supplying an XStringFormat to all calls that accept them.

To mitigate this, I changed the XStringFormats members to initialize static readonly properties instead of having the accessors create new instances each time. These XStringFormat objects could be mutated so this wouldn't be a general purpose fix, but there's probably some approach that would eliminate all of these allocations.


Top
 Profile  
Reply with quote  
PostPosted: Mon Apr 08, 2013 9:03 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3095
Location: Cologne, Germany
Thank you for the feedback - I think this part of PDFsharp should be reviewed to take advantage of the latest C# features.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 57 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group