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.

Mario Galic
  • 47,285
  • 6
  • 56
  • 98
Tomasz Mikus
  • 1,286
  • 2
  • 9
  • 8
  • I'm using `gradle clean test` and having the same issue. – Max Peng Aug 22 '17 at 14:16
  • This is the bug reported to OpenJDK: https://bugs.openjdk.java.net/browse/JDK-8143378 It has been resolved as a duplicate of another bug which won't be fixed. It should probably be reopened. – Lóránt Pintér Oct 24 '19 at 23:41

8 Answers8

379

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.

Community
  • 1
  • 1
thoeni
  • 4,064
  • 2
  • 14
  • 14
  • Same issue... And I can confirm that disabling SIP didn't work for me as well. – borges Sep 26 '16 at 20:22
  • Great it works, you can check with the github project https://github.com/thoeni/inetTester – christmo Oct 11 '16 at 16:59
  • How to know what is name of mac? I have already 127.0.0.1 localhost and ::1 localhost in /etc/hosts – VK321 Oct 22 '16 at 06:34
  • 27
    Found it! Its not mac name.. Its hostname. By typing "hostname" in terminal can give correct hostname. – VK321 Oct 22 '16 at 06:52
  • @VinodKumar You can also find and edit it in Preferences > Sharing > "Computers on your local network can access your computer at:" –  Dec 01 '16 at 00:36
  • Important not to forget the ipv6 entry, like I did – Hans Westerbeek Dec 04 '16 at 12:17
  • @thoeni thanks for this! just a note, this still repros on 10.12.2 for me, so the hosts file modification is still necessary – cambecc Dec 30 '16 at 11:36
  • @cambecc I confirm that after restarting the issue appeared again, therefore the update didn't fix it. I've removed the update since the solution to the problem is still the correct one (and the only one for now) – thoeni Jan 04 '17 at 10:24
  • Many thanks, it actually solved my JDBC connection performance issue with Spring! – Daniel G. Jun 07 '17 at 14:31
  • @thoeni I'm seeing 2 different values for hostname vs Sharing > Computer Name, please clarify which one to use ? – vikramvi Jul 24 '17 at 15:04
  • @vikramvi the hostname should be the same, just appending the ".local" suffix, probably... which names do you see? I would try to use the output of the hostname command... – thoeni Jul 25 '17 at 12:27
  • I had to add 'localunixsocket.local' (both for ::1 and 127.0.0.1) to fix this problem. – reto Nov 16 '17 at 17:01
  • 1
    Still seeing this on OSX 10.14.2, years later. Thanks so much for the fix. – Malcolm Crum Jan 24 '19 at 00: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 '19 at 13:27
  • Came here from https://issues.apache.org/jira/browse/ZEPPELIN-5023 - this also works running Zeppelin locally, makes it almost instant instead of the 2 - 3 minute wait. – Sinister Beard May 20 '21 at 09:44
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
Community
  • 1
  • 1
camikiller
  • 1,511
  • 20
  • 19
  • 4
    Awesome! This boosted my minecraft launching time from 12 seconds to 0.5 seconds. – Bob May 07 '17 at 00:50
  • @camikiller I'm seeing 2 different values for hostname vs Sharing > Computer Name, please clarify which one to use ? – vikramvi Jul 24 '17 at 15:06
  • Better solution is to upgrade your MacOS @vikramvi – camikiller Jul 24 '17 at 15:58
  • @camikiller later I did per https://apple.stackexchange.com/questions/66611/how-to-change-computer-name-so-terminal-displays-it-in-mac-os-x-mountain-lion, to have same name but still it's not working. Do I need to do some other settings in Sharing ? – vikramvi Jul 24 '17 at 18:48
  • @vikramvi Try editing manually your `/etc/hosts` file and add the 2 hosts – camikiller Jul 25 '17 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.

Koray Tugay
  • 22,894
  • 45
  • 188
  • 319
Antid0te
  • 91
  • 3
  • 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 https://github.com/spring-projects/spring-boot/issues/7087) – thoeni Oct 11 '16 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).

jurajw
  • 41
  • 2
  • I've 10.12.5 but your solution didn't work. which version do you have ? – vikramvi Jul 24 '17 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 '17 at 00: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 '20 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

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)

Arne
  • 1,364
  • 1
  • 11
  • 22
  • 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 '19 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.

Erik Madsen
  • 1,913
  • 3
  • 20
  • 34