{"id":444,"date":"2019-02-25T11:49:58","date_gmt":"2019-02-25T02:49:58","guid":{"rendered":"http:\/\/localhost:8000\/?p=444"},"modified":"2021-01-23T11:51:28","modified_gmt":"2021-01-23T02:51:28","slug":"java-tips","status":"publish","type":"post","link":"http:\/\/localhost:8000\/2019\/02\/java-tips.html","title":{"rendered":"Java\u306eTips"},"content":{"rendered":"
Java\u306b\u95a2\u9023\u3059\u308bTips\u3002\u4eca\u5f8c\u306fJava\u3067\u5b9f\u88c5\u3059\u308b\u6a5f\u4f1a\u3082\u5c11\u306a\u304f\u306a\u308b\u3068\u306f\u601d\u3046\u3051\u3069\u3001\u304a\u308a\u306b\u3075\u308c\u3066\u8ffd\u8a18\u3057\u3066\u3044\u304f\u4e88\u5b9a\u3002<\/p>\n
# \u6700\u65b0\u30d0\u30fc\u30b8\u30e7\u30f3\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\nbrew cask install java # 2019\u5e744\u6708\u73fe\u5728\u3060\u3068jdk12\u304cinstall\u3055\u308c\u308b\n\n# \u904e\u53bb\u30d0\u30fc\u30b8\u30e7\u30f3\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\nbrew tap homebrew\/cask-versions\n# brew cask install java8 #\u914d\u5e03\u505c\u6b62\u305a\u307f\nbrew cask install adoptopenjdk8\nbrew cask install java11<\/code><\/pre>\nJAVA_HOME\u78ba\u8a8d<\/h3>\n\/usr\/libexec\/java_home -v 1.8\n> \/Library\/Java\/JavaVirtualMachines\/adoptopenjdk-8.jdk\/Contents\/Home\n\/usr\/libexec\/java_home -v 11\n-> \/Library\/Java\/JavaVirtualMachines\/openjdk-11.0.2.jdk\/Contents\/Home\n\/usr\/libexec\/java_home -v 12\n-> \/Library\/Java\/JavaVirtualMachines\/openjdk-12.0.1.jdk\/Contents\/Home<\/code><\/pre>\nJAVA_HOME\u5207\u308a\u66ff\u3048<\/h3>\n
alias\u3092\u8a2d\u5b9a\u3057\u3066\u304a\u304d\u3001java12<\/code>, java8<\/code>\u306e\u3088\u3046\u306a\u611f\u3058\u3067\u5207\u308a\u66ff\u3048\u308b<\/p>\nalias java8='export JAVA_HOME=`\/usr\/libexec\/java_home -v 1.8`'\nalias java11='export JAVA_HOME=`\/usr\/libexec\/java_home -v 11`'\nalias java12='export JAVA_HOME=`\/usr\/libexec\/java_home -v 12`'<\/code><\/pre>\n\u30e1\u30e2\u30ea\u8abf\u67fb\u95a2\u4fc2<\/h2>\n\u30ed\u30fc\u30ab\u30eb\u3067\u52d5\u3044\u3066\u3044\u308bjava\u306e\u76e3\u8996<\/h3>\n\n- GC\u306e\u5b9f\u884c<\/li>\n
- \u30d2\u30fc\u30d7\u30c0\u30f3\u30d7\u53d6\u5f97\n
jvisualvm -J-Xmx1024m<\/code><\/pre>\n<\/li>\n<\/ul>\njConsole<\/h3>\n\n- \u30e1\u30e2\u30ea\u4f7f\u7528\u72b6\u6cc1\u3092\u30ea\u30a2\u30eb\u30bf\u30a4\u30e0\u76e3\u8996<\/li>\n
- \u5916\u304b\u3089GC\u3092\u304b\u3051\u3089\u308c\u308b<\/li>\n<\/ul>\n
java \u30d7\u30ed\u30bb\u30b9ID\u3092\u8abf\u3079\u308b<\/h3>\njps -l<\/code><\/pre>\nGC\u306e\u624b\u5b9f\u884c<\/h3>\njcmd ${\u30d7\u30ed\u30bb\u30b9ID} GC.run<\/code><\/pre>\nthread dump\u306e\u53d6\u5f97<\/h3>\njstack ${\u30d7\u30ed\u30bb\u30b9ID} > ${\u30d5\u30a1\u30a4\u30eb\u540d} # hoge.tdump<\/code><\/pre>\nheap dump\u306e\u53d6\u5f97<\/h3>\ndf -h \/${\u5bfe\u8c61\u30d5\u30a9\u30eb\u30c0}\njmap -dump:format=b,file=${\u30d5\u30a1\u30a4\u30eb\u540d} ${\u30d7\u30ed\u30bb\u30b9ID} # hoge.hprof<\/code><\/pre>\nEclipse Memory Analizer<\/h3>\n\n- Eclipse Plugin<\/li>\n
- \u30ed\u30fc\u30ab\u30ebPC\u3067\u5b9f\u884c\u4e2d\u306eJVM\u3084GC\u30c0\u30f3\u30d7\u3092\u53d6\u308a\u8fbc\u3093\u3067\u5206\u6790\n
\n- \u5b9f\u884c\u4e2d\u306eJVM\u3092\u5206\u6790\u3059\u308b\u5834\u5408\u3001\u53d6\u308a\u8fbc\u3093\u3060\u30bf\u30a4\u30df\u30f3\u30b0\u3067\u3001\u3044\u3063\u305f\u3093dump\u30d5\u30a1\u30a4\u30eb\u304c\u4f5c\u6210\u3055\u308c\u308b<\/li>\n
- 1G\u30af\u30e9\u30b9\u306edump\u30d5\u30a1\u30a4\u30eb\u3092\u89e3\u6790\u3059\u308b\u306b\u306f\u3001\u305d\u308c\u3092\u8aad\u307f\u8fbc\u3080\u5fc5\u8981\u304c\u3042\u308a\u305d\u308c\u3060\u3051\u3067\u6570\u5206\u304b\u304b\u3063\u305f\u30fb\u30fb\u30fb<\/li>\n<\/ul>\n<\/li>\n
- Leek suspects -> Dominator Tree\u3092\u898b\u3066\u3044\u304f\u3068\u3001\u30e1\u30e2\u30ea\u30ea\u30fc\u30af\u3057\u3066\u3044\u308b\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u304c\u5206\u304b\u308b<\/li>\n
- \u300cShallow Heap\u300d\u306f\u305d\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u81ea\u4f53\u306e\u30b5\u30a4\u30ba\u3002\u300cRetained Heap\u300d\u306f\u305d\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\uff0b\u5b50\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u5408\u8a08\u30b5\u30a4\u30ba\n
\n- \u5b50\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u300cRetained Heap\u300d\u306e\u5408\u8a08\u3000\uff0b\u3000\u81ea\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u300cShallow Heap\u300d\u3000\uff1d\u3000\u81ea\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u300cRetained Heap\u300d <\/li>\n
- \u540c\u3058\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u8907\u6570\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u304b\u3089\u53c2\u7167\u3057\u3066\u3044\u308b\u5834\u5408\u3001\u30c0\u30d6\u30eb\u30ab\u30a6\u30f3\u30c8\u3055\u308c\u308b\u304b\u3069\u3046\u304b\u3001\u3088\u304f\u5206\u304b\u3089\u305a\uff1f<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n
\u30c6\u30b9\u30c8<\/h2>\njMockito\u3067\u5b9f\u884c\u6642\u306e\u6319\u52d5\u3092\u4e0a\u66f8\u304d\uff08static method\u3082OK\uff09<\/h3>\nnew MockUp<Hoge>() { \/\/ \u5bfe\u8c61\u306e\u30af\u30e9\u30b9\u3092\u6307\u5b9a\n @Mock\n public void fuga(Invocation inv, final Request request, final Response response) { \/\/ \u5c5e\u6027\u3001\u30e1\u30bd\u30c3\u30c9\u540d\u3001\u5f15\u6570\u306f\u5408\u308f\u305b\u308b\n \/\/ do something\n inv.proceed();\n }\n};<\/code><\/pre>\n\u30ea\u30d5\u30ec\u30af\u30b7\u30e7\u30f3<\/h2>\n\u30d5\u30a3\u30fc\u30eb\u30c9\u306e\u5024\u3092\u53d6\u5f97\u3057\u3066\u66f8\u304d\u63db\u3048\u308b<\/h3>\nClass<Hoge> clazz = Hoge.class;\n\n\/\/ \u3053\u306e\u30af\u30e9\u30b9\u304b\u3089\u53c2\u7167\u4e0d\u53ef\u80fd\u306a\u3082\u306e\u3082\u542b\u3081\u305f\u5168\u30d5\u30a3\u30fc\u30eb\u30c9\u4e00\u89a7\u53d6\u5f97\nField[] declaredFields = clazz.getDeclaredFields();\n\/\/ \u3053\u306e\u30af\u30e9\u30b9\u304b\u3089\u53c2\u7167\u4e0d\u53ef\u80fd\u306a\u3082\u306e\u3082\u542b\u3081\u305f\u30d5\u30a3\u30fc\u30eb\u30c9\u53d6\u5f97\nField declaredField = clazz.getDeclaredField('fuga');\n\n\/\/ \u3053\u306e\u30af\u30e9\u30b9\u304b\u3089\u53c2\u7167\u53ef\u80fd\u306a\u30d5\u30a3\u30fc\u30eb\u30c9\u4e00\u89a7\u53d6\u5f97\nField[] fields = clazz.getFields();\n\/\/ \u3053\u306e\u30af\u30e9\u30b9\u304b\u3089\u53c2\u7167\u53ef\u80fd\u306a\u30d5\u30a3\u30fc\u30eb\u30c9\u53d6\u5f97\nField field = clazz.getField('fuga');\n\n\/\/ \u5024\u306e\u8a2d\u5b9a\nHoge hoge = new Hoge();\nfield.set(hoge, "dummy_string");\n<\/code><\/pre>\ninput stream<\/h2>\nString <=> Input Stream<\/h3>\n\/\/ String -> Input Stream\nString input = "0000000001\\n0000000002\\n0000000003";\nInputStream is = new ByteArrayInputStream(input.getBytes("utf-8"));\n\n\/\/ Input Stream -> String\nBufferedReader reader = new BufferedReader(new InputStreamReader(is, "utf-8"));\nreader.lines().forEach(line -> {});\n<\/code><\/pre>\n\u305d\u306e\u4ed6<\/h2>\n\u6642\u9593\u8a08\u6e2c<\/h3>\nlong start = System.currentTimeMillis();\n\/\/do something\nlong stop = System.currentTimeMillis();\nSystem.out.println("\u5b9f\u884c\u6642\u9593: " + (stop - start));<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"Java\u306b\u95a2\u9023\u3059\u308bTips\u3002\u4eca\u5f8c\u306fJava\u3067\u5b9f\u88c5\u3059\u308b\u6a5f\u4f1a\u3082\u5c11\u306a\u304f\u306a\u308b\u3068\u306f\u601d\u3046\u3051\u3069\u3001\u304a\u308a\u306b\u3075\u308c\u3066\u8ffd\u8a18\u3057\u3066\u3044\u304f\u4e88\u5b9a\u3002 \u30a4\u30f3\u30b9\u30c8\u30fc\u30eb \u30a4\u30f3\u30b9\u30c8\u30fc\u30eb # \u6700\u65b0\u30d0\u30fc\u30b8\u30e7\u30f3\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb brew cask install java # 2019\u5e744\u6708\u73fe\u5728\u3060\u3068jdk12\u304cinstall\u3055\u308c\u308b # \u904e\u53bb\u30d0\u30fc\u30b8\u30e7\u30f3\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb brew tap homebrew\/cask-versions # brew cask install java8 #\u914d\u5e03\u505c\u6b62\u305a\u307f brew cask install adoptopenjdk8 brew cask install java11 JAVA_HOME\u78ba\u8a8d \/usr\/libexec\/java_home -v 1.8 > \/Library\/Java\/JavaVirtualMachines\/adoptopenjdk-8.jdk\/Contents\/Home \/usr\/lib <\/span>Continue Reading<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[34],"tags":[],"_links":{"self":[{"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/posts\/444"}],"collection":[{"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/comments?post=444"}],"version-history":[{"count":0,"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/posts\/444\/revisions"}],"wp:attachment":[{"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/media?parent=444"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/categories?post=444"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/tags?post=444"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}