apptekstudios / ASCollectionView

A SwiftUI collection view with support for custom layouts, preloading, and more.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Memory Leak when using ASCollectionView

mwilsoncricut opened this issue · comments

It seems as though the ASCollectionView code has a memory leak. I noticed that anytime I display a ASCollectionView in my app the memory jumps and doesn't come back down when the view disappears. You can see the same behavior in the demo project. The issue occurs on the 1.2.2 release as well as on the current commit on master.

To Reproduce
Remove the caching functionality from the Demo project, you'll see that each time you view the waterfall layout or scroll through the images the memory jumps for each cell displayed and it never comes back down even after leaving the view.

Expected behaviour
When the view is removed from the view hierarchy you should be able to see a drop in memory usage.

Xcode Version:

  • 11.3

Simulator, Device, Both?
Device

Hmm thanks for pointing this out. We cache some SwiftUI hosting controllers in order to maintain @State variables in cells, however this is having quite a memory impact. I'll look at changing this behaviour in the next release.

Same problem here when using ObservedObject / Published as datasource.

I have addressed a number of leaks in yesterday’s release (v1.3). Please let me know if you come across any more once updating :)

Well, it looks like it's ok now, although I had changed my code a lot before the 1.3.0 release.

But on 1.3.0 we have a new issue. I think there is some problem on reusing cells, because every time I scroll a ASTableView the disappearing cells are rebuilt and the ASCollectionView in each cell are reloaded. So if I have a ASTableView as parent and I scroll a ASCollectionViewto right in first cell, then scroll ASTableView down, when I come back to first cell the ASCollectionView were rebuilt and lost the scroll position.

I had to rollback to 1.2.2.

@alexandremorgado that is a good point about nested collection views! I think the right way to approach it may be to allow flagging sections that should be ‘cached’ so that they are not re-created on scrolling away. I’ll look into this when I get a chance :)

@alexandremorgado I have implemented what I discussed above and have released as version 1.3.1 🎉

I hope this means you can use the latest version :) Marking a section as one that should be cached is as simple as the following:

ASSection(...) {
...
}
.cacheCells()

It works @apptekstudios! Thank you!

BTW, what a amazing library is ASCollectionView! It allow us to combine the best of the two worlds. Great work!