Skip to content
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

Unrecoverable error. mmap() failed: Out of memory #3502

Closed
simc opened this issue Sep 27, 2016 · 7 comments
Closed

Unrecoverable error. mmap() failed: Out of memory #3502

simc opened this issue Sep 27, 2016 · 7 comments
Labels

Comments

@simc
Copy link

simc commented Sep 27, 2016

Goal

My app runs in kiosk mode and it can not be restarted for at least a month (This is why I can't use compactRealm()). The app syncs about twice per minute with the server on a background thread and the realm database is being accessed every time.

It is impossible that I forgot to close a Realm. I tripe checked it. And I used the following Kotlin method to make sure there is no leak.

fun runWithRealm(exec: (Realm) -> Unit) {
    val realm = Realm.getDefaultInstance()
    try {
        exec(realm)
        realm.close()

    } catch(e: Exception) {
        realm.close()
        //handle e
    }
}

Actual Results

I still get the same error as in #3050 and I tried running the network access on a looper thread and closing the realm during long running operations. After some time the app has a memory (disk) usage of about 1.8Gb :/ But the stored data should not take more than 50kb.

Steps & Code to Reproduce

Sadly I cannot reproduce the error. Some time the app runs without a problem and without taking more space and after about two weeks the memory usage explodes.

Version of Realm and tooling

Realm version(s): 1.2.0

Android Studio version: 2.2

Which Android version and device: Android 5.1.0, Hardkernel ODROID C2


Is it possible that Realm keeps old DB versions after closing the Realm and after shutting down the looper safely?

@Zhuinden
Copy link
Contributor

Zhuinden commented Sep 27, 2016

Did you use Realm.setAutoRefresh(true) for your looper thread? (and Realm.setAutoRefresh(false) on termination)

I am actually not sure if it's necessary, but I think it might be.

Also, query for results inside the transaction when you're on a background thread, and preferably don't execute multiple transactions on the same loop.

@simc
Copy link
Author

simc commented Sep 27, 2016

I did not set it explicitly but I think it's default on looper threads. I think I never use more than one transaction...
But shouldn't the database be able to clean all used resources when being closed without calling compactRealm()? Is this a known problem?

@Zhuinden
Copy link
Contributor

Zhuinden commented Sep 27, 2016

Realm instances that are closed should release their versions. The addition of size added by previous versions is not released automatically (only with Realm.compactRealm(), however future writes re-use the space previously used by obsolete versions that are no longer retained by any open Realm instance.

Meaning your file should not increase so drastically, unless you have many concurrent background threads that continuously create new versions that can never be filled up by future threads.

Or if your versions are not released sometimes, in which case you should consider putting realm.close() in finally { instead. After all, a Throwable would easily override your current catch

@kneth
Copy link
Contributor

kneth commented Sep 29, 2016

@leisim The file size will not decrease but freed resources (deleted objects, old versions) will be reused eventually. Unfortunately, Realm doesn't yet support online compaction (compacting open Realms).

Can you schedule a restart of the app? If so, you could compact the Realm during restart.

@kneth kneth added the T-Help label Sep 29, 2016
@kneth kneth changed the title Unrecoverable error. mmap() failed: Out of memory Unrecoverable error. mmap() failed: Out of memory Sep 29, 2016
@simc
Copy link
Author

simc commented Sep 29, 2016

@kneth OK. I can schedule a restart but of course it is not ideal. The app is used 24/7 by our employees and a restart will disturb their work.

Is online compaction a scheduled feature? Is there an issue I can watch?

This issue sounds quite similar but it is already closed realm/realm-core#34

@kneth
Copy link
Contributor

kneth commented Sep 29, 2016

Oh dear, that's a very old issue and Realm files can easily come larger than 2 GB. Actually, it's more mmap() which is limiting the size.

Online compaction isn't currently a scheduled feature as we didn't image that apps with run 24/7.

Is it possible for you to share your app with us? By reading the source code, we might be able to give you a suggestion on how to get around it.

@beeender
Copy link
Contributor

Close due to no response.
@leisim feel free to reopen it if necessary. It would be helpful if we can have more information :P

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 17, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

5 participants