Malloc Error "Can't Allocate Region" Failed with Error Code 12. Any Idea How to Resolve This

Malloc error can't allocate region failed with error code 12. Any idea how to resolve this?

Googling will reveal quite a few tutorials on using instruments to understand what is going on with your memory:

How to debug memory leaks: (tutorial)

http://www.raywenderlich.com/2696/how-to-debug-memory-leaks-with-xcode-and-instruments-tutorial

And another:

Finding Obj-C memory leaks (video)

http://www.youtube.com/watch?v=R449qEuexNs&feature=related

*There are many similar questions on stackoverflow you might benefit from.

Can't allocate region malloc error when running millions of iterations of loop

The problem seems to be that a lot of autoreleased objects fill up the memory waiting to be released. So a solution is to add your own autorelease pool scope to collect autoreleased objects and release them sooner.

I suggest that you do something like this:

for (NSString *currentEightLetterWord in [eightLetterWordsDictionary allKeys]) {
@autoreleasepool {
for (NSString *currentWord in [allWordsDictionary allKeys]) {
}
}
}

Now all autoreleased objects inside @autoreleasepool { .. } will be released for each iteration of the outer loop.

As you see ARC might save you from thinking about most reference counting and memory management issues but objects can still end up in autorelease pools with ARC when using methods that directly or indirectly create autoreleased objects.

An alternative solution that I don't really recommend is to try to avoid using method that will use autorelease. Then does:contain: could awkwardly be rewritten to something like this:

- (BOOL) does: (NSString* ) longWord contain: (NSString *) shortWord {
NSMutableString *haystack = [longWord mutableCopy];
NSMutableString *needle = [shortWord mutableCopy];
while([haystack length] > 0 && [needle length] > 0) {
NSMutableCharacterSet *set = [[NSMutableCharacterSet alloc] init];
[set addCharactersInRange:NSMakeRange([needle characterAtIndex:0], 1)];
if ([haystack rangeOfCharacterFromSet:set].location == NSNotFound) return NO;
haystack = [haystack mutableCopy];
[haystack deleteCharactersInRange:NSMakeRange(0, [haystack rangeOfCharacterFromSet: set].location)];
needle = [needle mutableCopy];
[needle deleteCharactersInRange:NSMakeRange(0, 1)];
}
return YES;
}

(XCode 7 + iOS 9 + iPhone 4s/iPhone5 only) issue: malloc: *** mach_vm_map(size=1048576) failed (error code=3)

Well, I guess I have finally found the issue: Its definitely memory issue, but I had to search where. I found that I am using two third party labels namely: CXAHyperlinkLabel and STTweetLabel. When I removed those, my app just work fine!! The issue has been resolved but still I am confused why it did work (and still works) great in iOS 8.4 and eats up more than 1.5 GB of memory in iOS 9.0 and above!! If it has some issues with memory (I found some and fixed already, still), why it did work with iOS 8.*.
So, my advice to any fellow who are having such issues, I recommend using UITextView for links (I did it and its nice replacement).

Xcode - Malloc_Error in Release and Distribution configuration

Probably because your simulator is able to allocate ~500Mb of memory while your iPhone is not able to do it. I think you should rethink what you are doing

  • do you really need so much memory?
  • isn't it just a calculating bug? (maybe a wrong sizeof or whatever)
  • in any case this is really too much data to be handled


Related Topics



Leave a reply



Submit