83

In a certain (consistent) point when my app is running, I consistently get the xcode error message

Terminated due to memory error.

I cannot find the code causing the error, but I can tell what code is near the error (using breakpoints).

The error is caused directly after returning a certain cell in my implemenation of the

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

UITableViewDataSource delegate method. I can confirm that it is returning a valid UITableViewCell, but I think that explaining and posting that entire method would be a waste of your time. However, I suspect it might be caused by a rapid, massive allocation of memory.

It definitely says Terminated due to memory error, not memory pressure.

I would like to know what is message really means. Also, is there any way to debug this message? No crash report is generated.

I am using ARC and iOS 7.

Raptor
  • 53,206
  • 45
  • 230
  • 366
Andrew
  • 15,357
  • 6
  • 66
  • 101
  • 1
    I saw the same error in Xcode. My app was crashing because I was creating a super-tall UIWebView (greater than 20000 pixels). I was never able to find a crash report, nor figure out a way to get memory warnings before the crash. – Heath Borders Oct 17 '13 at 21:40
  • @HeathBorders I am using a UIWebview as well. That is very interesting. I will have to check the height on mine. Thanks! – Andrew Oct 17 '13 at 23:03
  • This is kind of a common error with these components. The possible causes are: somehow you are generating too many cells (check your calculations), your cell sizes are wrong (as in dividing by 0 generating a nan which is kind of an infinite number when making the size). or incurring on an infinite loop. – Pochi Nov 11 '13 at 07:37
  • @HeathBorders Now I am interested to know what device you were testing on? – Andrew Nov 11 '13 at 11:37
  • I was testing on an iPad3. Also, my `UIWebView` wasn't just super-tall, it was also super-wide (30000px). – Heath Borders Nov 11 '13 at 15:33
  • @HeathBorders This is very interesting. Everyone that participated in this post has been testing on iPad 3rd gen. Coincidence? Has anyone been able to reproduce the error on another device? I will try if I get a chance. – Andrew Nov 11 '13 at 20:53
  • Related: http://stackoverflow.com/questions/19482396/how-i-can-solve-terminated-due-to-memory-error – Andrew Jan 07 '14 at 04:16
  • Related: http://stackoverflow.com/questions/19711184/termination-due-to-memory-pressure-with-known-cause-unknown-solution – Andrew Jan 07 '14 at 04:17
  • I am running with the same problem :| – JgdGuy Nov 06 '14 at 12:12
  • same problem with iphone 6 + for MKMapView. how can I solve it? it works fine in 4s – user1960169 Nov 18 '15 at 05:48
  • same error iPhone 6+ – Dude Jan 25 '16 at 17:49
  • Same error in iPhone 6 – Ramakrishna Feb 13 '16 at 13:31

13 Answers13

60

I was getting this error and could not understand what was wrong.

After some searching I found out that i forgot to disable zombies.

To disable do the following:

Select edit scheme

Deselect "Enable Zombie Objects

Slipp D. Thompson
  • 33,165
  • 3
  • 43
  • 43
zirinisp
  • 9,971
  • 5
  • 32
  • 38
  • 6
    When zombies are enable everything is affected as objects are not deallocated. This fills up the memory very fast in the case of a table view where a lot of thing are being allocated and deallocated. Definition of Enable Zombies: Replace deallocated objects with a “zombie” object that traps any attempt to use it. When you send a message to a zombie object, the runtime logs an error and crashes. You can look at the backtrace to see the chain of calls that triggered the zombie detector. – zirinisp Jun 03 '15 at 06:32
  • 2
    Still, I'm facing the problem. Please guide me. – I'm a Learner Apr 07 '17 at 07:25
  • How do you disable zombies? – Sam Apr 23 '19 at 00:24
11

I was faced the same issue.("Terminated due to Memory Error") I had tried the above all answers but nothing work for me. Then i debug my code and found a for loop is running for infinity time with allocating memory to a array with nil value every time.its use 300+MB so it give this error

Thanks.

7

I had exactly same issue. I thought it caused my program had memory leak or using too much memory. I use instruments and used allocating profile and program works fine. Also I ran program by device long enough, and it also works fine.

I also using iPad 3rd Gen for debugging, it might be causing because of that slow of the device or bug, it it seems like just Xcode and running from Xcode problem. Not the problem of memory leak or allocation.

If you make sure with instruments and running app on device itself and work

Amit Srivastava
  • 1,105
  • 9
  • 18
