Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java7 SecureRandom performance on MacOSX

I'm running tomcat7 with jdk7 on MacOSX Mavericks DP3. Everything goes well , and it only takes 500 ms to start up . But suddenly, it slows down to 35 seconds.

The log message shows SecureRandom is the root cause. Thanks for google, I found it's a jre bug , and following code to verify:

import java.security.SecureRandom;
class JRand {
     public static void main(String args[]) throws Exception {
        System.out.println("Ok: " +
           SecureRandom.getInstance("SHA1PRNG").nextLong());
     }
}

Yes. The simplest code also takes 35 seconds. But it seems that all those related solutions do not work for me. Both /dev/random and /dev/urandom are not block devices on Mac.

cat /dev/random | hexdump -C

output very quickly!

When switched back to jre6, it's very fast to generate randoms. Download the latest jdk8-ea, the problem still exists.

In fact , not only tomcat slows down significantly, Netbeans, glassfish are all affected. After struggling with for hours, I gave up at last.

This morning, when I came to office, plugged in Ethernet, guess what ? It recovered!

So my question is , what happens behind ? It's really weird.

Thanks.

like image 601
outersky Avatar asked Sep 20 '26 18:09

outersky


1 Answers

Haha, resolved. Get InetAddress.java source code (can copy from IDE); modify method getLocalHost from

String local = impl.getLocalHostName();

to :

String local = "localhost"; // impl.getLocalHostName();

recompile it, and add java.net.InetAddress.class back to JDK/jre/lib/rt.jar.

RESOLVED.

like image 105
outersky Avatar answered Sep 22 '26 09:09

outersky