无法分配这个内存大小?😂😂😂
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"
应该是你要存储的数据对象未被正确初始化
Translation: _It should be the data you want to store the object is not properly initialized_.
Hey @Jinxiansen! Thanks for reaching out. Would you be able to share a sample project that reproduces/demonstrates the problem you're experiencing? Cheers.
Hello! It looks like the file you are trying to open is ~3 GB in size. iOS won't let any application mmap that much memory. Can you tell us more about your Realm file and use case? Is this a problem you are seeing on user's devices? Or is it a problem you are seeing when developing your app?
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.
真让人头疼。
Translation: _Really a headache._

@Jinxiansen: 試一下用 Realm Browser 開啟 App 中的 Realm file
Translation: _Are you able to download the Realm file and use Realm Browser for Mac to open it?_
看看是不是这原因
Translation: _See if this is the reason_.

@Xiahaiquan We stored contents using these types. For instance, in one Realm file. Simply doing compact reduce the file size from 671MB to 295KB.
While I look at @Jinxiansen case, I have the same error message of bottleneck at memory size: 3221225472 I'm worried that there is a corruption to the Realm file, and thus not able to open it again. Since I don't have the file on hand, would appreciate if @Jinxiansen can try it out.
Thanks
@harryworld 是在 iPhone 真机上面的数据,无法使用Realm Browser 打开啊
Translation: _@harryworld is the real machine in the iPhone above the data, can not use Realm Browser to open ah_.
@Xiahaiquan 我是每秒都会对 Realm 更新一个 model 的,可能因为写入太频繁。
Translation: _@Xiahaiquan I will update a model of Realm every second, probably because the write too frequently_.
@harryworld 是在 iPhone 真机上面的数据,无法使用Realm Browser 打开啊。
Translation: _@harryworld My data is in real device, cannot open using Realm Browser_.
@Xiahaiquan 我是美妙都会对 Realm 更新一个 model 的,可能因为写入太频繁。
Translation: _@Xiahaiquan I am writing data to Realm every second, it may be due to frequent write transactions._
@Jinxiansen I am having the realm file in device as well, what I do is to enable iTunes File Sharing, so that I can download the realm file in iTunes.
File Sharing allows you to read the app's Documents folder. In case you are not saving realm into default location, you can use FileManager to copy the item back to Documents.
To view the File Sharing, here are the steps:
DemoSwift on the left Apps menu, and you will see the file backup. The file is named as a date with extension .realm@harryworld 在手机上它实际上只占用了5MB 左右的空间,但是在 Xcode 控制台却提示是 3489660928 ,大于3G 的容量,我想应该是 Realm 分配了这么大的逻辑空间,但是实际上没有这么大的,你说的用 iTunes 打开这个办法我找不到的.realm 。
面前暂时的解决办法只能减少写入次数了。
Translation: _@harryworld on the phone it actually takes up only about 5MB of space, but the prompt is 3489660928 in the Xcode console, greater than the capacity of 3G, I think Realm should be allocated such a large logical space, but in fact not so big , You say with iTunes to open this way I can not find. Temporary solutions in front of only reduce the number of writes._
In our case, the disk size can be more than 3GB
@harryworld 忧桑啊。
https://github.com/realm/realm-cocoa/issues?utf8=✓&q=Cannot%20allocate%20memory
之前有很多人提过这个问题,但是我没看懂有什么有效的解决办法。
大兄弟有什么高见撒?
Translation: _@harryworld worry mulberry ah.
Https://github.com/realm/realm-cocoa/issues?utf8=✓&q=Cannot%20allocate%20memory
Many people have mentioned this before, but I did not understand what an effective solution.
Brothers have any ideas Takao?_
Hi all, it's unclear what you need from us to help you at this point. Are you looking for ways to avoid having such large Realm files?
In general I think it would help you to know that setting breakpoints in places like RLMCacheRealm or in realm core's SlabAlloc::resize_file() is a great way to help debug why you might be creating lots of separate Realm instances at different versions, or why Realm might be aggressively growing the database file.
@jpsim Thanks for reply. In practice, I would like to know why it is happening and how to prevent the realm file getting bloated. No one would want their users waking up one day and crashing the app in every launch. Will try your tip to debug why it's growing fast.
In practice, I would like to know why it is happening and how to prevent the realm file getting bloated.
Have you read our docs on File size & tracking of intermediate versions? That might help explain things.
@Jinxiansen: Do you require anymore assistance?
@istx25 似乎无人能够解决这个问题,想关就关了吧。
我在 issues 里搜索 “ Cannot allocate memory size:”,这个问题多次被人提及,然而似乎最后的解决办法都是不了了之,这让我感到有点失望,也对贵团队的能力表示怀疑。
Have you read our docs on File size & tracking of intermediate versions? That might help explain things.
@jpsim Does it mean I should call realm.invalidate() whenever I don't need to access the results anymore?
For instance, I may retrieve some objects and copy them into array. Once the copy is done, I should already invalidate realm instead of reusing it?
@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 [email protected].
@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.
Hi @harryworld, I'm just following up on @austinzheng's last comment. Were you able to adjust your code to avoid holding on to too many older Realm versions?
@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
Most helpful comment
@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 [email protected].