124

I seem to have a problem with performance of "sbt test" (which includes looking up localhost names/IP addresses) after upgrading to macOS Sierra. On a previous version of OS X it took about 40-50 seconds to finish. macOS Sierra times are much higher than that. Last run I did was around 15 minutes. Compile times are about the same as on 'El Capitan'.

I'm the only one from my team to try this new macOS so I can't tell if it's only happening on my mac or is it a universal issue.

My colleague had a similar issue on Ubuntu and it was related with random number generation slowing down the tests - Slow service response Times : Java SecureRandom & /dev/random

Unfortunately, that didn't work for me. Originally I tried that on JDK 8u54 and then tried updating to JDK 8u102 and that didn't help as well.

P.S. I'm running Macbook Pro Mid-2015 2.8GHz i7, 16GB ram, 1TB SSD.

2

8 Answers 8

381

I had the same problem. Tomcat went from 15 seconds to 6 minutes to initialise spring context after the upgrade... disabling csrutils didn't solve the issue for me.

I solved the problem by adding my Mac hostname (i.e. Macbook.local, or whatever your Mac is called) on the /etc/hosts file mapped to the 127.0.0.1 address as well as the ::1 like this:

127.0.0.1   localhost mbpro.local
::1         localhost mbpro.local

If you're interested you can find some details on the issue and solution here: https://thoeni.io/post/macos-sierra-java/

On the post I also link to a github project to help troubleshooting the issue and validating the solution.

The problem is related (I believe) on how the localhost name resolution works and how the java.net.InetAddr class is retrieving the addresses. I verified with few colleagues and apparently it doesn't happen to everyone who upgraded to Sierra, but I'm still investigating the roots of this change.

The solution anyway was the same that antid0te implemented and worked immediately.

15
  • Same issue... And I can confirm that disabling SIP didn't work for me as well.
    – borges
    Sep 26, 2016 at 20:22
  • Great it works, you can check with the github project github.com/thoeni/inetTester
    – christmo
    Oct 11, 2016 at 16:59
  • 27
    Found it! Its not mac name.. Its hostname. By typing "hostname" in terminal can give correct hostname.
    – VK321
    Oct 22, 2016 at 6:52
  • 1
    Still seeing this on OSX 10.14.2, years later. Thanks so much for the fix. Jan 24, 2019 at 0:20
  • 2
    macOS Mojave 10.14.4, still a problem. Appending hostname after localhost to IPv4 and IPv6 loopback lines (127.0.0.1, ::1) still verified fix!
    – Draculater
    May 10, 2019 at 13:27
51

Correct answer: Jvm takes a long time to resolve ip-address for localhost

For the lazy people:

sudo sed -i bak "s^127\.0\.0\.1.*^127.0.0.1 localhost $(hostname)^g" /etc/hosts
sudo sed -i bak "s^::1.*^::1 localhost $(hostname)^g" /etc/hosts
sudo ifconfig en0 down
sudo ifconfig en0 up
5
  • 4
    Awesome! This boosted my minecraft launching time from 12 seconds to 0.5 seconds.
    – Bob
    May 7, 2017 at 0:50
  • @camikiller I'm seeing 2 different values for hostname vs Sharing > Computer Name, please clarify which one to use ?
    – vikramvi
    Jul 24, 2017 at 15:06
  • Better solution is to upgrade your MacOS @vikramvi
    – camikiller
    Jul 24, 2017 at 15:58
  • @camikiller later I did per apple.stackexchange.com/questions/66611/…, to have same name but still it's not working. Do I need to do some other settings in Sharing ?
    – vikramvi
    Jul 24, 2017 at 18:48
  • @vikramvi Try editing manually your /etc/hosts file and add the 2 hosts
    – camikiller
    Jul 25, 2017 at 19:11
7

I have the same problem. My spring-boot application take 60 seconds to start on Sierra against 25 seconds on Yosemite.

While debugging, I realized that the problem comes from InetAddress.getLocalHost(). I changed my host file to add my hostname for 127.0.0.1 and :: 1 and now the application starts as fast as before.

