Open Ramaraj-Marimuthu opened 1 year ago
Could you try to create the typeface outside the loop? You will most likely not see any increase in memory usage. Skia is caching glyphs data and is only clearing the cache if it reaches some defined memory usage level.
@Gillibald, Thanks for the update.
Yes, as you mentioned memory leak doesn’t occur if we create the typeface outside the loop.
But for our requirement, we need to create a typeface for every looping. So, is there any possibility to forcibly dispose the memory by using any API’s?
@Gillibald & @mattleibow - Any solution for this?
It would be very helpful for us.
But for our requirement, we need to create a typeface for every looping
Why do you need it though?
@FoggyFinder - Please find the below requirement details,
We are working on the Word document to PDF conversion project, and we are using the SkiaSharp library to measure the width and height of the text by using an embedded font stream in the document.
While converting multiple (more than 100) Word documents to PDF in for loop, memory keeps on growing until the loop ends due to creating more number of SKTypeface for each Word document. Since each Word document may have a different set of embedded font streams, it’s not possible to create SKTypeface commonly (outside of the loop).
To avoid this memory leak problem, we expected to dispose the SKTypeface allocated memory after completing each conversion. Could you please help us to solve this memory problem?
Note: At certain stage, app crashed with out of memory problem.
@FoggyFinder & @Gillibald & @mattleibow - Any help would be much more appreciated.
SkTypefaceCache
is not exposed in skia sharp, but the purge can be called via SkGraphics:
SKGraphics.PurgeFontCache();
You can also set your own limits in the cache via
SKGraphics.SetFontCacheCountLimit
SKGraphics.SetFontCacheLimit
@themcoo - Huge thanks for your solution. It's solved the problem in attached simple app.
Will try the same in our project and will update further details.
Thanks again @themcoo
I had same memory leak issue when using SKTypeface.FromFile
. Using SKGraphics.PurgeFontCache();
didn't solve my issue.
At the end I solved memory leak by encapsulating and caching output of SKTypeface.FromFile
in a static class.
public static SKTypeface? GetTypefaceFromFile(string filename)
{
return typefaceCache.GetOrAdd(filename, filename => SKTypeface.FromFile(filename) ?? null);
}
Description
Memory keep on increasing while measuring the text using font stream
Code
Note: You can use any available TTF font to replicate this problem.
Expected Behavior
Allocated memory should be disposed properly
Actual Behavior
Memory allocation increasing rapidly without any disposal
Basic Information
Screenshots
Measuring the text using font stream: (Memory keep on increasing here)
Measuring the text using installed font: (Memory disposed properly here and it's maintained in same level)
Reproduction Link
Please use the attached sample to replicate the same problem, SkiaSharpTesting.zip