How to simulate app termination?

While implementing the background behaviour of an app, I learned that there is a difference between the following app states:

-being suspended

-being terminated by the system

-being terminated by the user (force quit)


I can simulate app suspension by pushing the home button and moving the app to the background. I can force quit the app by double-tapping the home button and flicking the app out of the screen in the app switcher.


But how can I reliably simulate the app being terminated by the system?


I need this to test a few background features that seem to behave differently when the app is terminated by the system or while it is still suspended (and do not do anything while the app is force quit).


Thanks!

Accepted Reply

implement a fork bomb, you'll run out of resources very quickly

Replies

You could temporarily allocate a huge chunck of memory. The system will kill the app for using too much memory.

implement a fork bomb, you'll run out of resources very quickly

Have you ever tried this? I haven't managed to get this to work correctly. I tried for example something like this:


NSMutableArray* hugeMemoryArray = [[NSMutableArray alloc] init];

BOOL repeat = YES;
while(repeat) {
     Byte* hugeData = malloc(9999999);
     NSData *data = [NSData dataWithBytes:hugeData length:sizeof(hugeData)];
     free(hugeData);
     [hugeMemoryArray addObject:data];
}

which takes a very long time to fill the memory and then ends the app with a "Message from debugger: Terminated due to signal 9". applicationWillTerminate is never even called with this, and malloc-ing larger memory parts than this will give me an error as well...


Do you have any advice on how to get this to work?

Thanks!

Same problem as above: The system will kill the app completely, no "appWillTerminate" is called and the app seems to behave as if the user had force-killed the app (= push notifications received in the background won't restart the app anymore).


I seem to need something less "extreme", maybe?

Ok, sorry, you were right, this works (as the first answer above probably does as well, but I can only mark one as the correct answer, sorry.. And this is faster than allocating huge chunks of memory).


The reason why I coudn't wake the app up via push notifications was because there seems to be some kind of timeout when the app is terminated. If I wait for a few minutes it works. I also terminated the app by running memory-hungry games until the memory of the app was needed by the system, but that can't be automated as easily as this...


Thanks!

Sorry, you are right, it does work (see my other answer below...).Thanks!

For those that have the same problem as I have, here are a few more hints how you can invoke the "Terminated due to signal 9".


1. By code:

kill(getpid(), SIGKILL);

This will immediately terminate the app due to signal 9 (so much faster than the above methods)


2. Manually:

Hold the power button until the "slide to power off" appears, then hold the home button until the main screen appears. This will terminate all suspended apps.


I still haven't found a good way to terminate the app due to memory pressure, except running a few memory-hungry games (which isn't that easy to automate...).

Try using a separate app (in the background, running with extra time requested) to allocate memory blocks in an infinite loop, in not too massive sized chucks, using a periodic/repeating NSTimer or CADisplayLink that allows enough time for a few frames of time between each allocation.

I actually did try this once. My "memoryEater" app filled the memory of the app until it had around 650 Mb ram (took quite a while) and then the system killed the "memoryEater" app, but not the other one. Maybe I should try this with multiple apps that all fill the memory at the same time?

I found here http://pinkstone.co.uk/how-to-kill-your-app-when-it-enters-background-mode/ the easiest way to kill background app.

Just set

Application does not run in background YES

in .plist file. And app will be killed every time it goes to background

Do you know how to do (2.) on an iPhone X? There's no home button and since i don't know what the shortcut is called, i can't look it up.

2) doesn't work anymore anyway. I just tried it on my iPhone and all the background tasks were still there. Apple must have changed that behavior since that information was posted ten months ago.

For this question, I found this article by Quinn (Apple Developer Relations, Developer Technical Support, Core OS/Hardware) very helpful:

https://developer.apple.com/forums/thread/14855