0

I am trying to simulate memory warning in ios project. But when used memory reaches to around 600 MB I am getting "Message from debugger: Terminated due to memory issue #1" on debugger. Same is working on objective C. Please refer this question for more details. I have tried by both ways as described by @Martin R.

I am using a timer to allocate 1MB memory in every 1 second to allocate all the available memory in iPHone and generate memory warning.

Here is the objective C code using malloc

p[allocatedMB] = malloc(1048576); memset(p[allocatedMB], 0, 1048576); allocatedMB += 1;

user1101733
  • 258
  • 2
  • 14

1 Answers1

0

The simulator has a command for this: Hardware > Simulate Memory Warning (Cmd-Shift-M).

On the device, try

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

(and don't forget to remove this code when submitting to the App Store)

Gereon
  • 17,258
  • 4
  • 42
  • 73
  • I want to generate it on device. – user1101733 Feb 11 '18 at 20:33
  • Updated my answer – Gereon Feb 12 '18 at 10:59
  • Thanks, But I already tried it. Now want to allocate memory iteratively and check app behaviour. That's why trying to write this. It will not crash the but if it happens in real time app would crash after 1-2 warnings. – user1101733 Feb 12 '18 at 14:21
  • I am trying to figure out why this code is working in objective c and crashing in swift. Check here if you find something - https://github.com/Split82/iOSMemoryBudgetTest – user1101733 Feb 12 '18 at 14:23