Paragraph memory leak?

60 views
Skip to first unread message

Bruno Piovan

unread,
Jan 28, 2020, 3:27:35 AM1/28/20
to skia-discuss
Hello,
I noticed that the memory usage of my application keeps increasing the more I use the Paragraph class.

I managed to created a piece of code that reproduces this:

using namespace std;
using namespace skia::textlayout;

int main()
{
if (!SkLoadICU()) return -1;

auto surface = SkSurface::MakeRasterN32Premul(1920, 1080);
auto canvas = surface->getCanvas();

for (size_t i = 0; i < 10000; i++)
{
canvas->clear(SK_ColorWHITE);

SkPaint paint;
paint.setAntiAlias(true);
paint.setColor(SK_ColorBLACK);

TextStyle textStyle;
textStyle.setForegroundColor(paint);
textStyle.setFontFamilies({ SkString("Roboto") });

auto fontCollection = sk_make_sp<FontCollection>();
fontCollection->setDefaultFontManager(SkFontMgr::RefDefault());

ParagraphStyle paragraphStyle;
paragraphStyle.setTextStyle(textStyle);

ParagraphBuilderImpl builder(paragraphStyle, fontCollection);
builder.addText("Text");

auto paragraph = builder.Build();
paragraph->layout(100);

paragraph->paint(canvas, 0, 0);

//used to add a delay so I can monitor memory usage
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}

return 0;
}

So, running the code above will show memory usage increasing and never decreases, so I suspect that there are memory not being released. If I don't call paragraph->layout(100) then the memory usage remains stable but of course it doesn't render the text. Does anybody know how to solve this? Do I need to release anything manually?

Thanks!
memory.png

Bruno Piovan

unread,
Jan 28, 2020, 3:41:46 AM1/28/20
to skia-discuss
I just noticed that if I move fontCollection to outside the for loop, then the memory usage remains the same, however if I add a new text on every loop, it doesn't matter and the memory usage keeps increasing...

Julia Lavrova

unread,
Jan 28, 2020, 9:23:00 AM1/28/20
to skia-d...@googlegroups.com
Yes, it's a memory leak in a form of cache.
You see, we keep all shaped results on fontCollection, so if you move it out of the loop it gets cached once and then you just use it.
FontCollection should clear the cache in destruction and it does not. It's a bug, thank you, I will fix it as soon as possible (today).
By the way, it's better for many reasons to use the same FontCollection rather than create a new one every time.

Julia

--
You received this message because you are subscribed to the Google Groups "skia-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to skia-discuss...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/skia-discuss/6dc5dca3-6c8b-486c-bad9-ce8cef36b83f%40googlegroups.com.

Bruno Piovan

unread,
Jan 28, 2020, 4:51:26 PM1/28/20
to skia-discuss
Hi Julia,
thank you very much!
To unsubscribe from this group and stop receiving emails from it, send an email to skia-d...@googlegroups.com.

Elliot Liu

unread,
Jul 8, 2022, 1:13:50 PMJul 8
to skia-discuss
I meet the same issue with the latest main branch code. which branch fixed this issue?  thanks.

Elliot Liu

unread,
Jul 8, 2022, 1:18:04 PMJul 8
to skia-discuss
the cached is in shared memory or temp file?
I noticed even I delete the whole class, the memory still not be released unless I exit the app.
Reply all
Reply to author
Forward
0 new messages