According to Official Documentation, always close the realm instance when you done with them.

Best Practise Close Instance

1
2
3
4
5
6
7
8
try {
Realm realm = Realm.getDefaultInstance();
//Use the realm instance
} catch(Exception e) {
//handle exceptions
} finally {
realm.close();
}

if you useminSdkVersion >= 19andJava >= 7, you won’t close it manually.

1
2
3
try (Realm realm = Realm.getDefaultInstance()) {
// No need to close the Realm instance manually
}

Getting the above answer from the this stackoverflow answer

Kotlin

The try (Realm realm = Realm.getDefaultInstance()) in Java is called try-with-resource. This is how we do in Kotlin:

1
2
3
4
Realm.getDefaultInstance().use {
// no need to close realm with .use keyword
it.where(...).equalTo(...).findAll()
}

Note: You have to use minSdkVersion >= 19andJava >= 7 in order to not close it manually.