Repository navigation
Closed
Description
Description
If create SKTypeface from file name or from stream, memory is not released. In the code example, there are commented lines, uncomment them to test creating typeface from family name (no problems with this one), from file name and memory stream. Attached screenshots shows that in the last two cases memory usage raise constantly.
Code
for (int i = 0; i < 1000; i++)
{
using (SkiaSharp.SKBitmap bmp = new SkiaSharp.SKBitmap(100, 100))
using (SkiaSharp.SKCanvas canvas = new SkiaSharp.SKCanvas(bmp))
using (SkiaSharp.SKPaint paint = new SkiaSharp.SKPaint())
//using (SkiaSharp.SKTypeface typeface = SkiaSharp.SKTypeface.FromFamilyName("Calibri")) // If create Typeface from family there is no memory leak.
using (SkiaSharp.SKTypeface typeface = SkiaSharp.SKTypeface.FromFile("C:\\WINDOWS\\Fonts\\calibri.ttf")) // If create Typeface from file name
//using (MemoryStream ms = new MemoryStream(File.ReadAllBytes("C:\\WINDOWS\\Fonts\\calibri.ttf"))) // Or stream, there is memory leak.
//using (SkiaSharp.SKTypeface typeface = SkiaSharp.SKTypeface.FromStream(ms))
{
paint.Typeface = typeface;
paint.TextSize = 12;
canvas.DrawText("Cool Text", new SkiaSharp.SKPoint(10, 10), paint);
canvas.DrawText("Cool Text", new SkiaSharp.SKPoint(10, 20), paint);
canvas.DrawText("Cool Text", new SkiaSharp.SKPoint(10, 30), paint);
canvas.DrawText("Cool Text", new SkiaSharp.SKPoint(10, 40), paint);
canvas.DrawText("Cool Text", new SkiaSharp.SKPoint(10, 50), paint);
canvas.DrawText("Cool Text", new SkiaSharp.SKPoint(10, 60), paint);
canvas.DrawText("Cool Text", new SkiaSharp.SKPoint(10, 70), paint);
canvas.DrawText("Cool Text", new SkiaSharp.SKPoint(10, 80), paint);
using (SkiaSharp.SKFileWStream fs = new SkiaSharp.SKFileWStream(@"C:\Temp\out.png"))
{
SkiaSharp.SKPixmap.Encode(fs, bmp, SkiaSharp.SKEncodedImageFormat.Png, 100);
}
}
GC.Collect();
}
Basic Information
- Version with issue: Tested and reproduced on the latest stable 1.68 and latest RC 1.68.1-rc.153
- IDE: Visual Studio
- Platform Target Frameworks: .NET Core 2.2
Screenshots
Memory usage when create SKTypeface from family name

Memory usage when create SkTypeface from file name

Memory usage when create SkTypeface from memory stream

Metadata
Metadata
Assignees
Labels
No labels
Activity
mattleibow commentedon Nov 5, 2019
I had a look and it appears to be some leak inside the native library when a stream is used. Even in the case of the filename, there is an underlying file stream that is opened.
I'll need to investigate further to see why it is not being released.
As a workaround, you can cache the typeface. This will use more memory than desired, but only once.
Just make sure no not dispose the typefaces as you will then get a disposed item from the dictionary.
AlexNosk commentedon Nov 6, 2019
Thank you for the proposed workaround, I changed the code to use overload that creates SkTypeface from family name. But still the fix is required, because sometimes it is needed to create typeface from stream or file.
mattleibow commentedon Dec 22, 2019
@AlexNosk Since the release of 1.68.1, I believe there were several major fixes for memory leaks. I am closing this issue as it appears to be fixed. If you fine that this is not the case, feel free to reopen this issue.