104

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.

0

10 Answers 10

288

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.

15
  • 6
    It appears this code accurately simulates the memory warning on the device. Thanks Enzo! Feb 22, 2012 at 9:59
  • 33
    Instead of using this in code, execute it in the debugger. Just hit pause and enter po [[UIApplication sharedApplication]performSelector:@selector(_performMemoryWarning)]
    – orkoden
    Jan 6, 2014 at 17:39
  • 20
    Rather than using po, use expr, i.e. expr (void)[[UIApplication sharedApplication] performSelector:@selector(_performMemoryWarning)];
    – Andy
    Mar 5, 2014 at 11:22
  • 14
    In the debugger, you don't need to use the workaround for not having the header: expr (void)[[UIApplication sharedApplication] _performMemoryWarning]
    – OrangeDog
    Aug 18, 2015 at 9:44
  • 3
    For anyone who is looking to do this with Swift: type expr UIApplication.sharedApplication().performSelector("_performMemoryWarning") instead of using the usual #selector
    – funct7
    Sep 20, 2016 at 1:58
29

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

enter image description here

3
  • Only way to check it on the device is to actually force it, ie by using the app for long periods of time or intensive images or what have you, will vary app to app. A suggestion might be to use an older device (3g perhaps?) instead of an iPhone 4 to help you get to the warning faster. Jan 17, 2011 at 19:56
  • Thanks, Jesse, I think I'm just gonna add some hacks to my app to simulate invites so that I can run it in the simulator and induce the memory warnings that way. Jan 17, 2011 at 20:04
  • 17
    Question was about the simulation on device.
    – KPM
    Feb 23, 2015 at 19:09
22

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

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

2
  • @OrangeDog What's happening?
    – ThomasW
    Aug 18, 2015 at 9:47
  • 3
    Well, nothing. When I use Enzo's answer views have to reload when I return to them, not so using this method.
    – OrangeDog
    Aug 18, 2015 at 9:49
14

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

UIControl().sendAction(Selector(("_performMemoryWarning")), to: UIApplication.shared, for: nil)
1
  • Thanks! Xcode warns of "no method declared with Objective-C selector", but this works nonetheless. May 31, 2016 at 20:08
14

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
2
  • Will doing something like this obfuscate the actual memory information I'm trying to view in Instruments? While I see there isn't a proper way to do this on a physical device, if my point is to see what is taking up my memory within my app it seems like this is going to skew all of the data and I'll no longer have a relative answer. I'll still see what is taking up more, but not the true proportions.
    – Jake T.
    Aug 6, 2018 at 20:21
  • 1
    this crashes immediately
    – user5306470
    Jun 17, 2020 at 17:31
10

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.

5
  • 7
    not very practical. the solution to call a private method for testing is better
    – Daij-Djan
    May 1, 2013 at 21:50
  • 5
    Not a good solution if you want to look at real world scenario. You are crippling the app due to a leak not due to memory pressure. That is bad since what you want to do is test how the app responds in high memory pressure state. On the downside its harder to figure out what the problem is a real leak or the fake one you introduced. The private method one is better for testing as given below.
    – fzaziz
    Apr 17, 2014 at 8:57
  • 2
    This answer should not be downvoted, as the second paragraph is actually what Apple says to do if you want to test your app's behavior when memory really gets low.
    – matt
    Aug 30, 2015 at 1:02
  • 1
    Answer would be improved with a code block to perform this allocation!
    – pkamb
    Dec 8, 2016 at 21:28
  • Although it takes more work to setup, this is a more realistic test than calling the private method. When there really is critical memory pressure, will your app be able to perform the actions you tell it to do? And will those actions relieve the situation or make it worse and cause iOS to terminate it? Besides, the private method does not work for testing DISPATCH_SOURCE_TYPE_MEMORYPRESSURE.
    – jk7
    Dec 28, 2016 at 17:54
7

Converted @ChikabuZ to swift 3:

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

If someone, for whatever reason, tries to do this in Swift 5 - here is how to allocate 1.2 GB of RAM:

for _ in 0...1200 {
    var p: [UnsafeMutableRawPointer] = []
    var allocatedMB = 0
    p.append(malloc(1048576))
    memset(p[allocatedMB], 0, 1048576);
    allocatedMB += 1;
}
2
  • 3
    let d = Data.init(repeating: 100, count: 1200000000) Dec 6, 2017 at 10:15
  • 2
    @Adobels you should post that as an answer :)
    – Koen.
    Feb 24, 2018 at 14:10
5

Theres a menu command that will invoke it.

Hardware > Simulate Memory Warning from the simulator.

2
  • 11
    Question was about the simulation on device.
    – KPM
    Feb 23, 2015 at 19:08
  • 2
    In Xcode 10 it's now under Debug > Simulate Memory warning.
    – Sebbo
    Dec 4, 2018 at 13:49
5

Swift 4:

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

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

    Button(action: {
        UIApplication.shared.perform(Selector(("_performMemoryWarning")))
    }, label: {
        Image(systemName: "memorychip")
    })

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.