-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Memory and file exceeds size issue #4813
Comments
According to the exception message, it says your Realm file grew to 1.125 GB in size, and cannot be opened in memory. This is typically caused by if the following two conditions are met:
For example, common cause is code like this (on background thread)
instead of (on background thread)
It is also possible to reduce the amount of space that Realm has "retained from deletions" using the |
Yes. I'm calling AsyncTask for every 2 minutes. And realm instances are opened number of times and I might not closing it to avoid crashes relating to realm instances already closed. Is there any way (tool or method) that checks the unnecessary instances to close |
for example to my above comment: The querywhich is used to fetch data: getTimeCard() method: |
and my app size grows to 17.29 GB |
See https://realm.io/docs/java/latest/#closing-realm-instances and you can also see https://realm.io/docs/java/latest/#strategies-when-dealing-with-android-framework-threads for notes about using Realm in AsyncTask Also:
and
Basically, open the Realm instance for duration of thread, and pass the Realm instance to the methods that need to operate on it. But there's also a note on recommended lifecycle management for the UI Thread. |
Thanks for the answer. MainClass.java:
LaborDB.java: Error: |
I do not understand why you have so many handlers and threads and handlerthreads, and I don't know what thread you are calling these methods on 🙁 |
the above mainclass.java (UI class) code is in one method which is called on button click. |
The only advice I can give is to follow the documentation on how to manage Realm lifecycle on the background thread and on the UI thread. |
Yes. I tried this way: but stil it returns the error saying : Unable to evaluate the expression Method threw 'java.lang.IllegalStateException' exception. |
But that's not the recommended way. Recommended way for background thread is protected Void doInBackground(Void... params) {
Realm realm = Realm.getDefaultInstance();
try {
RealmResults<TCRLog> logs = getLogs(realm);
// do other things with Realm
} finally {
realm.close();
}
return null;
}
public RealmResults<TCRLog> getLogs(Realm realm) {
return realm.where(TCRLog.class).findAll();
} |
Sorry, I think I'm confusing you. My requirement is I just need to fetch the data from realm by closing and opening its instance. this is the code: TCRLogDB.class: Unable to evaluate the expression Method threw 'java.lang.IllegalStateException' exception. There is no background thread involved here. Please explain |
do i need to create realm instance in onCreate if calling from activity instead of creating instances in each method when calling from activity? |
You should not close
yes. |
Thanks @zaki50 Right now I'm not creating any realm instances in Activities. Strange thing is its working in some cases and not in some cases |
no, you have to make sure the Realm instance is not closed if you plan to use any |
But why its working in some cases: The folllowing is the code which is in one activity: (xxx.java extends AppCompatActivity) //getTimeCard() method is in LaborDB class public RealmResults getTimeCard() { This is working perfectly without throwing any error related to realm instance. |
We are using ref count to manage Realm instances internally. See below example:
|
Realm instance returned from So, if you already called |
@ssunkar technically for UI thread access it is possible to globally count number of all open activities and keep 1 global ui thread realm instead of using But you still need to pass Realm instance to methods instead of opening/closing in each method, especially if you need it on both UI thread and background thread. @beeender really? I thought it provides different "local instance" and each gets closed individually. 😮 |
That was the plan ... see #4573 |
@zaki50 @beeender Now I'm trying to close all the instances from beginning of my code. is it ok to not close just one instance to avoid the error "This Realm instance has already been closed, making it unusable" ??? keeping this just once instance causes memory leak exceptions?? |
By practise, have a Realm instance on UI thread won't be a big issue if you are always need it. And since there is a looper on UI thread, the Realm instance will be refreshed automatically. On a background thread, yes, you have to close the Realm instance properly otherwise there will be leaks. But those totally depend on your architecture. this may help https://realm.io/docs/java/latest/#controlling-the-lifecycle-of-realm-instances |
Thanks for clear explaination. So for a quick fix can i do this way? The folllowing is the code which is in one activity: (xxx.java extends AppCompatActivity) //getTimeCard() and closeRealm() method is in LaborDB class LaborDB.java |
No, the only quick fix would be to set |
is it possible to use realmResults by the above step after closing its instance? |
RealmResults is a thread-local proxy view to the database. So not unless all the data is copied before closing the Realm, which can be slow with large amount of data |
can you help how to find which method producing this below warning: I have like hundreds of transactions running in my application. where to check the closing of the realm instance? |
Ctrl+Shift+F for |
Is there anyway to copy RealmResults (to create detached copies) sams like RealmObjects? so that even if we close realm instances, I can use it from the stores memory |
Yes, with |
Without this workaround I noticed warnings related to realm ( for ex: Remember to call close() on all Realm instances. Realm /data/data/com.tcrsoftware.android/files/default.realm is being finalized without being closed, this can lead to running out of native memory.). But after the above steps I did not see any such warnings(I test for half day with number of transactions). Does this solved my issue or is there any better way to test memory usage? |
It should solve your issue I think if can you ensure all Realm instances which are used on the bg thread are closed properly. the warning log will be printed if the Realm instance gets GCed before |
ohk thanks. I have problem converting List to RealmResults. How to achieve this? |
Only a query executed against an open Realm returns a RealmResults. A managed RealmList can also be converted to RealmResults using |
@Zhuinden is there any way i can send my code for review. The problem is as I am copying the realmresults (using copyFrpmrealm) to List, I'm unable to modify the obtained list in particular transaction. so either I need to convert that List to realmresults or copyFromRealm should results realmresults again. |
An unmanaged object can be used to update existing RealmObjects if their primary key matches and |
@ssunkar Did the comment above answer the question? |
as of now there is no issue regarding implementation. |
Goal
To allow app to work by solving realm memory issues
Steps & Code to Reproduce
06-12 13:45:51.895 16425-16425/? D/dalvikvm: Late-enabling CheckJNI
06-12 13:45:51.895 16425-16425/? D/dalvikvm: Try to disable coredump for pid 16425
06-12 13:45:51.895 16425-16425/? D/dalvikvm: Process 16425 nice name: com.tcrsoftware.android
06-12 13:45:51.895 16425-16425/? D/dalvikvm: Extra Options: not specified
06-12 13:45:51.935 16425-16425/com.tcrsoftware.android I/MultiDex: VM with version 1.6.0 does not have multidex support
06-12 13:45:51.935 16425-16425/com.tcrsoftware.android I/MultiDex: install
06-12 13:45:51.935 16425-16425/com.tcrsoftware.android I/MultiDex: MultiDexExtractor.load(/data/app/com.tcrsoftware.android-2.apk, false)
06-12 13:45:51.945 16425-16425/com.tcrsoftware.android I/MultiDex: loading existing secondary dex files
06-12 13:45:51.945 16425-16425/com.tcrsoftware.android I/MultiDex: load found 1 secondary dex files
06-12 13:45:51.945 16425-16425/com.tcrsoftware.android I/MultiDex: install done
06-12 13:45:51.945 16425-16425/com.tcrsoftware.android D/dalvikvm: DexOpt: couldn't find static field Landroid/os/Build;.SUPPORTED_ABIS
06-12 13:45:51.945 16425-16425/com.tcrsoftware.android W/dalvikvm: VFY: unable to resolve static field 162 (SUPPORTED_ABIS) in Landroid/os/Build;
06-12 13:45:51.945 16425-16425/com.tcrsoftware.android D/dalvikvm: VFY: replacing opcode 0x62 at 0x0006
06-12 13:45:51.945 16425-16425/com.tcrsoftware.android D/dalvikvm: DexOpt: couldn't find static field Landroid/os/Build;.SUPPORTED_ABIS
06-12 13:45:51.945 16425-16425/com.tcrsoftware.android I/dalvikvm: DexOpt: unable to optimize static field ref 0x00a2 at 0x0b in Lcom/getkeepsafe/relinker/SystemLibraryLoader;.supportedAbis
06-12 13:45:51.945 16425-16425/com.tcrsoftware.android D/dalvikvm: Trying to load lib /data/app-lib/com.tcrsoftware.android-2/librealm-jni.so 0x43a301e0
06-12 13:45:51.945 16425-16425/com.tcrsoftware.android D/dalvikvm: Added shared lib /data/app-lib/com.tcrsoftware.android-2/librealm-jni.so 0x43a301e0
06-12 13:45:51.995 16425-16425/com.tcrsoftware.android D/REALM: jni: ThrowingException 11, mmap() failed: Out of memory size: 1207959552 offset: 0 in io_realm_internal_SharedGroup.cpp line 113, .
06-12 13:45:51.995 16425-16425/com.tcrsoftware.android D/REALM: Exception has been throw: Unrecoverable error. mmap() failed: Out of memory size: 1207959552 offset: 0 in io_realm_internal_SharedGroup.cpp line 113
06-12 13:45:51.995 16425-16425/com.tcrsoftware.android D/AndroidRuntime: Shutting down VM
06-12 13:45:51.995 16425-16425/com.tcrsoftware.android W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x43712160)
06-12 13:45:51.995 16425-16425/com.tcrsoftware.android E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.tcrsoftware.android, PID: 16425
io.realm.exceptions.RealmError: Unrecoverable error. mmap() failed: Out of memory size: 1207959552 offset: 0 in io_realm_internal_SharedGroup.cpp line 113
at io.realm.internal.SharedGroup.createNativeWithImplicitTransactions(Native Method)
at io.realm.internal.SharedGroup.openSharedGroupOrFail(SharedGroup.java:95)
at io.realm.internal.SharedGroup.(SharedGroup.java:74)
at io.realm.internal.SharedGroupManager.(SharedGroupManager.java:49)
at io.realm.BaseRealm.(BaseRealm.java:86)
at io.realm.Realm.(Realm.java:135)
at io.realm.Realm.createAndValidate(Realm.java:233)
at io.realm.Realm.createInstance(Realm.java:214)
at io.realm.RealmCache.createRealmOrGetFromCache(RealmCache.java:126)
at io.realm.Realm.getInstance(Realm.java:178)
at com.tcrsoftware.android.MainApplication.onCreate(MainApplication.java:44)
at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1007)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4384)
at android.app.ActivityThread.access$1500(ActivityThread.java:138)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1296)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:149)
at android.app.ActivityThread.main(ActivityThread.java:5061)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:610)
at dalvik.system.NativeStart.main(Native Method)
06-12 13:46:42.055 16425-16425/? I/Process: Sending signal. PID: 16425 SIG: 9
Code Sample
MultiDex.install(this);
The text was updated successfully, but these errors were encountered: