realm-java icon indicating copy to clipboard operation
realm-java copied to clipboard

Out of memory in io_realm_internal_SharedGroup.cpp

Open dotgc opened this issue 11 years ago • 43 comments

java.lang.RuntimeException: Unspecified exception. mmap() failed: Out of memory in io_realm_internal_SharedGroup.cpp line 101
       at io.realm.internal.SharedGroup.createNativeWithImplicitTransactions(SharedGroup.java)
       at io.realm.internal.SharedGroup.<init>(SharedGroup.java:55)
       at io.realm.Realm.<init>(Realm.java:160)
       at io.realm.Realm.createAndValidate(Realm.java:435)
       at io.realm.Realm.create(Realm.java:414)
       at io.realm.Realm.getInstance(Realm.java:332)
       at io.realm.Realm.getInstance(Realm.java:294)
       at io.realm.Realm.getInstance(Realm.java:275)

dotgc avatar Jan 26 '15 23:01 dotgc

Hi @ImGauravC

That error is happening because the device has run out of memory. It can happen in a number of scenarios, the most likely being:

  1. You a trying to commit a too large transaction
  2. You are not closing your Realms on background threads, and keep making new threads.
  3. You have too many open Realms across threads that doesn't auto update, hence are "different" versions.

We really need more information to give an informed answer, but given you get the error when calling .getInstance() I guess that 2) is the most likely culprit.

cmelchior avatar Jan 27 '15 08:01 cmelchior

I got this in the UI thread and I access realm instances in just 2 threads (1 UI and another background) and I properly close the realm instances everytime i open them and it's anyway a thread singleton.

dotgc avatar Jan 27 '15 20:01 dotgc

@ImGauravC 1) You a trying to commit a too large transaction. How many items you save per transaction? Model of your device on which you test?

desugar-64 avatar Jan 28 '15 09:01 desugar-64

Be aware that you have to call close() for every time you called getInstance().

bmunkholm avatar Jan 28 '15 13:01 bmunkholm

Would open Realms in a different "process" lead to a memory error because of reason 3) ?

Dexteroo7 avatar Jan 31 '15 11:01 Dexteroo7

Realm doesn't support multiprocess access yet, so it would fail. I doubt it would throw a out of memory error though.

cmelchior avatar Jan 31 '15 19:01 cmelchior

a) I call close exactly as many times as I call getInstance b) I'm using Realm in only two thread - Main and a background thread. c) What exactly would be a "large transaction"? I'm committing only text messages to the realm.

dotgc avatar Jan 31 '15 19:01 dotgc

@ImGauravC can you check your app's data size in your settings. Also clear your data and try again. I think you are facing same issue as me. In my case write transactions keep increasing the app size.

Dexteroo7 avatar Feb 01 '15 06:02 Dexteroo7

Following is the stacktrace I get.

java.lang.RuntimeException: Unable to start activity ComponentInfo{[package_name].MessagingActivity}: java.lang.RuntimeException: Unspecified exception. mmap() failed: Out of memory in io_realm_internal_SharedGroup.cpp line 101
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
       at android.app.ActivityThread.access$900(ActivityThread.java:161)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:157)
       at android.app.ActivityThread.main(ActivityThread.java:5356)
       at java.lang.reflect.Method.invokeNative(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:515)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
       at dalvik.system.NativeStart.main(NativeStart.java)
Caused by: java.lang.RuntimeException: Unspecified exception. mmap() failed: Out of memory in io_realm_internal_SharedGroup.cpp line 101
       at io.realm.internal.SharedGroup.createNativeWithImplicitTransactions(SharedGroup.java)
       at io.realm.internal.SharedGroup.<init>(SharedGroup.java:55)
       at io.realm.Realm.<init>(Realm.java:159)
       at io.realm.Realm.createAndValidate(Realm.java:441)
       at io.realm.Realm.create(Realm.java:418)
       at io.realm.Realm.getInstance(Realm.java:336)
       at io.realm.Realm.getInstance(Realm.java:298)
       at io.realm.Realm.getInstance(Realm.java:279)
       at [package_name].MessagingActivity.onCreate(MessagingActivity.java:100)
       at android.app.Activity.performCreate(Activity.java:5426)
       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
       at android.app.ActivityThread.access$900(ActivityThread.java:161)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:157)
       at android.app.ActivityThread.main(ActivityThread.java:5356)
       at java.lang.reflect.Method.invokeNative(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:515)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
       at dalvik.system.NativeStart.main(NativeStart.java)

dotgc avatar Feb 02 '15 20:02 dotgc

@realm-ci I was getting a similar out of memory error when I tried to rapidly commit transactions on a background thread. Surprisingly the app data size increased exponentially (checked in settings). Adding a change listener to my main thread (UI) realm fixed this. I will share my project as soon as I am able to :)

Dexteroo7 avatar Feb 02 '15 20:02 Dexteroo7

Are you having long living transactions in your app? By long living I mean seconds between beginTransaction() and commitTransaction.

Also, you say you only have two threads, UI and background. How does the background thread look like? Is it a normal Thread? Does it have a Looper? Is it an AsyncTask? the more details you can share the better. We really want to get help you and get to the bottom of this.

On Mon, Feb 2, 2015 at 9:20 PM, Gaurav [email protected] wrote:

java.lang.RuntimeException: Unable to start activity ComponentInfo{[package_name].MessagingActivity}: java.lang.RuntimeException: Unspecified exception. mmap() failed: Out of memory in io_realm_internal_SharedGroup.cpp line 101 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363) at android.app.ActivityThread.access$900(ActivityThread.java:161) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:157) at android.app.ActivityThread.main(ActivityThread.java:5356) at java.lang.reflect.Method.invokeNative(Method.java) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081) at dalvik.system.NativeStart.main(NativeStart.java)Caused by: java.lang.RuntimeException: Unspecified exception. mmap() failed: Out of memory in io_realm_internal_SharedGroup.cpp line 101 at io.realm.internal.SharedGroup.createNativeWithImplicitTransactions(SharedGroup.java) at io.realm.internal.SharedGroup.(SharedGroup.java:55) at io.realm.Realm.(Realm.java:159) at io.realm.Realm.createAndValidate(Realm.java:441) at io.realm.Realm.create(Realm.java:418) at io.realm.Realm.getInstance(Realm.java:336) at io.realm.Realm.getInstance(Realm.java:298) at io.realm.Realm.getInstance(Realm.java:279) at [package_name].MessagingActivity.onCreate(MessagingActivity.java:100) at android.app.Activity.performCreate(Activity.java:5426) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363) at android.app.ActivityThread.access$900(ActivityThread.java:161) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:157) at android.app.ActivityThread.main(ActivityThread.java:5356) at java.lang.reflect.Method.invokeNative(Method.java) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081) at dalvik.system.NativeStart.main(NativeStart.java)

— Reply to this email directly or view it on GitHub https://github.com/realm/realm-java/issues/794#issuecomment-72529968.

Emanuele Zattin (@emanuelez https://twitter.com/emanuelez) SW Engineer & CI Specialist

emanuelez avatar Feb 02 '15 20:02 emanuelez

a) Yes, I do have long living transactions in my app b) The background thread is a looper thread. https://github.com/realm/realm-java/issues/726#issuecomment-69439067 , this is the pattern I'm following.

dotgc avatar Feb 02 '15 20:02 dotgc

a) Having long lived transactions is a very bad pattern to follow, whichever database you are using, not only Realm. I suggest you change that as a first thing. The longer a transaction is the more memory will be needed to hold all the versions of your Realm, which can of course lead to your memory issues.

b) Since it's a Looper thread it's not a good idea to continuously call getInstance() and close(). You can see how we setup a worker thread in our adapter example here: https://github.com/realm/realm-java/blob/master/examples/adapterExample/src/main/java/io/realm/examples/realmadapters/WorkerThread.java Ad you can see we keep using the same Realm instance and only call close in the finally block, which will be called when the Looper is killed.

On Mon, Feb 2, 2015 at 9:43 PM, Gaurav [email protected] wrote:

a) Yes, I do have long living transactions in my app b) The background thread is a looper thread. #726 (comment) https://github.com/realm/realm-java/issues/726#issuecomment-69439067 , this is the pattern I'm following.

— Reply to this email directly or view it on GitHub https://github.com/realm/realm-java/issues/794#issuecomment-72533787.

