37. Sample: Nativeイメージを作る
• Libcの依存も無くそう!!
– --staticでsingle binaryが生成できる
37
$ native-image –H:Name=hello --no-server --static HelloWorld
$ file ./hello
./hello: ELF 64-bit LSB executable, x86-64, version 1 (GNU/Linux),
statically linked, for GNU/Linux 3.2.0,
BuildID[sha1]=db0a71c9360fe7b8a21e1c5cdcedad965643152e, with debug_info, not stripped
$ ldd ./hello
not a dynamic executable
$ ls -hs ./hello
3.4M ./hello
$ ./hello
Hello, World!
38. Sample: Nativeイメージを作る
• Nativeイメージは起動がはやい
38
$ time java Hello
real 0m0.051s
user 0m0.032s
sys 0m0.016s
$ time ./hello
real 0m0.028s
user 0m0.000s
sys 0m0.025s
Java Native
0.051s 0.028s
50. Sample: Redis 50
• Mainのコード
public class App {
public static void main( String[] args )
throws Exception {
Jedis jedis = new Jedis("localhost", 6379);
jedis.set("foo", "bar");
String value = jedis.get("foo");
System.out.println("value: " + value);
}
}
51. Sample: Redis 51
• 単純にビルドしても怒られます
[hello-1.0-SNAPSHOT:7248] classlist: 2,885.53 ms
[hello-1.0-SNAPSHOT:7248] (cap): 1,210.36 ms
[hello-1.0-SNAPSHOT:7248] setup: 2,941.58 ms
SLF4J: slf4j-api 1.6.x (or later) is incompatible with this binding.
SLF4J: Your binding is version 1.5.5 or earlier.
SLF4J: Upgrade your binding to version 1.6.x.
[hello-1.0-SNAPSHOT:7248] analysis: 6,413.27 ms
error: Class initialization failed: redis.clients.jedis.HostAndPort
Detailed message:
Error: Class initialization failed: redis.clients.jedis.HostAndPort
Original exception that caused the problem: java.lang.NoSuchMethodError:
org.slf4j.impl.StaticLoggerBinder.getSingleton()Lorg/slf4j/impl/StaticLogger
Binder;
at org.slf4j.LoggerFactory.bind(LoggerFactory.java:150)
85. 制限事項: synchronized 85
import java.util.ArrayList;
public class Sync2 {
static String hoge() {
ArrayList<String> v = new ArrayList<>();
v.add("Me"); v.add("You"); v.add("Her");
return v.toString();
}
public static void main(String[] args) {
for (int i = 0; i < 10000000; i++) {
String v = hoge();
}
}
}
real 0m2.099s
user 0m3.578s
sys 0m0.393s
real 0m1.700s
user 0m1.591s
sys 0m0.102s
Java Native
Synchronized無しは
Nativeの方が若干速いけど、
86. 制限事項: synchronized 86
import java.util.Vector;
public class Sync2 {
static String hoge() {
Vector<String> v = new Vector<>();
v.add("Me"); v.add("You"); v.add("Her");
return v.toString();
}
public static void main(String[] args) {
for (int i = 0; i < 10000000; i++) {
String v = hoge();
}
}
}
real 0m2.075s
user 0m3.789s
sys 0m0.238s
real 0m4.004s
user 0m3.901s
sys 0m0.090s
Java Native
Synchronizedがあると、
Nativeはとても遅い!!