JVMã®ãã¥ã¼ãã³ã°
ååãJVMとGCのしくみ - ITエンジニアとして生きるã§JVMã¨GCã®ããã¿ã«ã¤ãã¦æ¸ããã
ä»åã¯ãã®ç¶ãã¨ãããã¨ã§JVMã®ãã¥ã¼ãã³ã°ã«ã¤ãã¦æ¸ãããã¨æãã
JVMãã¥ã¼ãã³ã°ã£ã¦
- -Xms ã»ã»ã» ãã¼ãå ¨ä½ï¼Newé åï¼Oldé åï¼ã®åæå¤
- -Xmx ã»ã»ã» ãã¼ãå ¨ä½ï¼Newé åï¼Oldé åï¼ã®æ大å¤
ããããã話é¡ã«ä¸ãããªããæèããªããã¨ãå¤ãï¼æ°ãããï¼ã
ã§ããã³ãã¯ããã ãã§ã¯ãã¡ã§ãååã®ããã«Permanenté åãNewé åãOldé åãæèãããã¥ã¼ãã³ã°ãå¿
è¦ã«ãªãã
VMãã¥ã¼ãã³ã°ãèãããã®åã«ã»ã»ã»
ãã¥ã¼ãã³ã°ã®è©±ãããåã«ã¾ãVMã®èµ·åã¢ã¼ãã«ã¤ãã¦è©±ãããã¨æããVMã«ã¯å¤§ãã以ä¸ï¼ã¤ã®èµ·åã¢ã¼ããããããããã以ä¸ã®ãããªç¹å¾´ãæã¤ã
âã¯ã©ã¤ã¢ã³ãVMã¢ã¼ã
- èµ·åæéãç縮ããã¡ã¢ãªãµã¤ãºã縮å°ããããã«èª¿æ´ããã¦ããã
- VMèµ·åæã«ã-clientããªãã·ã§ã³ãä»ãã¦å®è¡ããã
âãµã¼ãVMã¢ã¼ã
- ããã°ã©ã å®è¡é度ãæ大ã«ãªãããã«è¨è¨ããã¦ããã
- VMèµ·åæã«ã-serverããªãã·ã§ã³ãä»ãã¦å®è¡ããã
ããã2ã¤ã®ã¢ã¼ãã«éãã«ã¤ãã¦ã¯jvm - Real differences between "java -server" and "java -client"? - Stack Overflowã§ãåãä¸ãããã¦ãããã2ã¤ã®ã¢ã¼ãã§ã¯VMãªãã·ã§ã³ãéããã¨ãããã¨ãè«ãããã¦ããã
èªç°å¢ã®JDKãããã©ã«ãã§ã©ã¡ãã®èµ·åã¢ã¼ãã«ãªããã調ã¹ãããã«ã¯ãjava -versionããå®è¡ããã°è¯ãã
以å¾ã®ãã¥ã¼ãã³ã°ã®è©±ã¯ããµã¼ãVMããåæã¨ãã¦è©±ãé²ããã
Permanenté åã®ãã¥ã¼ãã³ã°
Permanenté åã®ãã¥ã¼ãã³ã°ã¯ä»¥ä¸ã®ã·ã¹ãã ããããã£ã§å®æ½ããã- -XX:PermSize ã»ã»ã» Permanent é åã®åæå¤
- -XX:MaxPermSize ã»ã»ã» Permanent é åã®æ大å¤
é常ã¯ãã¾ãæãå
¥ããªãè¨å®ã
ã§ãTomcatå©ç¨ãã¦ãã人ãªãå¿
ãï¼åº¦ã¯
java.lang.OutOfMemoryError: PermGen space at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClassCond(ClassLoader.java:632) ãããããï¼ ãããããï¼
ãªã¨ã©ã¼ã«åºãããããã¨ãããã ããã
Tomcatã®ãããªWeb/APãµã¼ãã¯æ°å¤ãã®ã©ã¤ãã©ãªããã¼ããããããããã©ã«ãå¤ï¼64MBï¼ã§ã¯Permanenté åãä¸è¶³ããã¡ã§ããã
ãã®å ´åãPermanenté åãæ¡å¼µããå¿
è¦ããããã ããçµé¨ä¸ã256MBããããã«ãã¦ããã°ã¾ãåé¡ãªãã¨æãã
-XX:PermSize=256m -XX:MaxPermSize=256m
ã¡ãªã¿ã«PermSizeã®ããã©ã«ãå¤ã¯ä»¥ä¸ã®éãã
ãã¼ãé åã®ãã¥ã¼ãã³ã°
ãã¼ãé åã®ãã¥ã¼ãã³ã°ã¯ä»¥ä¸ã®ã·ã¹ãã ããããã£ã§å®æ½ããã- -Xms ã»ã»ã» ãã¼ãé åï¼Newé åï¼Oldé åï¼ã®åæå¤
- -Xmx ã»ã»ã» ãã¼ãé åï¼Newé åï¼Oldé åï¼ã®æ大å¤
æ¬æ¥ã¯ãOSã®ç©ºãã¡ã¢ãªéãããã¢ããªã±ã¼ã·ã§ã³ãå¿
è¦ã¨ããã¡ã¢ãªéããç®åºãã¦æ±ºå®ãã¹ãå¤ã§ãããããã®ç®åºã¯ãªããªãé£ããã
å®éã«ã¯è«ççã«ç®åºããããããããç¨åº¦æ±ºãæã¡ã§ã¡ã¢ãªãå²ãå½ã¦ãè² è·è©¦é¨ãå®æ½ããªããã¡ã¢ãªéã«åé¡ããªããã¨ã確èªãã¦ããã±ã¼ã¹ãå¤ãã¨æãã
ãOSã®ç©ºãã¡ã¢ãªéãã許ãã®ã§ããã°ãã¨ããããã2GBãã¨ãã¦ããã°è¯ããï¼â»ï¼
-Xms2048m -Xmx2048m
â» JVM Max Heap Size recommended (Performance forum at Coderanch)ã§è«ãããã¦ããããã«ã32bitã®HotspotJVMã§ã¯æ大ãã¼ããµã¤ãºã¯ã2GBãã¨ããã¦ãããããã2GBã以ä¸ã®ãã¼ããå²å½ã¦ãå ´åã¯æ³¨æãå¿ è¦ã
Newé åã¨Oldé åã®ãã¥ã¼ãã³ã°
Newé åã¨Oldé åã®ãã¥ã¼ãã³ã°ã¯ä»¥ä¸ã®ã·ã¹ãã ããããã£ã§å®æ½ããã- -XX:NewRatio ã»ã»ã» New é å㨠Old é åã®æ¯ç
- -XX:NewSizeï¼-Xmnã§ãå¯ï¼ ã»ã»ã» New é åã®åæå¤
- -XX:MaxNewSize ã»ã»ã» New é åã®æ大å¤
- -XX:SurvivorRatio ã»ã»ã» Edené åã¨Survivoré åã®æ¯ç
ã¾ãã¯NewRatioã«ã¤ãã¦ã
NewRatioã®ããã©ã«ãå¤ã¯ä»¥ä¸ã®éãã
ã¤ã¾ãããµã¼ãVMã®ããã©ã«ãã§ã¯ãNewé åï¼Oldé åï¼1ï¼2ãã¨ãªã£ã¦ããããã¼ãå
¨ä½ã®1/3ãNewé åã2/3ãOldé åã«å²ãå½ã¦ããããã¨ã«ãªãã
é常ãNewé åã®ãµã¤ãºã¯æ大ãã¼ããµã¤ãºï¼-Xmxï¼ã®1/4ã1/2ã§è¨å®ããã®ããã¹ããã©ã¯ãã£ã¹ã¨ããã¦ãããããããã©ã«ãå¤ã§OKã
ããç´°ãããªãã¥ã¼ãã³ã°ãå¿
è¦ã¨ããå ´åã¯è¦ç´ãå ´åãããã ããããè² è·è©¦é¨çã§åé¡ãçºçããªãéãç¹ã«å¤æ´ããå¿
è¦ã¯ç¡ãã ããã
次ã«-XX:NewSizeï¼-Xmnï¼ã¨-XX:MaxNewSizeã«ã¤ãã¦ã
ããã¯ä¸è¨èª¬æã®éãNewé åã®ãµã¤ãºããã¦ãããããã®ã
ãã ãã®è¾ºã®è¨å®ã¯ã-Xmsãã-Xmxãã-XX:NewRatioãã®3ã¤ã§æå®ããæ¹ãåãããããã¨æãã®ã§ç§ã¯ãã¾ãå©ç¨ããªãã
åè¿°ã®éãNewé åã®ãã¥ã¼ãã³ã°ã«ãããé¢å¿äºã¯ãæ大ãã¼ããµã¤ãºï¼-Xmxï¼ã®ä½åã®ï¼ã¨ãããï¼ãã§ãããããã-XX:NewRatioãã«ã¦è¨å®ãããæ¹ãè¯ãã¨æãã
æå¾ã«-XX:SurvivorRatioã«ã¤ãã¦ã
SurvivorRatioã®ããã©ã«ãå¤ã¯ä»¥ä¸ã®éãã
ã¤ã¾ãããµã¼ãVMã®ããã©ã«ãã§ã¯ãEdené åï¼Fromé åï¼Toé åï¼8ï¼1ï¼1ãã¨ãªã£ã¦ãããNewé åã®8å²ãEdené åã«å²ãå½ã¦ããã¦ãããã¨ã«ãªãã
ãã®å¤ã¯æ¬æ¥ãã©ã®ç¨åº¦ã®é »åº¦ã§ãªãã¸ã§ã¯ããã¤ã³ã¹ã¿ã³ã¹åããããï¼ããå
ã«ç®åºãã¦æ±ºå®ãã¹ãã§ãããããã®ç®åºã¯é£ãããï¼ã²ã¨ã¤ã²ã¨ã¤ã®ãªãã¸ã§ã¯ããã©ã®ç¨åº¦ã¡ã¢ãªãå°ãããç®åºããã®ã¯é£ããããï¼
ãªã®ã§è«ççã«ç®åºããã®ã§ã¯ãªããã²ã¨ã¾ãããã©ã«ãå¤ï¼8ï¼ã«ãã¦ããã¦ãè² è·è©¦é¨ãå®æ½ããªããåé¡ãããã©ããã確èªãã¦ããæ¹éã¨ããã®ãè¯ãã ããã
ã¡ãªã¿ã«ç§ã®çµé¨ä¸ã§ã¯ããã©ã«ãå¤ã§ãåé¡ãªããã¨ãå¤ãã£ãã
ã¨ãããã¨ã§ãNewé åã¨Oldé åã®ãã¥ã¼ãã³ã°ã¯
-XX:NewRatio=2 -XX:SurvivorRatio=8
ã¨ããã®ãè¯ãã ããã
åæå¤ã¨æ大å¤
ãã¥ã¼ãã³ã°ã«é¢ããã·ã¹ãã ããããã£ã«ã¯ãåæå¤ãã¨ãæ大å¤ããè¨å®ãããã®ããããï¼ã-Xms â -Xmxããã-XX:PermSize â -XX:MaxPermSizeãï¼ããã¯åºæ¬çã«åå¤ã«è¨å®ãã¦ããã¹ãã§ããã
ãåæå¤ â æ大å¤ãã¨ããå ´åãåæåæã«ã¯æ大ã¢ãã¬ã¹ç©ºéããä»®æ³çãã«ç¢ºä¿ãããå¿
è¦ã«ãªãã¾ã§ç©çã¡ã¢ãªã¯å²ãå½ã¦ãããªããï¼åæå¤ã§å¦çãã¾ããªããªããªã£ãå ´åã«ããã®ãä»®æ³çããªé¨åã«ç©çã¡ã¢ãªãå²å½ã¦ããããï¼
ãã®ç©çã¡ã¢ãªãå²å½ã¦ã¦ããç¬éã¯ã¦ã¼ã¶ããªãã£ãè½ã¨ããã¨ããããããããªãï¼â»ï¼ãããããããã®ãããªç¡é§ãªå¦çãå¼ãèµ·ããè¨å®ã¨ããªãæ¹ãè¯ãã¨èããã
ãã®ããããã¹ããã©ã¯ãã£ã¹ã¨ãã¦ãåæå¤ï¼æ大å¤ãã¨ãããã¨ãæ¨å¥¨ãããã
â» å®éã«ãããã£ããã¨ã«åºãããã¦ããªãï¼ãåæå¤â æ大å¤ãã§éç¨ãããã¨ããªãï¼ã®ã§ãããã¾ã§ããã¯å¯è½æ§ã®è©±ã¨ãããã¨ã§ã
ãããã«
ããã¾ã§ã®è©±ãçµ±åããã¨ããããªãã©ã¡ã¼ã¿ã«ãªãã-XX:PermSize=256m -XX:MaxPermSize=256m -Xms2048m -Xmx2048m -XX:NewRatio=2 -XX:SurvivorRatio=8
ä»åã¯Permanenté åãNewé åãOldé åãæèãããã¥ã¼ãã³ã°ããã¹ããã¨ãããã¨ã話ããã
ãã㯓Stop the World”を防ぐコンカレントGCとは? (1/2):現場から学ぶWebアプリ開発のトラブルハック(2) - @ITã§ç´¹ä»ããã¦ããã³ã³ã«ã¬ã³ãGCãå©ç¨ããéã«ã¯ç¹ã«å¤§äºã«ãªã£ã¦ããã
ï¼ã³ã³ã«ã¬ã³ãGCãå©ç¨ããã°ååè¿°ã¹ã¦ãããããªFull GCæã®æ¸å¿µç¹ã解æ¶ããããããã¾ããã¥ã¼ãã³ã°ï¼ç¹ã«Newé åï¼ããªãã¨éã«æ§è½ãè½ã¨ããã¨ã«ãªããï¼
ãã¤ãã³ã³ã«ã¬ã³ãGCã«ã¤ãã¦ãè¨äºãæ¸ããããªãã