If you are getting an error saying that you cannot preview this file (while using SwiftUI), then perhaps like my experience, a reboot might fix it. That’s the short version to possibly get you going before reading on.
I created a new project today and switched the default ContentView.swift to use an iPad Pro 11 inch for the preview. An error appeared in right pane saying “Cannot preview in this file – Unable to boot device due to insufficient system resources”. Clicking the diagnostics button provided the following:
HumanReadableNSError: Unable to boot device due to insufficient system resources.
Please see Simulator Help for information on adjusting resource limits.
==NSLocalizedFailureReason: The current system settings are not sufficient to allow booting additional simulators: maxFiles: 12288, openFiles: 11669, enforcedFilesBuffer: 1868
As mentioned, a reboot fixed the issue. It seems that Apple last recognised this happening back in 2017 in the Simulator Help section on the Apple help site that gives some recommendations on how to modify the number of processes and number of open files.
Although the article, found here, relates to the iOS simulator, it appears from errors that the same might apply to SwiftUI previews as well.
The recommendations are as follows:
- Find the current process and file limits.
- Find the total number of running processes.
- Increase the maximum number of processes (until the next restart).
- Increase the maximum number of open files (until the next restart).
Please note that if you decided to do steps three and four, something you do need to watch out for is being careful not to set the limits too low. I’ll raise my hand here. I did this and my MacBook Pro 2019 almost completely froze. In other words, use sparingly, and only if you actually pay attention to the numbers you got in step one. The good thing is that the changes you might make in steps three and four are just temporary and a reboot will set them back to normal settings.
What does launchctl limit maxproc do?
launchctl limit maxproc <per user limit> <total limit>
The command above is used in step three to change the number of concurrent processes that the CPU can handle at the same time. If the machine is under load, more concurrent processes are needed. The maxproc limit prevents the CPU from doing too much at the same time, although we might argue that there is plenty of space to do more with a modern machine.
Sometimes though, you might hit this limit. A reboot clears out the processes, but sometimes you might just need more without a reboot which is when you can set the maxproc manually.
What does launchctl limit maxfiles do?
launchctl limit maxfiles <per process limit> <total limit>
Just like maxproc limits the number of processes, maxfiles limits the number of files that are open. The same warning applies when making changes with the command above. If you set it too low, you might cause your system to freeze.
Steps One and Two
Before making configuration changes, you need to make sure you follow steps one and two in the article at Apple and get the actual number of processes in use, and the actual number of files open. This will allow you to make a good judgement on where to set the limits.
Making the Changes Permanent
It is possible to make the changes permanent by making changes to ~/.bash_profile and including the commands there, but as a reboot fixed my problems, at least for now, I haven’t read up about any caveats to doing this, so I don’t feel to make more suggestions at this point in case I make an error on your side.