Tomohisa Takaoka
  • 885
  • 5
  • 16
  • Hmmm. Everyone that participated in this post has been testing on iPad 3rd gen. Coincidence? – Andrew Nov 11 '13 at 20:51
  • I only use iPad 3rd gen so I am not sure yet. Today I have ordered iPad mini retina, so I can provably answer this next week ;) – Tomohisa Takaoka Nov 13 '13 at 01:54
  • Just encountered this on an iPhone5S. Just added Google's tracking code and had to reduce from arm64 to arm7/7s as Google's slooooww at building arm64. I'd assumed this was an arm64/7s thing. Certainly not just an iPad 3rd gen issue. I had 'error in __connection_block_invoke_2' which separate searches tells me might be related to downloading too much and OOMing. Shame about the lack of stacktrace though.... – Peter Dec 16 '13 at 17:18
  • I'm encountering it on an iPad mini 2nd-gen ([iPad4,4](http://www.everymac.com/systems/apple/ipad/specs/apple-ipad-mini-retina-2nd-gen-a1489-wi-fi-only-specs.html)). – Slipp D. Thompson Jul 15 '14 at 23:19
  • 3
    I have similar issue when downloading whole bunch of images using NSURLSession. Simulator and standalone device(iPhone 6) works fine. In instruments, allocation diagram also looks smooth. Don't crash either. Problem occurs when I have device connected to xCode. No matter build configuration (Debug or Release). Always ends with mystery "Memory error". – Neru Jan 14 '15 at 19:32
  • I'm encountering it in iPhone6. Working fine on simulator but when i run my app in iPhone6 shows the memory error and also working fine in other devices like 5, 5S etc.. – Ramakrishna Feb 13 '16 at 13:21
  • In my Universal app, I'm getting his problem with iPhone device 5s,6. But the same is working fine with iPad. So please suggest me for the same. – I'm a Learner Apr 07 '17 at 10:46
6

I was using Tesseract for OCR and when my target text got scanned, a GIF was supposed to play. When the GIF started to play, there was a memory spike, from 70-80MB to 450MB. The problem was that GIF was taking too much memory, the app would crash and Xcode would show that message. So I removed the concerned imageView from the superview of the ViewController.

imageView.removeFromSuperview

After this, the app would still spike to 450MB but then immediately release and come down to 40MB

Yash Tamakuwala
  • 1,789
  • 1
  • 24
  • 33
5

Restart device worked for me. “Terminated due to memory error” message stopped to appear.

Ofir Malachi
  • 1,145
  • 14
  • 20
3

I got this error because I was adding full size photos to a collectionView cell sized 40x40. Read the 2 comments under the question from @matt. I had to scale down the images before adding them to the cell.

Lance Samaria
  • 17,576
  • 18
  • 108
  • 256
2

Edit the scheme, under Diagnostics you'll find several options to guard allocations. Refer to the documentation for details.

Question is: does it really say "terminated due to memory error" and not "pressure"? Check in instruments if your app is running low on memory, if the app does seem to run low on memory then that's what you should focus on regardless what the exact message is.

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
1

I had similar issue in Xcode 11.3 wherein camera was getting calling every-time we press on try again button. If these is done multiple times then crash happens.

This was fixed when we disabled Zombie objects. Below are steps:

  1. Tap on project name present in top left. This will show list of targets present in project.
  2. Tap on Edit scheme

Screenshot-1

  1. Select Run option -> Diagnostics -> Uncheck Zombie Objects.

Screenshot-2

Now, run your project. It should work fine.

Jayprakash Dubey
  • 35,723
  • 18
  • 170
  • 177
0

With Xcode 11 it started my project in Debug mode. I am doing some image recording/editing/returning to the user and that is not something you can use in Debug. Once I turned to Release mode, all went well.

impression7vx
  • 1,728
  • 1
  • 20
  • 50
0

I've faced this kind of issue due to inattentiveness.

I've been calling a function, which adds imageView as subview in:

override func layoutSubviews() {
    super.layoutSubviews()
}

This caused a huge memory usage, so be attentive to this sort of things!

0

The thing that I noticed is that when I run my app on the device through cable and leave it idle for a long time I will also get that error.

Apple do address this and it just might be that it feels like the app is idle and just kills it.

Cyrus Zei
  • 2,589
  • 1
  • 27
  • 37
0

In my case it was a corrupted Image from the API which raised my memory from 100MB to 4.5 GB due to processing size it took to display on screen!

Marwan. KA
  • 29
  • 3
0

An infinite loop was the cause of the memory leak for me. I could see the memory in Xcode rising to 1,6 Gb at which point the app crashed. The app's memory usage should be in mb and not gb and it should be relativly stable. If it rises quickly, say 100 mb a second, there's definitely something wrong. If none of the suggestions above have worked, you should 1. check the memory usage of the app, and 2. if the usage is too high, look for loops you've recently added.

Joakim Sjöstedt
  • 824
  • 2
  • 9
  • 18