Emanuele Zattin (@emanuelez https://twitter.com/emanuelez) SW Engineer & CI Specialist

emanuelez avatar Feb 02 '15 20:02 emanuelez

I had looked at the example. I continuously call for getInstance() and close() because I couldn't find an easy way to reference the realm instance of the Looper thread in the Runnable that I'll post to it. Can you suggest a way?

dotgc avatar Feb 02 '15 21:02 dotgc

You could send plain messages instead of runnables. You could implement the logic of the runnable in a Handler instead. See the worker handler in the same adapter example.

On Mon, Feb 2, 2015 at 10:18 PM, Gaurav [email protected] wrote:

I had looked at the example. I continuously call for getInstance() and close() because I couldn't find an easy way to reference the realm instance of the Looper thread in the Runnable that I'll post to it. Can you suggest a way?

— Reply to this email directly or view it on GitHub https://github.com/realm/realm-java/issues/794#issuecomment-72539896.

Emanuele Zattin (@emanuelez https://twitter.com/emanuelez) SW Engineer & CI Specialist

emanuelez avatar Feb 02 '15 21:02 emanuelez

Ok. Also what exactly is the issue with calling getInstance and close multiple times? I thought since it's a thread singleton, it won't matter.

dotgc avatar Feb 02 '15 21:02 dotgc

If you call getInstance N times and then close N times, then the only Nth time you call close will actually perform the cleanup of the native resources and delete the cached Realm instance. But since you keep calling getInstance once and close once (but several times in a row), then every call to close will perform the cleanup. Also getInstance will have to instantiate from scratch which is a fairly expensive operation.

On Mon, Feb 2, 2015 at 10:52 PM, Gaurav [email protected] wrote:

Ok. Also what exactly is the issue with calling getInstance and close multiple times? I thought since it's a thread singleton, it won't matter.

— Reply to this email directly or view it on GitHub https://github.com/realm/realm-java/issues/794#issuecomment-72545859.

Emanuele Zattin (@emanuelez https://twitter.com/emanuelez) SW Engineer & CI Specialist

emanuelez avatar Feb 02 '15 22:02 emanuelez

Also how long a transaction would be safe? Currently the longest transaction I make takes around 300ms.

dotgc avatar Feb 03 '15 05:02 dotgc

TL;DR

I have a total of 3 Realm instances in the entire application and I still have this issue. My local db size is about 100 MBs.

Long Version: From time to time I get this stack trace:

Realm Version: 0.78.0
02-03 23:14:26.367    6957-6957/com.example.android E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.android, PID: 6957
    java.lang.RuntimeException: Unspecified exception. mremap(): failed: Out of memory in io_realm_internal_SharedGroup.cpp line 144
            at io.realm.internal.SharedGroup.nativeAdvanceRead(Native Method)
            at io.realm.internal.SharedGroup.advanceRead(SharedGroup.java:88)
            at io.realm.internal.ImplicitTransaction.advanceRead(ImplicitTransaction.java:30)
            at io.realm.Realm$RealmCallback.handleMessage(Realm.java:216)
            at android.os.Handler.dispatchMessage(Handler.java:98)
            at android.os.Looper.loop(Looper.java:146)
            at android.app.ActivityThread.main(ActivityThread.java:5748)
            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:1291)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
            at dalvik.system.NativeStart.main(Native Method)

Its kind of becoming pretty unusable once the DB size grows beyond 100 MBs. I have 4-5 tables with some have relationships among them. When the DB size is small I had no issues, reading or writing data. For writing data I am using the pattern as suggested in the WorkerHandler example in the docs. After a while, making a findAll call for reading also causes Out of memory issues. Can 3 realms cause this much damage? I have hunted down Java related issues and have run out of clues. Its upto the native part now, which unfortunately I can't debug.

creativepsyco avatar Feb 03 '15 15:02 creativepsyco

Hello Mohit. Do you have long living transactions? Do you have different Realm instances at different versions for long periods of time? Can you by any chance share your code, even in a private way?

On Tue, Feb 3, 2015 at 4:31 PM, Mohit Kanwal [email protected] wrote:

TL;DR

I have a total of 3 Realm instances in the entire application and I still have this issue. My local db size is about 100 MBs. From time to time I get this stack trace:

Realm Version: 0.78.0 02-03 23:14:26.367 6957-6957/com.example.android E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.example.android, PID: 6957 java.lang.RuntimeException: Unspecified exception. mremap(): failed: Out of memory in io_realm_internal_SharedGroup.cpp line 144 at io.realm.internal.SharedGroup.nativeAdvanceRead(Native Method) at io.realm.internal.SharedGroup.advanceRead(SharedGroup.java:88) at io.realm.internal.ImplicitTransaction.advanceRead(ImplicitTransaction.java:30) at io.realm.Realm$RealmCallback.handleMessage(Realm.java:216) at android.os.Handler.dispatchMessage(Handler.java:98) at android.os.Looper.loop(Looper.java:146) at android.app.ActivityThread.main(ActivityThread.java:5748) 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:1291) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107) at dalvik.system.NativeStart.main(Native Method)

Its kind of becoming pretty unusable once the DB size grows beyond 100 MBs. I have 4-5 tables with some have relationships among them. When the DB size is small I had no issues, reading or writing data. For writing data I am using the pattern as suggested in the WorkerHandler example in the docs. After a while, making a findAll call for reading also causes Out of memory issues. Can 3 realms cause this much damage? I have hunted down Java related issues and have run out of clues. Its upto the native part now, which unfortunately I can't debug.

— Reply to this email directly or view it on GitHub https://github.com/realm/realm-java/issues/794#issuecomment-72670553.

Emanuele Zattin (@emanuelez https://twitter.com/emanuelez) SW Engineer & CI Specialist

emanuelez avatar Feb 03 '15 15:02 emanuelez