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

JavaScalaJvmLocalhostMacos Sierra

Java Problem Overview


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 - https://stackoverflow.com/questions/27173833/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.

Java Solutions


Solution 1 - Java

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.

Solution 2 - Java

Correct answer: https://stackoverflow.com/questions/39636792/sbt-test-extremely-slow-on-macos-sierra/39698914#39698914

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

Solution 3 - Java

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.

Solution 4 - Java

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).

Solution 5 - Java

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.

Solution 6 - Java

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.

Solution 7 - Java

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)

Solution 8 - Java

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTomasz MikuśView Question on Stackoverflow
Solution 1 - JavathoeniView Answer on Stackoverflow
Solution 2 - JavacamikillerView Answer on Stackoverflow
Solution 3 - JavaAntid0teView Answer on Stackoverflow
Solution 4 - JavajurajwView Answer on Stackoverflow
Solution 5 - JavaSidd GautamaView Answer on Stackoverflow
Solution 6 - JavaAndras GyetvanView Answer on Stackoverflow
Solution 7 - JavaArneView Answer on Stackoverflow
Solution 8 - JavaErik MadsenView Answer on Stackoverflow