I am developing keyboard extension for iPhone. There is an emoji screen smilar to Apples own emoji keyboard that shows some 800 emoji characters in UICollectionView
.
When this emoji UIScrollView
is scrolled the memory usage increases and does not drop down. I am reusing cells correctly and when testing with single emoji character displayed 800 times the memory does not increase during scrolling.
Using instruments I found that there is no memory leak in my code but it seems that the emoji glyphs are cached and can take around 10-30MB of memory depending on font size (reseach shows they are actually PNGs). Keyboard extensions can use little memory before they are killed. Is there a way to clear that font cache?
Edit
Adding code example to reproduce the problem:
let data = Array("๐๐โบ๏ธ๐๐๐๐๐๐๐๐๐๐ณ๐๐๐๐๐๐ฃ๐ข๐๐ญ๐ช๐ฅ๐ฐ๐
๐๐ฉ๐ซ๐จ๐ฑ๐ ๐ก๐ค๐๐๐๐ท๐๐ด๐ต๐ฒ๐๐ฆ๐ง๐๐ฟ๐ฎ๐ฌ๐๐๐ฏ๐ถ๐๐๐๐ฒ๐ณ๐ฎ๐ท๐๐ถ๐ฆ๐ง๐จ๐ฉ๐ด๐ต๐ฑ๐ผ๐ธ๐บ๐ธ๐ป๐ฝ๐ผ๐๐ฟ๐น๐พ๐น๐บ๐๐๐๐๐ฝ๐ฉ๐ฅโจ๐๐ซ๐ฅ๐ข๐ฆ๐ง๐ค๐จ๐๐๐๐
๐๐๐๐๐โโ๏ธ๐โ๐๐๐๐๐๐๐โ๏ธ๐๐ช๐ถ๐๐๐ซ๐ช๐ฌ๐ญ๐๐๐ฏ๐๐
๐๐๐๐๐
๐ฐ๐๐๐๐ถ๐บ๐ฑ๐ญ๐น๐ฐ๐ธ๐ฏ๐จ๐ป๐ท๐ฝ๐ฎ๐๐ต๐๐ด๐๐๐ผ๐ง๐ฆ๐ค๐ฅ๐ฃ๐๐๐ข๐๐๐๐๐๐๐๐ ๐๐ฌ๐ณ๐๐๐๐๐๐
๐๐๐๐๐๐๐๐๐๐ฒ๐ก๐๐ซ๐ช๐๐๐ฉ๐พ๐๐ธ๐ท๐๐น๐ป๐บ๐๐๐๐ฟ๐พ๐๐ต๐ด๐ฒ๐ณ๐ฐ๐ฑ๐ผ๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐ โญ๏ธโ๏ธโ
๏ธโ๏ธโก๏ธโ๏ธโ๏ธโ๏ธ๐๐๐๐โ๏ธ๐ต๐ถ๐ผ๐บ๐ป๐ธ๐น๐ท๐ด๐๐๐๐๐๐๐๐ค๐ฑ๐ฃ๐ฅ๐๐๐๐๐ฒ๐ข๐ก๐ณ๐๐ฉ๐ฎ๐ฆ๐จ๐ง๐๐ฐ๐ช๐ซ๐ฌ๐ญ๐ฏ๐๐๐๐๐๐๐๐๐๐๐๐๐๐ ๐๐
๐ฝ๐๐๐๐๐๐๐๐๐๐๐๐ป๐
๐๐๐๐๐๐๐๐ฎ๐๐๐๐โค๏ธ๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐๐ก๐ ๐ข๐๐๐๐๐ฝ๐๐๐๐ผ๐๐๐๐๐๐๐๐๐๐ฌ๐ญ๐ฐ๐จ๐ฌ๐ฉ๐ช๐ญ๐ค๐ง๐ผ๐ต๐ถ๐น๐ป๐บ๐ท๐ธ๐พ๐ฎ๐๐ด๐๏ธ๐ฒ๐ฏ๐๐โฝ๏ธโพ๏ธ๐พ๐ฑ๐๐ณโณ๏ธ๐ต๐ด๐๐๐๐ฟ๐๐๐๐ฃ").map {String($0)}
class CollectionViewTestController: UICollectionViewController {
override func viewDidLoad() {
collectionView?.registerClass(Cell.self, forCellWithReuseIdentifier: cellId)
}
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return data.count
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(cellId, forIndexPath: indexPath) as! Cell
if cell.label.superview == nil {
cell.label.frame = cell.contentView.bounds
cell.contentView.addSubview(cell.label)
cell.label.font = UIFont.systemFontOfSize(34)
}
cell.label.text = data[indexPath.item]
return cell
}
override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
}
class Cell: UICollectionViewCell {
private let label = UILabel()
}
After running and scrolling the UICollectionView
I get memory usage graph like this: