Android : How to use RealmBaseAdapter without memory exceptions?

on Wednesday, April 1, 2015


I am trying to figure out what is the right way to use the RealmBaseAdapter.


Background:



  • I have an app with a single activity and many fragments.

  • The main screen is a fragment that contains a ViewPager with child fragments (with tabs in the action bar).

  • Each child fragment contains a list view that displays realm results using a subclass of RealmBaseAdapter.

  • When the user taps on an item in one of the list views an inner fragment is displayed (it replaces the main fragment with all its child fragments).

  • For the RealmBaseAdapter to work the Realm object that created the RealmResults has to be open (after getInstance and before close), so I have a base fragment that creates an instance of Realm in onStart and closes it in onDestroy.


Now the problem:

There a re too many memory related crashes in the app and I think that I understand the reason.

The problem is that there is always at least one open Realm, which avoids from it to be cleaned.

Meaning that realm grows in the memory all the time and never gets the chance to be cleaned.


According to few memory related issues in the realm's github repo I understand that the realm should be opened and closed for as short operation as possible, but it comes in contrast with the fact that the RealmBaseAdapter requires the originating realm object to be open for all its life time.


Any help/thoughts will be appreciated.


0 comments:

Post a Comment