Java JVM 需要很长时间才能解析 localhost 的 ip-address
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39636792/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jvm takes a long time to resolve ip-address for localhost
提问by Tomasz Miku?
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'.
升级到 macOS Sierra 后,我似乎遇到了“sbt 测试”(包括查找本地主机名称/IP 地址)的性能问题。在以前版本的 OS X 上,大约需要 40-50 秒才能完成。macOS Sierra 时间远高于此。我最后一次运行大约是 15 分钟。编译时间与“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.
我是我团队中唯一一个尝试这款新 macOS 的人,所以我不知道它是否只发生在我的 mac 上,还是普遍存在的问题。
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
我的同事在 Ubuntu 上有一个类似的问题,它与随机数生成减慢测试有关 -服务响应时间慢: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.
不幸的是,这对我不起作用。最初我在 JDK 8u54 上尝试过,然后尝试更新到 JDK 8u102,但这也没有帮助。
P.S. I'm running Macbook Pro Mid-2015 2.8GHz i7, 16GB ram, 1TB SSD.
PS 我正在运行 Macbook Pro 2015 年年中 2.8GHz i7,16GB 内存,1TB SSD。
采纳答案by thoeni
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.
我有同样的问题。升级后,Tomcat 从 15 秒到 6 分钟初始化 spring 上下文……禁用 csrutils 并没有解决我的问题。
I solvedthe 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:
我通过在/etc/hosts
映射到127.0.0.1
地址的文件上添加我的 Mac 主机名(即 Macbook.local,或任何你的 Mac 的名字)解决了这个问题,::1
就像这样:
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/
如果您有兴趣,可以在此处找到有关问题和解决方案的一些详细信息:https: //thoeni.io/post/macos-sierra-java/
On the post I also link to a github projectto help troubleshooting the issue and validating the solution.
在帖子中,我还链接到了一个github 项目,以帮助解决问题并验证解决方案。
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.
问题与(我相信)本地主机名称解析的工作方式以及 java.net.InetAddr 类如何检索地址有关。我与少数同事进行了验证,显然升级到 Sierra 的每个人都不会发生这种情况,但我仍在调查这种变化的根源。
The solution anyway was the same that antid0teimplemented and worked immediately.
回答by Andras Gyetvan
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.
我认为这是新操作系统的普遍问题。我有一个类似的问题:我有一个部署到 tomcat 的 Web 应用程序。在 El Capitan 上,它在 10 秒内启动,现在需要 95 秒并且客户端(基于 Swing 的桌面应用程序)无法连接到它(或者至少花了很多时间)。我认为这与网络通信有关,因为一个简单的测试控制台应用程序运行良好。
回答by Antid0te
I have the same problem. My spring-boot application take 60 seconds to start on Sierra against 25 seconds on Yosemite.
我也有同样的问题。我的 spring-boot 应用程序需要 60 秒才能在 Sierra 上启动,而在 Yosemite 上需要 25 秒。
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.
在调试时,我意识到问题来自 InetAddress.getLocalHost()。我更改了我的主机文件,为 127.0.0.1 和 :: 1 添加了我的主机名,现在应用程序的启动速度和以前一样快。
回答by camikiller
Correct answer: Jvm takes a long time to resolve ip-address for localhost
正确答案:JVM 需要很长时间来解析 localhost 的 ip-address
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
回答by jurajw
Enabling e.g. System Preferences > Sharing > Remote Login, results in the hostname being automatically assigned an IP address.
启用例如系统偏好设置 > 共享 > 远程登录,会导致主机名自动分配一个 IP 地址。
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).
由于人们在升级后看到问题,因此假设 10.12 更改了主机名的解析方式是有道理的,即至少在 10.11 中,主机名始终可以解析,而在 10.12 中,只有在“系统偏好设置”中启用服务时才能解析 >共享(使用 10.11 的人可以确认这一点)。
回答by Sidd Gautama
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.
安装更新后出现了一个奇怪的问题Mac Sierra 10.12 (16A323)
。在下面的主机文件中对问题进行了排序。
::1 localhost <myhostname>.local <--- Was already present
127.0.0.1 localhost <myhostname>.local <--- Solved the tomcat loading issue
You can get the myhostnameby command $hostname
anywhere in the terminal.
您可以在终端的任何位置通过命令获取myhostname$hostname
。
回答by Arne
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
我的主机名类似于“我的 Mac”,无法解析。在设置中,它告诉我可以使用 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
我认为这是空间并将我的 mac 重命名为“my.mac”,但即使这样也无济于事,因为自动添加的 dns 仍然是 mymac.local
Adding my.mac to the /etc/hosts helped then.
然后将 my.mac 添加到 /etc/hosts 有帮助。
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)
所以我猜测实际问题是什么:这仅在您的计算机名称包含不是字母的任何内容时才会发生。这会被操作系统自动删除,然后主机名和 dns 条目不匹配。(这可以修复我手动添加它)
回答by Erik Madsen
I experienced the same issue on my Mac.
我在 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).
当我将主主机名和 Bonjour 主机名更改为仅包含字母数字字符时,它解决了该问题。这个想法来自一位同事,他在遇到类似问题时曾在某处阅读过建议(他不记得在哪里)。
Taking inspiration from this guidethese 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).
接下来,更改 Bonjour 主机名(为了完整起见,我从未尝试过没有这一步,因此可能不需要)。
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.
现在重新启动您遇到问题的 java 进程,希望它们不再挂起。
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.
附带说明一下,这也解决了我遇到的另一个问题,即尽管我有偏好,但终端中的新选项卡不会在同一目录中启动 bash。我无法解释为什么会这样,但我很高兴。