IOS development: how can I trigger low memory alerts on a device?

I would test the features of my application well in low memory conditions, but this is difficult to verify. How can I trigger low memory warnings that trigger the didReceiveMemoryWarning method in my views when the application is running on a device and not on a simulator? Or how can I test my application in these possible conditions?

The reason I can’t use the simulator is because of my application using Game Center, and the invitations do not work on the simulator.

+84
ios iphone ipad
Jan 17 '11 at 19:44
source share
8 answers

To test the device, simply add code that periodically allocates large chunks of memory without freeing it up (i.e. leaks for the intended purpose). You can do this in a separate thread or in response to a timer or using any mechanism that best allows you to test and monitor the behavior of your application.

You can also create a standalone application that does something similar, and is designed to run in the background if you want to easily reuse this and / or test multiple applications.

+7
Jan 17 '11 at 20:08
source share

You can call the private method :

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

Remember to use it only for debugging, otherwise your application will be rejected.

+273
Jan 04 2018-12-01T00:
source share

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

+25
Jan 17 '11 at 19:45
source share

Using Tools, use the menu item: Tool β†’ Simulate Memory Warning.

To use the Tools in your application from Xcode, use the item "Product β†’ Profile".

+19
Jul 03 '15 at 9:18
source share

I rewrote Enzo Tran's answer in Swift:

 UIControl().sendAction(Selector(("_performMemoryWarning")), to: UIApplication.shared, for: nil) 
+10
Mar 15 '16 at 11:31
source share

Creates a menu command that will call it.

Hardware > Simulate Memory Warning from the simulator.

+7
Jan 17 '11 at 19:45
source share

Converted @ChikabuZ to fast 3:

 UIControl().sendAction(Selector(("_performMemoryWarning")), to: UIApplication.shared, for: nil) 
+7
Apr 24 '17 at 14:56 on
source share

If someone for some reason tries to do this in Swift 3 - here's 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; } 
+3
Aug 25 '17 at 13:30
source share



All Articles