Realm-cocoa: Fatal error: 'try!' expression unexpectedly raised an error: Error Domain=io.realm Code=9 "mmap() failed: Cannot allocate memory size: 1207959552 offset: 0"

Created on 10 Apr 2020  路  6Comments  路  Source: realm/realm-cocoa

FYI, Found same kind of issues reported earlier, but non of the solutions worked for me.

Issue

I'm using Realm as DB for my chat application, over time i'm getting crash as below,
Fatal error: 'try!' expression unexpectedly raised an error: Error Domain=io.realm Code=9 "mmap() failed: Cannot allocate memory size: 1207959552 offset: 0" UserInfo={NSLocalizedDescription=mmap() failed: Cannot allocate memory size: 1207959552 offset: 0, Error Code=9}:

Thinks to be know,

  • I'm using custom realm configuration
  • My realm file is in shared container (To access from Notification service extension)
  • Messages will be written to realm from Notification service extension.
  • Using realm in background thread, realm will be configured every time when realm is required in BG thread. Since realm used in a thread should be created from same thread.

Solutions tried

  • Added _shouldCompactOnLaunch_, When free space is more than 60% i'm returning _true_
    Realm.Configuration(shouldCompactOnLaunch: { totalBytes, usedBytes in let occupiedPercentage = (Double(usedBytes) / Double(totalBytes)) * 100 let junkFilePercentage = 100 - occupiedPercentage return junkFilePercentage >= 60 })

  • I could see _shouldCompactOnLaunch_ works fine, usedBytes before: 102435 after returning true reduced to 4566. Even though i'm getting crash at some point

  • Deleting realm files default.realm, default.realm.lock, default.realm.management when getting exception in config realm. And re-configuring realm.

Expected Results

Should avoid crash while config realm.

Actual Results

Getting crash with above mentioned exception.

Steps to Reproduce

When continuously accessing realm, In my case continuously sending and receiving messages (around 300 message or 2 to 3 hours)

Version of Realm and Tooling

Realm framework version: 4.3.1

Xcode version: 11.3 (11C29)

iOS/OSX version: In all iOS version, especially iOS 13

O-Community

Most helpful comment

Same issue here

All 6 comments

@gokulgovind Did you fix this issue ?

Same issue here

@gokulgovind , are you using Realm Sync at all, or are these local realms? If you're using Realm Sync then there could be some other lurking variables.

If you're using local realms only, make sure to follow the recommendations tgoyne makes in this thread.

It goes into depth on background threads impact on file size growth.

I'm also experiencing this crash since upgrading from Realm 3.x. When I open the container from my device, the Realm file is quite small (20-30MB, or even 1MB in one case). Is there a way to determine whether it's file size that's causing the trouble? (Maybe the file size bloats momentarily before I'm able to view the file?) I'm stuck, not sure how to diagnose this further.

Thread 17: Fatal error: 'try!' expression unexpectedly raised an error: Error Domain=io.realm Code=9 "mmap() failed: Cannot allocate memory size: 37748736 offset: 134217728" UserInfo={NSLocalizedDescription=mmap() failed: Cannot allocate memory size: 37748736 offset: 134217728, Error Code=9}

If you are encountering issues due to mmap use Realm.Configuration.maxNumberOfActiveVersions to limit the amount of active versions a Realm contains. This may help you figure out areas in your code which may be holding onto a Realm version too long and cannot be cleaned up, hence causing memory issues.

Setting an arbitrary maxNumberOfActiveVersions to, lets say 5 may help narrow down the issue, but play around with this number.

Please see our release note regarding Realm.Configuration.maxNumberOfActiveVersions here: https://github.com/realm/realm-cocoa/releases/tag/v5.0.0

I've successfully reproduced a bug where we weren't releasing old versions in a place where we should, which would cause this problem.

Was this page helpful?
0 / 5 - 0 ratings