iOS Development: How can I induce low memory warnings on device?

IphoneIpadIos

Iphone Problem Overview


I'd like to test my app functions well in low memory conditions, but it's difficult to test. How can I induce low memory warnings that trigger the didReceiveMemoryWarning method in my views when the app is running on the device, not the simulator? Or what are some ways I can test my app under these possible conditions?

The reason I can't use the simulator is my app uses Game Center and invites don't work on the simulator.

Iphone Solutions


Solution 1 - Iphone

You can call the private method:

[[UIApplication sharedApplication] performSelector:@selector(_performMemoryWarning)];

Just remember to use it on debug only, or else your app will get rejected.

Solution 2 - Iphone

The iOS Simulator's Simulate Memory Warning menu item allows you to simulate a memory warning.

enter image description here

Solution 3 - Iphone

Using Instruments, use the menu item: Instrument -> Simulate Memory Warning.

To use Instruments on your app from Xcode, use the Product -> Profile menu item.

Solution 4 - Iphone

I've re-written Enzo Tran's answer in Swift:

UIControl().sendAction(Selector(("_performMemoryWarning")), to: UIApplication.shared, for: nil)

Solution 5 - Iphone

If someone, for whatever reason, tries to do this in Swift 4 - here is how to allocate 1.2 GB of ram.

let d = Data.init(repeating: 100, count: 1200000000)
  • This is helpful to trigger a warning alert in other apps

Solution 6 - Iphone

To test on a device, just add some code that periodically allocates large chunks of memory without freeing it (i.e. leak on purpose). You can do this in a separate thread, or in response to a timer, or using whatever mechanism that best allows you to test and observe the behavior of your application.

You might also choose to create a separate app that does something similar and is designed to run in the background, if you'd like to easily reuse this and/or test with multiple applications.

Solution 7 - Iphone

Converted @ChikabuZ to swift 3:

UIControl().sendAction(Selector(("_performMemoryWarning")), to: UIApplication.shared, for: nil)

Solution 8 - Iphone

Theres a menu command that will invoke it.

Hardware > Simulate Memory Warning from the simulator.

Solution 9 - Iphone

If someone, for whatever reason, tries to do this in Swift 3 - here is how to allocate 1.2 GB of ram.

   for i in 0...1200 {
      var p: [UnsafeMutableRawPointer] = []
      var allocatedMB = 0
      p.append(malloc(1048576))
      memset(p[allocatedMB], 0, 1048576);
      allocatedMB += 1;
   }

Solution 10 - Iphone

Swift 4:

UIApplication.shared.perform(Selector(("_performMemoryWarning")))

Can execute the above in response to an event/notification.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBeachRunnerFredView Question on Stackoverflow
Solution 1 - IphoneEnzo TranView Answer on Stackoverflow
Solution 2 - IphoneOle BegemannView Answer on Stackoverflow
Solution 3 - IphoneThomasWView Answer on Stackoverflow
Solution 4 - IphoneChikabuZView Answer on Stackoverflow
Solution 5 - IphoneBlazej SLEBODAView Answer on Stackoverflow
Solution 6 - IphoneBinaryStarView Answer on Stackoverflow
Solution 7 - IphonekindaianView Answer on Stackoverflow
Solution 8 - IphoneDaniel A. WhiteView Answer on Stackoverflow
Solution 9 - Iphoneph1lb4View Answer on Stackoverflow
Solution 10 - IphoneVishal ChaudhryView Answer on Stackoverflow