Realm.getInstance very slow with big database (~12mb)
I have a database that contains several thousands data. When I try to get an instance of realm with the Realm.getInstance method to load data on the main thread, it takes several seconds and the main thread is blocked. There are some optimizations I could do or it is an identified problem without solution ? Thank you in advance.
Hi @SilkSama Can you tell more about what specific part is being slow? Is it just opening the Realm, querying it? or reading the data?
Hi @cmelchior just opening the realm
That sounds really odd. Opening a Realm should normally be really fast operation.
- Do you see this problem across all devices or only some? What about emulators/genymotion?
- What version of Realm are you using?
- Is it also slow to open if your Realm is empty as well?
- Where is your Realm stored? Internal storage or the SDCard?
- Can you show us the code where you are opening the Realm?
Do you see this problem across all devices or only some? What about emulators/genymotion? -> I've tried on Moto X, Nexus 4 and Genymotion and the result is the same What version of Realm are you using? -> I use the 0.80.3 version. I have not try the last version. Is it also slow to open if your Realm is empty as well? -> if the database is empty open realm is fast, no problem in this case. Where is your Realm stored? Internal storage or the SDCard? -> Realm is store on the internal storage Can you show us the code where you are opening the Realm? -> I just call Realm.getInstance(mAppContext, "db_name");
Thank you. There should be very little between 0.80.3 and 0.81.1 that should speedup opening a Realm, so I doubt that would reveal much.
Are you able to share your project with so we can debug this further or alternative provide a Realm file with the data that is causing it to be slow to open? You can do it privately through [email protected] if you wish
I'm just trying with the 0.81.1 version and the problem disappeared :) So the slowness is due to the 0.80.3 version. I'm just reading the changelog and there is this line -> Fixed a performance regression introduced in 0.80.3 occurring during the validation of the Realm schema. May be my problem was due to this regression on 0.80.3 ? Thank you for your help
Yes, that sounds like it. Thank you for trying that out. I completely forgot we fixed a bug in that area. I will close this issue now, but feel free to reopen or or open another if you experience any other problems.
@cmelchior You said opening a Realm is a "really fast" operation. How fast is it supposed to be? I'm analyzing my dependency graph performance (Dagger 2) and opening Realms seems to be an expensive operation? I'm using Realm 0.87.5 and calling Realm.getInstance(realmConfiguration) to get my Realm instances.
Here is a screenshot of my method trace:

Hi @Plastix It depends. The first time you open a Realm instance (on any thread) we run a validation of all your schemas vs. what is actually on disk. That takes an effort linear with how many model classes / fields you have. I would imagine that is what you are seeing there, but after the validation are done, it should be faster as we cache the validation across all threads as well as the actual Realm instance on any individual thread.
I don't have any specific numbers as it depends a lot on a number of factors.
We have been talking about adding a async getInstance methods for this exact case though: https://github.com/realm/realm-java/issues/2299
A work-around for now would be to make sure a Realm is opened on a background thread before being accessed from the main thread.
Note however that if you close all access to a Realm file we are running the validation, so having an instance running for the lifetime of your app will prevent issues with that. That is probably a good idea anyway though.
@cmelchior Ah it makes sense now. Doing some more profiling it looks like it is just the first creation that takes a while. Should I use a single Realm instance for my entire application? I've been opening and closing Realms as I need them. However, if I always access a singleton Realm instance from the main thread and use async operations will I run into problems?
Thanks for the super quick response and clearing this up!
Realm instances are thread local so you need as a minimum 1 instance pr. thread. That said, we have internal caching in place, so opening an closing Realms is usually the easiest in terms of controlling the lifecycle:
Realm realm1 = Realm.getDefaultInstance(); // Occur first hit of opening / validating
Realm ream2 = Realm.getDefaultInstance(); // Just a map lookup
We also have some tips here: https://realm.io/docs/java/latest/#controlling-the-lifecycle-of-realm-instances
Great. Thanks for the support!