So, let’s just say that you happen to be a Mac user, and browse the web on Safari, only to have this frustrating problem where some of your favorite websites are mysteriously starting to look like this when they load:
What’s worse, once it happens to that one site, it starts happening on every site you navigate to. The only way to stop this silly behavior is to quit Safari (sometimes a Force Quit is necessary), then relaunch and go back to what you’re doing… until it starts failing on you again.
It seems like only certain specific, image-heavy sites cause Safari to act up, making you wonder if the problem is Safari, or a badly-coded website. What’s worse, this problem (typically) doesn’t exist on FireFox or Opera.
If you look in the browser’s Activity/Debug window, you might see error messages popping up like crazy in red, that read something like:
POSIX error: Too many files open
Sound familiar? This problem can be really annoying for some of us who like Safari and some of its features, and would really rather not have to switch browsers.
But don’t fret. There IS a solution.
The problem isn’t directly Safari, and while the the websites in question probably could use some better coding, the fault doesn’t exactly lie on that end, either. As it turns out, OS X comes by default with a configuration that is rather restrictive and overprotective, and Safari sometimes gets a bit carried away in the number of resources it calls up at once. And when they start fighting, you lose.
For the tech-minded: the problem lies in a configuration file for launchd and launchctl, the two main gatekeepers for apps launched on a Mac. A config file specifies how many files and processes are allowed to be open at once. And on most macs, the settings are set way low.
Before we proceed, It should be pointed out: if you’re not having any issues at all with Safari, then you probably shouldn’t be messing with any settings, because they’re likely working just fine for you. If however, you’re frustrated with seeing broken pages and apparent crashes by the web browser, then the following steps may assist you.
To begin: You will need to open up a Terminal Window on your mac, and enter the following command:
sudo launchctl limit
The output will look something like this (possibly with slightly different values):
The values of interest here are “maxproc” and “maxfiles.” These are soft and hard limits for how may processes the OS will allow applications to spawn at once, and the maximum number of files that OS X will allow to be open. In my case, I have “soft limits” of 266 max processes, and 256 maximum open files. For most everyday uses this appears to be enough, but is a little too conservative when you’r loading up graphic intensive websites or applications. Safari appears to be kept on a very short leash, and chokes when it hits these “soft” limits.
If you find yourself in the same boat, there is a way to bump up these limits. We don’t want to get too liberal with these limits, as they do serve an important function in making sure that a runaway application doesn’t take the whole system down. But, most modern macs, with multiple CPUs and oodles of RAM, can definitely juggle more than the limits shown here.
Changing the values involves issuing a couple of commands into the terminal. First, make sure you have the old values written down. You might even want to take a screenshot of the original values, as I did above. This assures that you can go back to these values if the changes cause other, more undesirable problems.
Now, we’re ready to modify things a bit. Let’s start with max processes, issuing this command into the terminal:
echo "limit maxproc 1000 2000" | sudo tee -a /etc/launchd.conf
Note: you will be asked for a password, as an “administrator” user must make this change.
This modifies (or creates, if it doesn’t exist) a configuration file to let OS X know that we want higher limits (in this case, 1000 soft, 2000 hard limit) to this parameter.
Next is maxfiles:
echo "limit maxfiles 4096 unlimited" | sudo tee -a /etc/launchd.conf
Once these changes are done, the mac must be rebooted for them to take effect.
This seems to have resolved the problem for me, and hopefully will solve the issue for the many others who have complained of similar problems, and not yet found a solution.