Stop Xcode Automatic Termination

Lion and now also Mountain Lion have an annoying tendency to helpfully terminate applications for you. I spotted a tip by Matt Neuburg over on the TidBITS blog this week which offers a solution that I think is worth filing for future reference. First though let me describe the problem as it most often impacts me with Xcode. I should say this problem and the solution are not specific to Xcode I just happen to notice it most with Xcode.

The Problem

My ageing Mac can take a little while to start Xcode which means that I often find myself switching to another application whilst I am waiting. Inevitably I get distracted and it can be a minute or two before I attempt to switch back to Xcode to open a project and do some work. Alternatively I may be using Xcode, close a project so it no longer has any active windows and switch to another application. In both situations if I am away from Xcode for more than a minute or two there is a good chance that Xcode will no longer be visible in the dock or in the application switcher with Command-Tab.

Automatic Termination

At first this behaviour is a little confusing and you may even be tempted to assume Xcode has crashed. Actually it is not a bug but a feature known as automatic termination introduced with Lion and still present in Mountain Lion. It allows OS X to terminate applications that it decides are no longer in use to reclaim resources. In theory this is optional for an application but unfortunately it seems that Xcode has decided to opt-in.

This is a familiar situation for an iOS application. Apple has always made it clear that inactive iOS applications can expect to be terminated and should be designed with that in mind. The iOS task switcher also does a good job of hiding which applications are actually running so in theory a user may never know when switching between applications. In practise it is not always totally invisible to the user but it seems to be a reasonable compromise on an iOS device.

Automatic termination of applications on a desktop class machine seems to be a much less reasonable compromise. OS X has access to much greater resources and it seems unnecessary to start terminating applications without user intervention. Even worse is the fact that the application disappears from both the dock (unless you opted to keep it there permanently) and from the application switcher forcing you to relaunch it.

To add further insult to injury the application is not even really terminated. In my case whenever I spot that Xcode has disappeared a quick ps from the command-line or checking activity monitor will reveal that the process is actually still running. This is a further “feature” where OS X may decide to keep the process memory resident in case you decide to relaunch it. In fact since on my Mac the initial Xcode load takes several seconds this difference in launch time is very noticeable. After OS X has helpfully “terminated” Xcode for me it reappears almost instantly when I relaunch it. Of course if OS X had not “terminated” it for me in the first place I would not need to relaunch it so the benefit is dubious…

The Solution

I recognise that opinion is still divided on many of the iOS style features that Apple has brought to Lion and Mountain Lion. If you are not convinced by the benefits of OS X automatically terminating applications here is the command to disable the feature:

defaults write -g NSDisableAutomaticTermination -bool yes

I found I had to reboot for the change to take effect but that was most likely because Xcode was still running as I previously explained. A logout or manually killing the Xcode process hiding in the background would probably have worked as well.