marcorei / Infinite-Fire

Connects Firebase to a RecyclerView
Apache License 2.0
19 stars 7 forks source link

Consuming too much memory #8

Open macxtor opened 6 years ago

macxtor commented 6 years ago

Hello,

I've integrated Infinite-Fire and it works perfectly fine, I have recyclerview with multiple view types including audio,video,image,text but as my data grows over 100 , my app has started consuming lot of memory, is there any work around to this ? as to release the data in array that's not being used ?

marcorei commented 6 years ago

Thanks for letting me know!

The way this library currently works is by re-creating the query each time a new page is requested. While this works great for most cases, when scrolling down a few pages there are network and memory concerns.

I meant to optimize / rewrite this a while ago. I'll try to look into this in the near future!

macxtor commented 6 years ago

Thanks Markus,

your library is been of great help to me, but can you please guide me in right direction to sort this issue ?

Thank you.

On Sat, Jan 27, 2018 at 1:18 AM, Markus Riegel notifications@github.com wrote:

Thanks for letting me know!

The way this library currently works is by re-creating the query each time a new page is requested. While this works great for most cases, when scrolling down a few pages there are network and memory concerns.

I meant to optimize / rewrite this a while ago. I'll try to look into this in the near future!

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/marcorei/Infinite-Fire/issues/8#issuecomment-360886407, or mute the thread https://github.com/notifications/unsubscribe-auth/ARy7WH6avg7TbQ1lH2JKKDi7hu-5satKks5tOiwYgaJpZM4RurJf .

marcorei commented 6 years ago

Sure.

The InfiniteFireArray caches DataSnapshots. While this can become a concern with large amounts of data, at 100 DataSnapshots this shouldn't really be a problem. You mentioned that you're using media data - so I'd start by checking if the problem might actually be leaking Views or Bitmaps.

That being said, here is what I'd recommend as a more efficient way to implement paging / infinite scroll:

Cursors

Every time you request a new page, InfiniteFire creates a new Listener for the current page AND all previous page. A more efficient way would be to only request the current page.

Unfortunately, Firebase does not support traditional paging. The best alternative to this is to use a cursor. Here is a guide on how to do this with FireStore, the approach is very similar to what you would do with the RealtimeDatabase.

Disclaimer: As you can imagine, this approach can be problematic in some cases. For example, when the data you're requesting is ordered based on a value that changes often (Think: live updating scoreboard, orderd by score). Especially because there is no way to use Query.startAt(value, key) with just the key.

LRU Caching of DataSnapshots

If you want to take this further you could cache DataSnapshots based on a LRU strategy.

I would only recommend this if your data sets are getting really big though.

Hope I could give you some ideas!