1
  • 2
    Hi, I found this high level explanation if it helps (but still it's not clear what changed in the MacOS Sierra release): "When you request a hostname, the JDK resolves it to IP addresses. It then tries a reverse lookup of those addresses and checks that at least one of the results maps back to the input host name. It's this reverse lookup that's slow. The slowness isn't limited to the JVM. Anything on the OS that tries to perform such a reverse lookup will be slow without appropriate configuration in /etc/hosts." (from github.com/spring-projects/spring-boot/issues/7087)
    – thoeni
    Oct 11, 2016 at 20:59
4

Enabling e.g. System Preferences > Sharing > Remote Login, results in the hostname being automatically assigned an IP address.

As people are seeing issues after the upgrade, it makes sense to assume that 10.12 changed how the hostname is resolved, i.e. at least with 10.11 the hostname is always resolved, while with 10.12 it is resolved only if a service is enabled in System Preferences > Sharing (someone with 10.11 could confirm this).

3
  • I've 10.12.5 but your solution didn't work. which version do you have ?
    – vikramvi
    Jul 24, 2017 at 18:49
  • @vikramvi I have 10.12.6 and for my installation things still work as described. With a standard /etc/hosts file and sharing disabled, ping $(hostname) fails with an unknown host error. With sharing enabled the host name is resolved to my VLAN's IP. As for Java (8u141), calling InetAddress.getLocalHost() takes 5s in case of sharing disabled. With sharing enabled the execution time drops to 13ms.
    – jurajw
    Jul 27, 2017 at 0:24
  • This does work (though is really weird), basically even if "Remote Login" was already checked, uncheck it, then check it again. Things start working again. You have to do it once/boot LOL. Note also that if you've ever once killed "mDNS" process (earlier), you'll need to do this trick to bring back .local addresses at all. Or use the /etc/hosts way for a more permanent fix
    – rogerdpack
    Jan 29, 2020 at 17:24
2

It was a weird issue after installing the update to Mac Sierra 10.12 (16A323). In the hosts file with the below sorted the issue.

::1         localhost <myhostname>.local   <--- Was already present
127.0.0.1   localhost <myhostname>.local   <--- Solved the tomcat loading issue

You can get the myhostname by command $hostname anywhere in the terminal.

1

I think it is a general problem with the new OS. I have a similar problem: I have a web application which is deployed to tomcat. On El Capitan it started up in 10 secs, now it takes 95 secs and the client (a Swing based desktop app) cannot connect to it (or at least it took lot of time). I think it is something around network communication, because a simple test console app runs well.

1
1

The accepted answer helped me! Just adding this here explaining what I think the issue for me was:

My Hostname was something like "My Mac" which couldn't be resolved. In the settings it showed me that the computer could be addressed with mymac.local

I thought it was the space and renamed my mac to "my.mac" but even this didn't help since the dns automatically added was still mymac.local

Adding my.mac to the /etc/hosts helped then.

So my guess what the actual issue is: This only happens when your computer name contains anything that is not a letter. This is automatically removed by the os and then hostname and dns entry don't match. (which can be fixed my manually adding it)

1
  • 2
    I think the issue runs deeper than that... my hostname was all normal alpha letters, yet adding it to /etc/hosts solved a range of weird issues for me
    – Magnus
    Feb 20, 2019 at 16:46
0

I experienced the same issue on my Mac.

When I changed my primary and Bonjour hostnames to only contain alphanumeric characters it resolved the issue. The idea came from a colleague who had read the advice somewhere when he was facing a similar problem (he couldn't remember where).

Taking inspiration from this guide these were the steps I followed:

First, change primary hostname

sudo scutil --set HostName <new host name>

e.g.:

sudo scutil --set HostName eggsandwich

Next, change Bonjour hostname (for completeness, I never tried without this step so it could be it's not needed).

sudo scutil --set LocalHostName <new host name>

e.g.:

sudo scutil --set LocalHostName eggsandwich

Now restart the java processes you were having issues with, and hopefully they should no longer hang.

On a side note, this also solved another issue I had where a new tab in Terminal would not start bash in the same directory in spite of my preferences. I have no explanation for why that happened, but I'm very pleased.

Not the answer you're looking for? Browse other questions tagged or ask your own question.