realm-swift: Unable to allocate this memory size

无法分配这个内存大小?😂😂😂

Translation: Unable to allocate this memory size.

Code sample:

config.fileURL = Optional(file:///private/var/mobile/Containers/Shared/AppGroup/1E15843C-9D83-4FA7-AE36-789BCCA12082/JTime.realm)

Error:

fatal error: 'try!' expression unexpectedly raised an error: Error Domain=io.realm Code=9 "mmap() failed: Cannot allocate memory size: 3489660928 offset: 0" UserInfo={NSLocalizedDescription=mmap() failed: Cannot allocate memory size: 3489660928 offset: 0, Error Code=9}: file /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-800.0.63/src/swift/stdlib/public/core/ErrorType.swift, line 178
2017-02-07 10:20:10.646285 JTimeObserver[7995:3270515] fatal error: 'try!' expression unexpectedly raised an error: Error Domain=io.realm Code=9 "mmap() failed: Cannot allocate memory size: 3489660928 offset: 0" UserInfo={NSLocalizedDescription=mmap() failed: Cannot allocate memory size: 3489660928 offset: 0, Error Code=9}: file /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-800.0.63/src/swift/stdlib/public/core/ErrorType.swift, line 178

/*************** I'm cutting line *******************/
failed: Cannot allocate memory size: 3489660928 offset: 0" 

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 24 (6 by maintainers)

Most upvoted comments

@Jinxiansen, we’re sorry that you’re still having issues with Realm. Would you be willing to send us the Realm file that is causing the issue, so we can examine it ourselves? You can email it to help@realm.io.

I have seem a similar case. It grows up to 3GB. We have 4 entities, and only one 1-to-n relationship between two entities. In a similar user journey for another user, the file size is as small as 200MB only. Some of our users have been using the app since our early versions embedding Realm 0.98.x, and upgrade to recent Realm along the way.

@jpsim @austinzheng Our current app structure makes it hard for me to switch in using realm.invalidate() at the moment, feel free to close this ticket first. As @austinzheng I will open another issue later if needed. Thanks

@harryworld can you file a new ticket so we can better track your issues? As for your question, you can call realm.invalidate() if you don’t plan on using that Realm again or any of the object instances you got from the Realm.

More specifically, you want to avoid keeping Realms that won’t be used again open. You can ensure that these Realms don’t live longer than they should by calling invalidate(), or by wrapping them in an autoreleasepool block so that they will be properly destroyed and their resources freed. Note that the implicit autorelease pools that are present on threads with runloops (like the main thread) only drain once per runloop iteration, so if you have (e.g.) a complicated loop that opens many Realms, you may end up keeping many versions of the Realm active even if you nil out your local Realm variables when you’re done using them.

You can read this document on autorelease pools for more information. Although it’s written using Objective-C examples it applies equally well to Swift’s ARC-based memory management.