Optimize realm database when Out of memory caused
See original GitHub issueI’m facing with out of memory error. Here is stack trace:
Exception io.realm.exceptions.RealmError: Unrecoverable error. mmap() failed: Out of memory size: 738197504 offset: 0 in /tmp/realm-java/realm/realm-library/src/main/cpp/io_realm_internal_SharedRealm.cpp line 238
I found a solution is using compactRealm
and I put Realm.compactRealm(getRealmConfig());
at line 116 in OnCreate()
of Application class.
It is ok if database become not too large enough to get Out of memory size
or I get this exception.
Exception io.realm.exceptions.RealmError: Unrecoverable error. mmap() failed: Out of memory size: 939524096 offset: 0 in /tmp/realm-java/realm/realm-library/src/main/cpp/io_realm_internal_SharedRealm.cpp line 238 io.realm.internal.SharedRealm.nativeGetSharedRealm (SharedRealm.java) io.realm.internal.SharedRealm.<init> (SharedRealm.java:185) io.realm.internal.SharedRealm.getInstance (SharedRealm.java:237) io.realm.internal.SharedRealm.getInstance (SharedRealm.java:201) io.realm.BaseRealm.compactRealm (BaseRealm.java:601) io.realm.Realm.compactRealm (Realm.java:1772) x.y.z.App.onCreate (App.java:116)
Version of Realm and tooling
Realm version(s): 3.4 Snapshot
Android Studio version: 3.0.0
Which Android version and device: 5.0.2
Issue Analytics
- State:
- Created 6 years ago
- Comments:11 (6 by maintainers)
Also keep an eye out for warnings like this: “Remember to call close() on all Realm instances. Realm %s is being finalized without being closed, this can lead to running out of native memory.”
This Realm object finalizer will log this as warnings if close is not correctly called. If you don’t see these and still run out of memory because of large file sizes, it means that you are actively holding on to resources somehow.
Hi @Zhuinden
I forgot using
realm.close()
when developing small app. By time, i received some Firebase crashes from user that they reached Out of Memory with Realm. Now i close realm in java code and trying to compact database for who reached Out of Memory error. That’s it.