UILabel bigger font size using a lot of memory

My Keyboard Extension has high memory usage for having UILabels with large font size. The keyboard has emojis in a single view with UICollectionView. I noticed that the bigger font size I use, the more memory usage I have. I set font size approximately to 25 and when scrolling through emojis, the memory usage goes up to 250mb. If I reduce the font size to 10, the memory usage goes down to 70mb when scrolled. The keyboard extension has 75mb memory usage. I've had my keyboard app uploaded for years and had no issues with memory usage but with recent OS update, my keyboard started crashing a lot due to memory usage and it's due to UILabel. Would it be possible to increase the memory limit or exception on the memory usage caused by the OS for large fonts stored as bitmap?

Replies

You note you have labels in a collection view – are you allowing your collection view cells to be reused?

Since UILabels do their work by drawing, if you increase the font size this necessitates a larger buffer per label. If your labels don't get reused (because the cells they are in don't get reused) then you will need a lot more memory.

Realistically even the 70MB of memory usage you see at the "smaller" font size is indicative of an issue preventing cell reuse. Your memory usage should be far below that if you are getting cell reuse.

I did reuse cells by registering and dequeuing. Here is a short example code that reproduces the issue. When scrolling through, memory usage climbs up to ~230mb. When font size is changed, it also changes the max memory usage. Memory usage also depends on how many cells are visible at a time but controlling that value, font size is directly correlated with memory usage.

Keyboard extension has 70mb so using the same font size as system keyboard won't work for keyboard extension since memory usage goes up to 230mb.

My keyboard app has been released for over 6 years and was fine but with recent iOS update, something changed and caused the increase in memory usage for the UILabels.