title: Realm: mmap() failed: Cannot allocate memory size
body: <p>I'm using <code>Realm 2.4.3</code> in a large iPad Application for one of our customer.<br>
  Most of the time <code>Realm</code> performed very well, but now the number of Records is increasing and a get some memory issues.</p>
  
  <h3>App</h3>
  
  <ul>
  <li>Deployment Target: 10.0</li>
  <li>BaseSDK: 10.2</li>
  <li>iPad only</li>
  <li>Realm 2.4.3</li>
  <li>The App was designed to work offline, thats why we use Realm for local storage</li>
  <li>There are some methods which syncs Data from a Backend via HTTP to the Realm Database and vice versa.</li>
  <li>Number of Records in Realm Database <a href="https://i.stack.imgur.com/mW787.png" rel="noreferrer"><img src="https://i.stack.imgur.com/mW787.png" alt="Number of Records"></a></li>
  </ul>
  
  <h3>(unexpected) behaviour</h3>
  
  <ul>
  <li>the realm file size growth above 2,5GB (with compact fix from below ~500KB)</li>
  <li>realm tries to load the whole file into memory</li>
  <li>iPad Air 2 (2GB) crashes, on iPad Pro (4GB) everything works well</li>
  </ul>
  
  <h3>Issues i already read</h3>
  
  <ul>
  <li>mmap() failed: Cannot allocate memory<br>
  <a href="https://github.com/realm/realm-cocoa/issues/1159" rel="noreferrer">https://github.com/realm/realm-cocoa/issues/1159</a></li>
  <li>mmap() failed: Cannot allocate memory size<br>
  <a href="https://github.com/realm/realm-cocoa/issues/3226" rel="noreferrer">https://github.com/realm/realm-cocoa/issues/3226</a></li>
  <li>'RLMException', reason: 'mmap() failed: Cannot allocate memory size: 1207959552'<br>
  <a href="https://github.com/realm/realm-cocoa/issues/3920" rel="noreferrer">https://github.com/realm/realm-cocoa/issues/3920</a></li>
  </ul>
  
  <h3>The fix I tried</h3>
  
  <p><strong>the <code>writeCopyToURL:encryptionKey:error:</code> compact hack</strong></p>
  
  <p>I have a Singleton Object which handles all storage actions to realm,
  which has a <code>writeTransaction:</code> method which handles <code>beginWriteTransaction</code> and <code>commitWriteTransaction</code> handling. All storage actions are coming through this method.</p>
  
  <pre><code>- (void)writeTransaction:(void (^)(void))block
  {
      [self _ensureRealmThread:^{
          NSDate *startDate = [NSDate date];
          [[self _defaultRealm] beginWriteTransaction];
          block();
  
          NSError *commitWriteTransactionError = nil;
          [[self _defaultRealm] commitWriteTransaction:&amp;commitWriteTransactionError];
  
          if (commitWriteTransactionError) {
              NSLog(@"commit error: %@", commitWriteTransactionError);
          }
  
          NSTimeInterval time = [[NSDate date] timeIntervalSinceDate:startDate];
          if (time &gt; 0.5) {
              NSLog(@"WARNING: Transaction duration &gt; 0.5");
          }
  
          // i added these 5 lines to compact the database every 2000 requests 
          _writeTransactionIndex++;
          if (_writeTransactionIndex &gt; 2000) {
              _writeTransactionIndex = 0;
              [self compactDatabase];
          }
      }];
  }
  
  
  - (void)compactDatabase
  {
      NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                                 NSUserDomainMask,
                                                                 YES);
      NSString *documentsPath = searchPaths[0];
  
      NSString *defaultCompactPath = [documentsPath stringByAppendingPathComponent:@"defaultCompact.realm"];
      NSURL *defaultCompactURL = [NSURL fileURLWithPath:defaultCompactPath];
  
      // remove old
      if ([[NSFileManager defaultManager] fileExistsAtPath:[defaultCompactURL path]]) {
          [[NSFileManager defaultManager] removeItemAtURL:defaultCompactURL
                                                    error:nil];
      }
  
      NSError *writeError = nil;
      [[self _defaultRealm] writeCopyToURL:defaultCompactURL
                             encryptionKey:nil
                                     error:&amp;writeError];
      if (!writeError) {
          [[NSFileManager defaultManager] replaceItemAtURL:[self _defaultRealm].configuration.fileURL
                                             withItemAtURL:defaultCompactURL
                                            backupItemName:nil
                                                   options:NSFileManagerItemReplacementUsingNewMetadataOnly
                                          resultingItemURL:nil
                                                     error:nil];
      }
  }
  </code></pre>
  
  <p>The fix works in the storage!! The file shrinks from 2,5GB to 500KB.
  But i still got the issue that realm wants to allocate too much memory:</p>
  
  <pre><code>commit error: Error Domain=io.realm Code=9 "mmap() failed: Cannot allocate memory size: 268435456 offset: 2952790016" UserInfo={NSLocalizedDescription=mmap() failed: Cannot allocate memory size: 268435456 offset: 2952790016, Error Code=9}
  </code></pre>
  
  <p>Is there somebody with an idea to fix this ? :-)<br>
  If i missed some necessary information please leave a comment.. i'm in this issue for several days and my brain is like </p>