Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Memory leak occurs while measuring the text using font stream #2446

Open
Ramaraj-Marimuthu opened this issue Apr 19, 2023 · 9 comments

Comments

@Ramaraj-Marimuthu
Copy link

Ramaraj-Marimuthu commented Apr 19, 2023

Description

Memory keep on increasing while measuring the text using font stream

Code

for (int i = 0; true; i++)
{
    Console.WriteLine(i);
    // Memory leak occurs while measuring the text using font stream.
    MeasureTextUsingFontStream();
}

// Measure the text using font stream
static void MeasureTextUsingFontStream() {
    using (var stream = File.OpenRead(@"../../../arial.ttf")) {
        using (var typeface = SKTypeface.FromStream(stream)) {
            using (var paint = new SKPaint { Typeface = typeface }) {
                // Issue doesn't occur if we commented the below line.
                var width = paint.MeasureText("Hello, world!");
                paint.Dispose();
            }
            typeface.Dispose();
        }
        stream.Dispose();
    }    
    GC.Collect();
    GC.WaitForPendingFinalizers();
}

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

  • Version with issue: 2.88.3
  • Last known good version: None
  • IDE: Visual Studio
  • Platform Target Frameworks:
    • Windows Classic: Windows 10
**IDE:** Microsoft Visual Studio Professional 2022 (64-bit) - Preview Version 17.6.0 Preview 1.0

OS:
OS - Windows 10 Pro
Processor - Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz 2.71 GHz
Installed RAM - 16.0 GB (15.9 GB usable)
System type - 64-bit operating system, x64-based processor


PASTE ANY DETAILED VERSION INFO HERE

Screenshots

Measuring the text using font stream: (Memory keep on increasing here)

image

Measuring the text using installed font: (Memory disposed properly here and it's maintained in same level)

image

Reproduction Link

Please use the attached sample to replicate the same problem,
SkiaSharpTesting.zip

@Gillibald
Copy link
Contributor

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.

@Ramaraj-Marimuthu
Copy link
Author

Ramaraj-Marimuthu commented Apr 20, 2023

@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?

@Ramaraj-Marimuthu
Copy link
Author

@Gillibald & @mattleibow - Any solution for this?

It would be very helpful for us.

@FoggyFinder
Copy link
Contributor

But for our requirement, we need to create a typeface for every looping

Why do you need it though?

@Ramaraj-Marimuthu
Copy link
Author

@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.

@Ramaraj-Marimuthu
Copy link
Author

@FoggyFinder & @Gillibald & @mattleibow - Any help would be much more appreciated.

@themcoo
Copy link

themcoo commented Apr 28, 2023

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

@Ramaraj-Marimuthu
Copy link
Author

@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

@afruzan
Copy link

afruzan commented Sep 19, 2024

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);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants