Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is closing and reopening Realm instances bad for performance?

When using SQLite I usually have a single SQLiteOpenHelper instance per application and I never ever close it, since its database is used continuously by many other classes and closing/reopening it would be slower and more complicated.

Now I'm toying with Realm and I'm planning to access Realm instances only from Data Access Objects. Every call will be made from a worker thread.

I've been reading the examples and they usually call getInstance/close per Activity or background task. Since Realm persists the data in a file like SQLite, is it a good idea to call getInstance/close for each operation? I mean, would calling close actually close the file connection and thus make the next getInstance call slower? Should I cache a Realm instance at application level and inject it into the DAOs instead?

like image 798
Mister Smith Avatar asked Apr 05 '16 14:04

Mister Smith


1 Answers

Realm uses a reference counted thread local cache + optimized schema validation. It means that as long as you have at least one instance open on a thread calling Realm.getInstance() it is just a HashMap lookup.

If you have one instance open on any thread, we skip schema validation on other threads even though it is the first instance opened there.

If you close all instances on a given Thread we will free the thread local memory and it will need to be reallocated for the next instance on that thread.

If you close all instances on all threads you will have a "cold boot" which is the most expensive as we need to allocate memory + do a schema validation.

Best practice is to keep the Realm instance open for as long as your thread lives. For the UI thread that is easiest done using the pattern described here: https://realm.io/docs/java/latest/#controlling-the-lifecycle-of-realm-instances

For worker threads opening the Realm instance at the beginning and closing it when exiting would be the most optimal:

new Thread(new Runnable() {
  public void run() {
    Realm realm = Realm.getDefaultInstance();
    doWork(realm);
    realm.close();
  }
}).start();
like image 135
Christian Melchior Avatar answered Nov 15 '22 00:11

Christian Melchior