ãã®èšäºã®æçµçãªç®æšã¯ãç°¡åãªè³ªåã«çããããšã§ãã
ã¢ããªã±ãŒã·ã§ã³å ã®ã©ã®è¡ãä¿®æ£ããããèŠã€ããã«ã¯ãã©ããã¯ãªãã¯ããŸããïŒ
ã¡ã¢ãªãªãŒã¯ãšã¯äœã§ããïŒ
ãã¡ã¢ãªãªãŒã¯ããšåŒã°ãããã®ããå§ããŸãããã å³å¯ãªç解ã§ã¯ããªããžã§ã¯ããžã®ãã¹ãŠã®åç §ã倱ãããåŸã§ãã¡ã¢ãªã«ååšãç¶ããå Žåããªããžã§ã¯ãã¯ã¡ã¢ãªãªãŒã¯ãšåŒã°ããŸãã ãã®å®çŸ©ã§ã¯åé¡ãããã«çºçããŸããäœæãããã¹ãŠã®ãªããžã§ã¯ãã®ã¡ã¢ãªã¯ãã¬ããŒãžã³ã¬ã¯ã¿ã®åå ã«ãã£ãŠå²ãåœãŠããããªããžã§ã¯ããžã®åç §ããããã©ããã«é¢ä¿ãªããã¬ããŒãžã³ã¬ã¯ã¿ã¯äœæããããã¹ãŠã®ãªããžã§ã¯ããèšæ¶ããŸãã
å®éãã¬ããŒãžã³ã¬ã¯ã¿ãŒã¯éåžžã«åå§çã«æ§ç¯ãããŠããŸãïŒå®éã«ã¯ããã§ã¯ãããŸããããåäœåçã¯éåžžã«åçŽã§ãïŒãæ¢åã®ãã¹ãŠã®ãªããžã§ã¯ããé ç¹ã§ããããªããžã§ã¯ãããä»ã®ãªããžã§ã¯ããžã®ãªã³ã¯ããšããžã§ããã°ã©ãããããŸãã ãã®ã°ã©ãã®ããã€ãã®é ç¹ã¯ç¹å¥ã§ãã ãããã¯ãã¬ããŒãžã³ã¬ã¯ã·ã§ã³ã®ã«ãŒãã®ã«ãŒãã§ããã·ã¹ãã ã«ãã£ãŠäœæãããä»ã®ãªããžã§ã¯ããããããåç §ããŠãããã©ããã«é¢ä¿ãªãååšãç¶ãããšã³ãã£ãã£ã§ãã ç¹å®ã®ãªããžã§ã¯ãããä»»æã®ã«ãŒããžã®ã°ã©ãäžã®ãã¹ãããå Žåã«ã®ã¿ããªããžã§ã¯ãã¯ã¬ããŒãžã³ã¬ã¯ã¿ã«ãã£ãŠç Žæ£ãããŸããã
ããã¯åé¡ã§ã-ãªããžã§ã¯ããç Žæ£ãããªãå Žåãã«ãŒããããã®ãªããžã§ã¯ããžã®ãªã³ã¯ã®ãã§ãŒã³ããããŸãïŒãŸãã¯ããã®ãããªãã§ãŒã³ãååšããªãå Žåã次ã®ã¬ããŒãžã³ã¬ã¯ã·ã§ã³äžã«ãªããžã§ã¯ããç Žæ£ãããŸãïŒãã€ãŸãããªããžã§ã¯ãã¯ãªãŒã¯ã§ããŸãããçšèªã®å³å¯ãªæå³ã§ã®èšæ¶ã å®éã«ã¯ãã¬ããŒãžã³ã¬ã¯ã¿ãŒèªäœãã·ã¹ãã å ã®æ¢åã®ãã¹ãŠã®ãªããžã§ã¯ããžã®ãªã³ã¯ãä¿åããŠãããšããäºå®ã§ããååã§ãã
Javaã§ãã¯ãªãŒã³ãªãã¡ã¢ãªãªãŒã¯ãååŸããããšããè©Šã¿ã¯ç¹°ãè¿ãè¡ãããŠããŸãã ããã¡ããåŒãç¶ãè¡ãããŠããŸãããã¡ã¢ãªã解æŸããã«ãªããžã§ã¯ããžã®åç §ãã¬ããŒãžã³ã¬ã¯ã¿ã«å¿ããããã¡ãœããã¯ãããŸããã ã¡ã¢ãªã®ãã€ãã£ãã³ãŒãå²ãåœãŠïŒJNIïŒã«é¢é£ããã¡ã¢ãªãªãŒã¯ããããŸããããã®èšäºã§ã¯ããããèæ ®ããŸããã
çµè«ïŒ é¢å¿ã®ãããªããžã§ã¯ããžã®ãã¹ãŠã®ãªã³ã¯ã倱ãå¯èœæ§ããããŸãããã¬ããŒãžã³ã¬ã¯ã¿ãŒã¯èšæ¶ããŠããŸãã
ãããã£ãŠãå³å¯ãªæå³ã§ã®ãã¡ã¢ãªãªãŒã¯ãã®å®çŸ©ã¯ãç§ãã¡ã«ã¯é©ããŠããŸããã ãããã£ãŠãã¡ã¢ãªãªãŒã¯ã¯ãç Žæ£ããå¿ èŠãããåŸãååšãç¶ãããªããžã§ã¯ããšããŠããã«ç解ããŸãã
次ã«ãã¡ã¢ãªãªãŒã¯ã®æãäžè¬çãªã±ãŒã¹ã®ããã€ãã瀺ããããããæ€åºããæ¹æ³ãšããããåé¿ããæ¹æ³ã瀺ããŸãã ãããã®å žåçãªã¡ã¢ãªãªãŒã¯ãä¿®æ£ããããšãåŠç¿ããå Žåã99.9ïŒ ã®ç¢ºçã§ãã¢ããªã±ãŒã·ã§ã³ã«å¿é ããå¿ èŠã®ããã¡ã¢ãªãªãŒã¯ã¯ãããŸããã
ãããããããã®äžè¬çãªãšã©ãŒã®èª¬æã«é²ãåã«ãäž»ãªè³ªåã«çããå¿ èŠããããŸãããããã®ãšã©ãŒããã¹ãŠä¿®æ£ããå¿ èŠããããŸããïŒ ã¢ããªã±ãŒã·ã§ã³ã¯åäœããŠããŸã...
ã¡ã¢ãªãªãŒã¯ã®ãã©ãã«ã·ã¥ãŒãã£ã³ã°ã«æéãè²»ããã®ã¯ãªãã§ããïŒ
ãªãœãŒã¹ãdrawable-ldpiãã©ã«ããŒã«è©°ã蟌ãã®ãå¿ãããããã¢ããªã±ãŒã·ã§ã³ã¯é·ãéã¯ã©ãã·ã¥ããŠããŸããã ãã®èšäºãæžãæºåãããŠãç°¡åãªå®éšãè¡ããŸããïŒäœæ¥äžã®ã¢ããªã±ãŒã·ã§ã³ã®1ã€ãååŸããäœæãããã¢ã¯ãã£ããã£ãã¡ã¢ãªããã¢ã³ããŒããããªãããã«ã¡ã¢ãªãªãŒã¯ãè¿œå ããŸããïŒéçãªã¹ãã«è¿œå ãå§ããŸããïŒã ã¢ããªã±ãŒã·ã§ã³ãéããŠç»é¢ãã¯ãªãã¯ããNexus 5ã§ã¢ããªã±ãŒã·ã§ã³ãæçµçã«ã¯ã©ãã·ã¥ããã®ãåŸ ã¡ãŸãããæåŸã«ã5å55ç»é¢åŸã«ã¢ããªã±ãŒã·ã§ã³ãã¯ã©ãã·ã¥ããŸããã ç®èãªããšã«ãGoogleã¢ããªãã£ã¯ã¹ã«ãããšãéåžžããŠãŒã¶ãŒã¯ã»ãã·ã§ã³ããšã«3ã€ã®ç»é¢ã«ã¢ã¯ã»ã¹ããŸãã
ã ããããŠãŒã¶ãŒãæ°ä»ããªããããããªãå Žåãã¡ã¢ãªãªãŒã¯ãå¿é ããå¿ èŠããããŸããïŒ ã¯ããããã«ã¯3ã€ã®çç±ããããŸãã
ãŸã ãã¢ããªã±ãŒã·ã§ã³ã§æ©èœããªãã¯ãã®äœããæ©èœããå Žåãããã¯éåžžã«æ·±å»ã§ãããã°ãå°é£ãªåé¡ã«ã€ãªããå¯èœæ§ããããŸãã
ããšãã°ããœãŒã·ã£ã«ãããã¯ãŒã¯çšã®ã¢ããªã±ãŒã·ã§ã³ãéçºããŸããã ãã®ã¢ããªã±ãŒã·ã§ã³ã§ã¯ããŠãŒã¶ãŒéã§ã¡ãã»ãŒãžã亀æã§ããŸããã¡ãã»ãŒãžã³ã°ç»é¢ã«ã¯ãæ°ããã¡ãã»ãŒãžãåä¿¡ããããã«10ç§ããšã«ãµãŒããŒã«èŠæ±ãè¡ãã¿ã€ããŒããããŸãããç»é¢ãçµäºãããšãã«ãã®ã¿ã€ããŒããªãã«ããã®ãå¿ããŠããŸããã ããã¯èŠèŠçã«äœãå°ããŸããïŒ ãªãã§ãããã ã¢ããªã±ãŒã·ã§ã³ãäœãééã£ãããšãããŠããããšã«æ°ä»ããªãã§ãããã ãã ããåæã«ãã¢ããªã±ãŒã·ã§ã³ã¯10ç§ããšã«ãµãŒããŒã«ãªã¯ãšã¹ããéä¿¡ãç¶ããŸãã ã¢ããªã±ãŒã·ã§ã³ãçµäºããåŸã§ãã ç»é¢ããªãã«ããåŸã§ãïŒé»è©±ãšã¯åäœãç°ãªãå ŽåããããŸãïŒã ãŠãŒã¶ãŒã3人ã®ç°ãªãå人ãšã®éä¿¡ç»é¢ã«å ¥ããšã1æé以å ã«ãµãŒããŒãžã®1000ã®è¿œå ãªã¯ãšã¹ããåä¿¡ãã1人ã®ãŠãŒã¶ãŒãã¢ããªã±ãŒã·ã§ã³ã«éåžžã«æã£ãŠãããããªãŒãéäžçã«æ¶è²»ããŸãã ãããã¯ãç»é¢ããªãã«ããŠé»è©±ã§ãã¹ãã¢ããªã±ãŒã·ã§ã³ã䜿çšããŠåŸãçµæã§ãã
ããã¯ã¡ã¢ãªãªãŒã¯ã§ã¯ãªããã¿ã€ããŒããªãã«ãªã£ãŠããªãããšã䞻匵ãããããããŸãããããã¯ãŸã£ããå¥ã®ãšã©ãŒã§ãã é¢ä¿ãããŸãã ã¢ããªã±ãŒã·ã§ã³ã§ã¡ã¢ãªãªãŒã¯ããã§ãã¯ããããšã«ãããä»ã®ãšã©ãŒãèŠã€ããããšãéèŠã§ãã ã¢ããªã±ãŒã·ã§ã³ã§ã¡ã¢ãªãªãŒã¯ããã§ãã¯ãããšããååšããã¯ãã®ãã¹ãŠã®ãªããžã§ã¯ããèŠã€ããããšèããŠããŸãã ãã®ãããªãªããžã§ã¯ããèŠã€ãããšãã©ã®ãããªè¿œå ã®æäœãåŒãç¶ãå®è¡ãããããããã«ããããŸãã
第äºã« ããã¹ãŠã®ã¢ããªã±ãŒã·ã§ã³ãã¡ã¢ãªãã»ãšãã©æ¶è²»ããªãããã§ã¯ãªãããã¹ãŠã®é»è©±æ©ã倧éã®ã¡ã¢ãªãå²ãåœãŠãããã§ããããŸããã
ããã5åã§55ã®ã¢ã³ããŒããããç»é¢ã®åŸã«ã¯ã©ãã·ã¥ããã¢ããªã±ãŒã·ã§ã³ãèŠããŠããŸããïŒ ãã®ãããåãã¢ããªã±ãŒã·ã§ã³ã®å ŽåãOutOfMemoryExceptionããã¯ã©ãã·ã¥ã®1ã2é±éã®ã¬ããŒããåãåããŸãïŒäž»ã«4.0ãŸã§ã®ããã€ã¹ãããã¢ããªã±ãŒã·ã§ã³ã«ã¯50,000ã®ã€ã³ã¹ããŒã«ããããŸãïŒã ããã¯ãã¢ããªã±ãŒã·ã§ã³ã«ã¡ã¢ãªãªãŒã¯ããªããšããäºå®ã«ããããããã§ãã ãããã£ãŠãç¹ã«ã¢ããªã±ãŒã·ã§ã³ã倧éã®ã¡ã¢ãªãæ¶è²»ããå Žåãã¡ã¢ãªãªãŒã¯ã®ããã¢ããªã±ãŒã·ã§ã³ãã¬ã€ã¢ãŠãããããšã§ãã«ã«ããããªãæãªãããšãã§ããŸãã ã¢ã³ããã€ãã®äžçã§ã¯ãã€ãã®ããã«ãéé ·ãªãã¬ãŒã³ãã¯ç§ãã¡ãèŒãããæªæ¥ããåŒãé¢ããŸãã
第äžã«ãç·ã¯ãã¹ãŠãè¡ãããšãã§ããªããã°ãªããŸããïŒ ïŒç§ã¯3ã€ã®çç±ãã¹ãŠãæ·±å»ã«ãªããšçŽæããïŒ
ã¡ã¢ãªãªãŒã¯ããã£ããããããã«èª¬åŸãããã®ã§ãããããçºçããäž»ãªçç±ãèŠãŠã¿ãŸãããã
éçå€æ°ã«ã¢ã¯ãã£ããã£åç §ïŒãã¥ãŒããã©ã°ã¡ã³ãããµãŒãã¹ïŒãä¿åããªã
ãã¹ãŠã®åå¿è éçºè ãçŽé¢ããæåã®è³ªåã®1ã€ã¯ãããã¢ã¯ãã£ããã£ãã次ã®ã¢ã¯ãã£ããã£ã«ãªããžã§ã¯ãã転éããæ¹æ³ã§ãã ç§ãå®æçã«èŠãŠããæãåçŽã§æãééã£ã解決çã¯ãæåã®ã¢ã¯ãã£ããã£ãéçå€æ°ã«æžã蟌ã¿ã2çªç®ã®ã¢ã¯ãã£ããã£ãããã®å€æ°ã«ã¢ã¯ã»ã¹ããããšã§ãã ããã¯éåžžã«å€±æããã¢ãããŒãã§ãã ã¡ã¢ãªãªãŒã¯ãå³åº§ã«åŒãèµ·ããã ãã§ãªãïŒã¢ããªã±ãŒã·ã§ã³ãååšããéãéçå€æ°ã¯ååšãç¶ãããããåç §ããã¢ã¯ãã£ããã£ã¯ã¢ã³ããŒããããŸããïŒã ãŸãããã®æ¹æ³ã§ã¯ããŠãŒã¶ãŒã«è¡šç€ºãããªãç»é¢ã¯ãŠãŒã¶ãŒãæ»ã£ããšãã«ã®ã¿ãã€ã§ãç Žæ£ããã³åäœæã§ãããããééã£ãç»é¢ãšæ å ±ã亀æããç¶æ³ã«ã€ãªããå¯èœæ§ããããŸãã
ã¢ã¯ãã£ããã£ã®æŒãããªããããªã«å€§ããªåé¡ãªã®ã§ããïŒ å®éã®ãšãããã¬ããŒãžã³ã¬ã¯ã¿ãŒãã¢ã¯ãã£ããã£ãåéããªãå Žåããã¹ãŠã®ãã¥ãŒãšãã©ã°ã¡ã³ããããã³ã¢ã¯ãã£ããã£äžã«ããä»ã®ãã¹ãŠã®ãªããžã§ã¯ãã¯åéãããŸããã åçãå«ãã¯ãªãªãŒã¹ãããŸããã ãããã£ãŠãã¢ã¯ãã£ããã£ã®ãªãŒã¯ã¯ãååãšããŠãã¢ããªã±ãŒã·ã§ã³ã§çºçããå¯èœæ§ãããæ倧ã®ã¡ã¢ãªãªãŒã¯ã§ãã
éçå€æ°ãžã®ã¢ã¯ãã£ããã£åç §ãèšè¿°ããªãã§ãã ããã Intentãä»ãããªããžã§ã¯ãã®è»¢éã䜿çšããããäžè¬çã«ã¯ãªããžã§ã¯ãã§ã¯ãªããªããžã§ã¯ãã®IDã転éããŸãïŒãã®IDãååŸã§ããããŒã¿ããŒã¹ãããå ŽåïŒã
ãã®é ç®ã¯ãAndroidã«ãã£ãŠçŽæ¥ãŸãã¯éæ¥çã«ã©ã€ãã¿ã€ã ãå¶åŸ¡ããããªããžã§ã¯ãã«ãé©çšãããŸãã ã€ãŸã 衚瀺ããã©ã°ã¡ã³ãããµãŒãã¹ãªã©ã«ã
ãã¥ãŒããã³ãã©ã°ã¡ã³ããªããžã§ã¯ãã«ã¯ãããããé 眮ãããŠããã¢ã¯ãã£ããã£ãžã®ãªã³ã¯ãå«ãŸããŠããããã1ã€ã®ãã¥ãŒããªãŒã¯ããå Žåããã¹ãŠ-ã¢ã¯ãã£ããã£ãšãã®äžã®ãã¹ãŠã®ãã¥ãŒãããã«ãªãŒã¯ããŸããç»é¢ããã®ãªã³ã¯ããããŸãïŒ
ã¢ã¯ãã£ããã£ãžã®ãªã³ã¯ïŒãã¥ãŒããã©ã°ã¡ã³ãããµãŒãã¹ïŒãä»ã®ãªããžã§ã¯ãã«è»¢éãããšãã¯æ³šæããŠãã ãã
ç°¡åãªäŸãèããŠã¿ãŸãããããœãŒã·ã£ã«ãããã¯ãŒã¯ã®ã¢ããªã±ãŒã·ã§ã³ã§ã¯ãã¢ããªã±ãŒã·ã§ã³ã®åç»é¢ã«çŸåšã®ãŠãŒã¶ãŒã®å§ãåãè©äŸ¡ã衚瀺ãããŸãã çŸåšã®ãŠãŒã¶ãŒã®ãããã¡ã€ã«ãæã€ãªããžã§ã¯ãã¯ãã¢ã«ãŠã³ãã«ãã°ã€ã³ããŠããçµäºãããŸã§ååšããã¢ããªã±ãŒã·ã§ã³ã®ãã¹ãŠã®ç»é¢ãåããªããžã§ã¯ãã§æ å ±ãæ±ããŸãã ãã®ãªããžã§ã¯ãã¯ãè©äŸ¡ãé »ç¹ã«å€æŽãããå¯èœæ§ãããããããµãŒããŒããã®ããŒã¿ãå®æçã«æŽæ°ããŸãã ãããã¡ã€ã«ãæã€ãªããžã§ã¯ãã¯ãè©äŸ¡ã®æŽæ°ã®çŸåšã®ã¢ã¯ãã£ããã£ãéç¥ããå¿ èŠããããŸãã ãããéæããæ¹æ³ã¯ïŒ éåžžã«ã·ã³ãã«ïŒ
@Override protected void onResume() { super.onResume(); currentUser.addOnUserUpdateListener(this); }
ãã®ç¶æ³ã§ã¡ã¢ãªãªãŒã¯ãéæããæ¹æ³ã¯ïŒ ãŸããéåžžã«ç°¡åã§ãïŒ onPauseã¡ãœããã§éç¥ã®è³Œèªã解é€ããã®ãå¿ããŠãã ããïŒ
@Override protected void onPause() { super.onPause(); /* */ currentUser.removeOnUserUpdateListener(this); }
ãã®ãããªã¡ã¢ãªãªãŒã¯ãåå ã§ãç»é¢ããŠãŒã¶ãŒã«è¡šç€ºãããªããªã£ãåŸã§ãããããã¡ã€ã«ãæŽæ°ããããã³ã«ã¢ã¯ãã£ããã£ãã€ã³ã¿ãŒãã§ã€ã¹ãæŽæ°ãç¶ããŸãã ããã«æªãããšã«ããã®æ¹æ³ã§ã¯ãç»é¢ã¯åãéç¥ã«å¯ŸããŠ2ã3ããŸãã¯ãã以äžã®åæ°çœ²åã§ããŸãã ããã«ããããã®ç»é¢ã ãã§ãªãããããã¡ã€ã«ã®æŽæ°æã«ã€ã³ã¿ãŒãã§ã€ã¹ãã¬ãŒãã衚瀺ãããå¯èœæ§ããããŸãã
ãã®ãšã©ãŒãåé¿ããã«ã¯ã©ãããã°ããã§ããïŒ
ãŸã ããã¡ãããã¢ã¯ãã£ããã£ãããã¯ã°ã©ãŠã³ããé¢ãããšãã«ããã¹ãŠã®éç¥ã®è³Œèªã解é€ããããšãåžžã«æ³šææ·±ãç£èŠããå¿ èŠããããŸãã
次㫠ãã¢ããªã±ãŒã·ã§ã³ã§ã¡ã¢ãªãªãŒã¯ãå®æçã«ãã§ãã¯ããå¿ èŠããããŸãã
第äžã«ãåé¡ã«å¯Ÿãã代æ¿ã¢ãããŒãããããŸãïŒãªããžã§ã¯ããžã®ãªã³ã¯ã§ã¯ãªãã 匱ããªã³ã¯ãä¿åã§ããŸã ã ããã¯ããã¥ãŒã¯ã©ã¹ã®çžç¶äººã«ãšã£ãŠç¹ã«äŸ¿å©ã§ããonPauseã¡ãœããããªããããã©ã®æç¹ã§éç¥ã®è³Œèªã解é€ãã¹ãããæ確ã§ã¯ãããŸããã 匱ããªã³ã¯ã¯ã¬ããŒãžã³ã¬ã¯ã¿ãŒãšãªããžã§ã¯ãéã®ãªã³ã¯ãšã¯èŠãªãããªãããã匱ããªã³ã¯ã®ã¿ãååšãããªããžã§ã¯ãã¯ç Žæ£ããããªã³ã¯ã¯ãªããžã§ã¯ãã®åç §ãåæ¢ããnullã«ãªããŸãã 䜿çšããã®ã«ããŸã䟿å©ã§ã¯ãªã匱ããªã³ã¯ãããããªãããã«ããããã次ã®ãã³ãã¬ãŒãã¯ã©ã¹ã䜿çšã§ããŸãã
public class Observer<I> { private ArrayList<I> strongListeners = new ArrayList<I>(); private ArrayList<WeakReference<I>> weakListeners = new ArrayList<WeakReference<I>>(); public void addStrongListener(I listener) { strongListeners.add(listener); } public void addWeakListener(I listener) { weakListeners.add(new WeakReference<I>(listener)); } public void removeListener(I listener) { strongListeners.remove(listener); for (int i = 0; i < weakListeners.size(); ++i) { WeakReference<I> ref = weakListeners.get(i); if (ref.get() == null || ref.get() == listener) { weakListeners.remove(i--); } } } public List<I> getListeners() { ArrayList<I> activeListeners = new ArrayList<I>(); activeListeners.addAll(strongListeners); for (int i = 0; i < weakListeners.size(); ++i) { WeakReference<I> ref = weakListeners.get(i); I listener = ref.get(); if (listener == null) { weakListeners.remove(i--); continue; } activeListeners.add(listener); } return activeListeners; } }
ããã¯æ¬¡ã®ããã«åäœããŸãïŒ
public class User { ... public interface OnUserUpdateListener { public void onUserUpdate(); } private Observer<OnUserUpdateListener> updateObserver = new Observer<OnUserUpdateListener>(); public Observer<OnUserUpdateListener> getUpdateObserver() { return updateObserver; } } ... @Override protected void onFinishInflate() { super.onFinishInflate(); /* */ currentUser.getUpdateObserver().addWeakListener(this); } /* ... */ ...
ã¯ãããã®ãã¥ãŒã®äžèŠãªæŽæ°ãååŸã§ããŸãã ããããå€ãã®å Žåãããã¯æªã®å°ãªãæ¹ã§ãã ãŸããã©ã®ãããªç¶æ³ã§ããã¡ã¢ãªãªãŒã¯ã¯çºçããŸããã
addWeakListenerã¡ãœããã䜿çšãããšãã®åŸ®åŠãªç¹ã¯1ã€ã ãã§ããè¿œå ãããªããžã§ã¯ãã誰ããåç §ããå¿ èŠããããŸãã ããããªããšãã¬ããŒãžã³ã¬ã¯ã¿ãŒã¯æåã®éç¥ãåãåãåã«ãã®ãªããžã§ã¯ããç Žæ£ããŸãã
/* ! */ currentUser.getUpdateObserver().addWeakListener(new OnUserUpdateListener() { @Override public void onUserUpdate() { /* */ } });
ç»é¢ãçµäºãããšãã«ãã£ã³ã»ã«ãããªãã¿ã€ããŒãšã¹ã¬ãã
ãã®åé¡ã«ã€ããŠã¯æ¢ã«äžã§èª¬æããŸããããœãŒã·ã£ã«ãããã¯ãŒã¯çšã®ã¢ããªã±ãŒã·ã§ã³ãéçºããŸããã ãã®ã¢ããªã±ãŒã·ã§ã³ã§ã¯ããŠãŒã¶ãŒéã§ã¡ãã»ãŒãžã亀æã§ããæ°ããã¡ãã»ãŒãžãåä¿¡ããããã«10ç§ããšã«ãµãŒããŒãžã®èŠæ±ãè¡ãã¿ã€ããŒãã¡ãã»ãŒãžã³ã°ç»é¢ã«è¿œå ããŸãããç»é¢ãçµäºãããšãã«ãã®ã¿ã€ããŒããªãã«ããã®ãå¿ããŸããïŒ
public class HandlerActivity extends Activity { private Handler mainLoopHandler = new Handler(Looper.getMainLooper()); private Runnable queryServerRunnable = new Runnable() { @Override public void run() { new QueryServerTask().execute(); mainLoopHandler.postDelayed(queryServerRunnable, 10000); } }; @Override protected void onResume() { super.onResume(); mainLoopHandler.post(queryServerRunnable); } @Override protected void onPause() { super.onPause(); /* */ /* mainLoopHandler.removeCallbacks(queryServerRunnable); */ } ... }
æ®å¿µãªããããã®åé¡ãåé¿ããããšã¯å°é£ã§ãã äžããããšãã§ãã2ã€ã®ãã³ãã¯ãåã®æ®µèœãšåãã§ããã¡ã¢ãªãªãŒã¯ããªããã泚æããŠå®æçã«ã¢ããªã±ãŒã·ã§ã³ã確èªããŠãã ããã 匱ããªã³ã¯ã䜿çšããŠãåã®æ®µèœãšåæ§ã®ã¢ãããŒãã䜿çšããããšãã§ããŸã ã
ã¢ã¯ãã£ããã£å ã®ãã©ã°ã¡ã³ããŸãã¯å¥ã®ãã©ã°ã¡ã³ããžã®åç §ãä¿åããªã
ãã®ãšã©ãŒã¯äœåºŠãèŠãŸããã ç»é¢ã«ã¯åžžã«1ã€ãã衚瀺ãããŠããŸããããã¢ã¯ãã£ããã£ã¯5ã6åã®å®è¡äžã®ãã©ã°ã¡ã³ããžã®ãªã³ã¯ãä¿åããŸãã ç°ãªãæéã«ç»é¢ã«è¡šç€ºããããã©ã°ã¡ã³ãã¯ãçŽæ¥ãã£ãã·ã¥ããããªã³ã¯ãä»ããŠäºãã«éä¿¡ããŸãã ãã®ãããªã¢ããªã±ãŒã·ã§ã³ã§ã¯ãFragmentManagerã¯ã»ãšãã©ã®å Žååæ©çãªåœ¹å²ãæãããŸã-é©åãªã¿ã€ãã³ã°ã§ã³ã³ããã®å 容ãç®çã®ãã©ã°ã¡ã³ãã«çœ®ãæãããã©ã°ã¡ã³ãèªäœã¯ããã¯ã¹ã¿ãã¯ã«è¿œå ãããŸããïŒããã¯ã¹ã¿ãã¯ã«çŽæ¥ãªã³ã¯ããŠãããã©ã°ã¡ã³ããè¿œå ãããšãé ããæ©ããã«ã€ãªãããŸããã©ã°ã¡ã³ãã¯ã¡ã¢ãªããã¢ã³ããŒããããŸã;ãã®ãã©ã°ã¡ã³ãã«æ»ã£ãåŸãæ°ãããã©ã°ã¡ã³ããäœæããããªã³ã¯ã¯åŒãç¶ãæ¢åã®ãªã³ã¯ã«ãªã³ã¯ãããŸããããŠãŒã¶ãŒãã©ã°ã¡ã³ãã«ã¯è¡šç€ºãããŸããïŒã
ããã¯ããã€ãã®çç±ã§éåžžã«æªãã¢ãããŒãã§ãã
ãŸããã¢ã¯ãã£ããã£ã«5ã6åã®ãã©ã°ã¡ã³ããžã®çŽæ¥ãªã³ã¯ãä¿åããå Žåã5ã6åã®ã¢ã¯ãã£ããã£ãžã®ãªã³ã¯ãä¿åããå Žåãšåãã§ãã ã¢ã¯ãã£ããã£ã®å®è¡äžã¯ãã€ã³ã¿ãŒãã§ã€ã¹å šäœããã¹ãŠã®ç»åãããã³5ã€ã®æªäœ¿çšãã©ã°ã¡ã³ãã®ãã¹ãŠã®ããžãã¯ãã¡ã¢ãªããã¢ã³ããŒãã§ããŸããã
第äºã«ããããã®ãã©ã°ã¡ã³ãã¯åå©çšãéåžžã«é£ãããªããŸãã ãã©ã°ã¡ã³ãããã©ã°ã¡ã³ããxãyãzãšåãã¢ã¯ãã£ããã£ã§å®è¡ããå¿ èŠãããå Žåããã©ã°ã¡ã³ããããã°ã©ã å ã®å¥ã®å Žæã«è»¢éããŠãã ããã転éããå¿ èŠã¯ãããŸããã
ãã©ã°ã¡ã³ããã¢ã¯ãã£ããã£ãšããŠæ±ããŸãã ããããå¯èœãªéãã¢ãžã¥ãŒã«åããã¢ã¯ãã£ããã£ãšfragmentManagerãä»ããŠã®ã¿ãã©ã°ã¡ã³ãéã§éä¿¡ããŸãã ããã¯éåžžã«è€éãªã·ã¹ãã ã®ããã«èŠãããããããŸããããªã³ã¯ãæž¡ãã ãã§ããã®ã«ããªããããªã«äžçæžåœåªåããã®ã§ããããïŒ ããããå®éã«ã¯ããã®ã¢ãããŒãã«ããããã°ã©ã ãããè¯ãç°¡åã«ãªããŸãã
ãã®ãããã¯ã«é¢ããGoogleã®åªããå ¬åŒèšäºãä»ã®ãã©ã°ã¡ã³ããšã®éä¿¡ãããããŸãã ãã®èšäºãèªã¿çŽããŠãã¹ãããããžã®ãã€ã³ã¿ãå床ä¿åããªãã§ãã ããã
äžè¬ã«ãŒã«
åã®4ã€ã®æ®µèœãèªãã åŸãå®éã«ã¯éãããªãããšã«æ°ä»ããããããŸããã ãããã¯ãã¹ãŠã1ã€ã®äžè¬èŠåã®ç¹æ®ãªã±ãŒã¹ã§ãã
ãã¹ãŠã®ã¡ã¢ãªãªãŒã¯ã¯ãã©ã€ããµã€ã¯ã«ãé·ããªããžã§ã¯ãïŒé·åœãªããžã§ã¯ãïŒã§ã©ã€ããµã€ã¯ã«ãçããªããžã§ã¯ãïŒçåœãªããžã§ã¯ãïŒãžã®åç §ãä¿åããå Žåã«ã®ã¿çºçããŸãã
ããã念é ã«çœ®ããŠãåžžã«ãã®ãããªç¶æ³ã«æ³šæããŠãã ããã
ãã®ã«ãŒã«ã«ã¯ãKISSãYAGNIãRTFMãªã©ã®çŸããçãååã¯ãããŸããããAndroidã®ã¢ã¯ãã£ããã£ã ãã§ãªããã¬ããŒãžã³ã¬ã¯ã¿ãŒãæã€ãã¹ãŠã®èšèªãšãã¹ãŠã®ãªããžã§ã¯ãã«é©çšãããŸãã
ã¡ã¢ãªãªãŒã¯ã®äž»ãªåå ã瀺ããã®ã§ãäœæ¥äžã®ã¢ããªã±ãŒã·ã§ã³ã§ããããèå¥ããããšã«æåŸã«é²ã¿ãŸãããã
ã¢ããªã±ãŒã·ã§ã³å ã®ã©ã®è¡ãä¿®æ£ããããèŠã€ããã«ã¯ãã©ããã¯ãªãã¯ããŸããïŒ
ãããã£ãŠãã¡ã¢ãªãªãŒã¯ãåé¿ããæ¹æ³ã¯ç¥ã£ãŠããŸãããããã¯ãã¡ã¢ãªãªãŒã¯ãåé¿ããæ¹æ³ãåŠç¿ããåã«äœæããã¿ã€ããã¹ããã°ãããã³ãããžã§ã¯ãããããªããä¿è·ããŸããã
ã¢ããªã±ãŒã·ã§ã³ã®ã¡ã¢ãªãªãŒã¯ã®ååšãšåå ãå€æããã«ã¯ãå°ãã®æéãšMATãå¿ èŠã§ãã 以åã«MATã䜿çšããããšããªãå Žåã¯ãEclipseã®ãã©ã°ã€ã³ãšããŠã€ã³ã¹ããŒã«ã ãDDMSããŒã¹ãã¯ãã£ããéãããDump HPROF fileããã¿ã³ãèŠã€ããŸãã ãã®ãã¿ã³ãæŒããšãéžæããã¢ããªã±ãŒã·ã§ã³ã®ã¡ã¢ãªãã³ããéããŸãã Android Studioã䜿çšããå ŽåãçŸæç¹ã§ã¯MATãAndroid Studioã®ãã©ã°ã€ã³ãšããŠãŸã ååšããªããããããã»ã¹ã¯ããå°ãè€éã«ãªããŸãã MATãå¥ã®ããã°ã©ã ãšããŠé 眮ãã stackoverflowã§åœä»€ã䜿çšããŸãã
次ã®æé ã«åŸã£ãŠãã ããïŒ
- ã³ã³ãã¥ãŒã¿ãŒã«æ¥ç¶ãããŠããããã€ã¹ã«ã¢ããªã±ãŒã·ã§ã³ãã€ã³ã¹ããŒã«ãããã¹ãŠã®ç»é¢ã«å°ãªããšã1å衚瀺ãããããã«äœ¿çšããŸãã 1ã€ã®ç»é¢ãç°ãªããã©ã¡ãŒã¿ãŒã§éãããšãã§ããå Žåã¯ãå¯èœãªãã¹ãŠã®ãã©ã¡ãŒã¿ãŒã®çµã¿åããã§ç»é¢ãéããŠã¿ãŠãã ããã äžè¬çã«ã¯ããªãªãŒã¹åã«ãã§ãã¯ããŠããããã«ãã¢ããªã±ãŒã·ã§ã³å šäœã調ã¹ãŸãã ãã¹ãŠã®ç»é¢ãå®äºããããã¢ããªã±ãŒã·ã§ã³ãçµäºãããŸã§æ»ããã¿ã³ãæŒããŸãã ããŒã ãã¿ã³ãæŒããªãã§ãã ãã-ããªãã®ã¿ã¹ã¯ã¯ãå®è¡äžã®ãã¹ãŠã®ã¢ã¯ãã£ããã£ãå®äºããããšã§ãããããããé衚瀺ã«ããã ãã§ã¯ãããŸããã
- Cause GCãã¿ã³ãæ°åæŒããŸãã ããããªããšããã³ãã«ã¯ãã¬ããŒãžã³ã¬ã¯ã¿ãŒã«ãã£ãŠç Žå£ããããããŸã ç Žå£ãããŠããªããªããžã§ã¯ãã衚瀺ãããŸãã
- ãHPROFãã¡ã€ã«ã®ãã³ãããã¿ã³ãã¯ãªãã¯ããŠãã¢ããªã±ãŒã·ã§ã³ã®ã¡ã¢ãªããã³ãããŸãã
- éãããŠã£ã³ããŠã§ãOQLã¯ãšãªãäœæããŸãããSELECT * FROM instanceof android.app.Activityã
çµæã®ãªã¹ãã¯ç©ºã§ãªããã°ãªããŸããã ãªã¹ãã«å°ãªããšã1ã€ã®èŠçŽ ãããå Žåããã®èŠçŽ ã¯ã¡ã¢ãªãªãŒã¯ã§ãã ã¹ã¯ãªãŒã³ã·ã§ããã§ã¯ããŸãã«ãã®ãããªèŠçŽ ãèŠãããšãã§ããŸã-HandlerActivityïŒããã¯ã¡ã¢ãªãªãŒã¯ã§ãã ãªã¹ãå ã®åã¢ã€ãã ã«ã€ããŠãæé 8ã10ãå®è¡ããŸãã - ãã©ã°ã¡ã³ãã®åå«ã«å¯ŸããŠåæ§ã®ã¯ãšãªãå®è¡ããŸãïŒãSELECT * FROM instanceof android.app.Fragmentãã åã®ã±ãŒã¹ãšåæ§ã«ãçµæã®ãªã¹ãã«å«ãŸããã®ã¯ã¡ã¢ãªãªãŒã¯ã ãã§ãã ãããã®ããããã«ã€ããŠãã€ã³ã8-10ãå®äºããŸãã
- ãã¹ãã°ã©ã ãéããŸãã ãã¹ãã°ã©ã ã«è¡šç€ºãããçµæã¯ããªããžã§ã¯ãã§ã¯ãªãã¯ã©ã¹ã衚瀺ãããšããç¹ã§OQLã«è¡šç€ºãããçµæãšã¯ç°ãªããŸãã ãã£ã«ã¿ãŒãã£ãŒã«ãã«ãã¯ã©ã¹ã«äœ¿çšããããã±ãŒãžåãå
¥åãïŒã¹ã¯ãªãŒã³ã·ã§ããã§ã¯com.examples.typicalleaksïŒããªããžã§ã¯ãåïŒã·ã¹ãã ã«çŸåšååšãããã®ã¯ã©ã¹ã®ãªããžã§ã¯ãã®æ°ïŒã§çµæããœãŒãããŸãã çµæã«ã¯ããã³ãæã«ã€ã³ã¹ã¿ã³ã¹ã0åååšããã¯ã©ã¹ã衚瀺ãããããšã«æ³šæããŠãã ããã ãããã®ã¯ã©ã¹ã¯èå³ããããŸããã æ¬åœã«å€ãã®ãªããžã§ã¯ããããå Žåã¯ãããŒãã«å
šäœãéžæããå³ã¯ãªãã¯ããŠ[æ£ç¢ºãªä¿æãµã€ãºã®èšç®]ãéžæããŸãã [ä¿æããŒã]ãã£ãŒã«ãã§ããŒãã«ããœãŒãããä¿æããŒãå€ã倧ããïŒããšãã°10000ãè¶
ããïŒãªããžã§ã¯ãã®ã¿ãèæ
®ããŸãã
ä»åã¯ããªã¹ãã«è¡šç€ºããããã¹ãŠã®ã¯ã©ã¹ãªããžã§ã¯ããã¡ã¢ãªãªãŒã¯ã§ã¯ãããŸããã ãã ãããããã®ã¯ã©ã¹ã¯ãã¹ãŠã¢ããªã±ãŒã·ã§ã³ã®ã¯ã©ã¹ã§ããããããã®åã¯ã©ã¹ã®ãªããžã§ã¯ããç¹å®ã®æç¹ã§ããã€ååšããå¿ èŠããããã倧ãŸãã«ç解ããå¿ èŠããããŸãã ããšãã°ãã¹ã¯ãªãŒã³ã·ã§ããã§ã¯ãExampleã¯ã©ã¹ã®6ã€ã®ãªããžã§ã¯ããš1ã€ã®é åExample []ã衚瀺ãããŠããŸãã ããã¯æ£åžžã§ã-Exampleã¯ã©ã¹ã¯åæåã§ããããã®ãªããžã§ã¯ãã¯æåã®åŒã³åºãã§äœæãããã¢ããªã±ãŒã·ã§ã³ãååšããéãååšããŸãã ããããHandlerActivityãšHandlerActivity $ 1ïŒHandlerActivity.javaãã¡ã€ã«å ã§å®£èšãããæåã®å¿åã¯ã©ã¹ïŒã¯ããã§ã«ããç¥ãããŠããã¡ã¢ãªãªãŒã¯ã§ãã çãããã¯ã©ã¹ãå³ã¯ãªãã¯ããŠãªã¹ããªããžã§ã¯ããéžæããçµæã®ãªã¹ããããªããžã§ã¯ãã®1ã€ã«å¯ŸããŠæé 8ã10ãå®è¡ããŸãã - ãã®ã¹ãããã§ãçããããªããžã§ã¯ãã1ã€ãèç©ãããŠããªãå Žåãããã§ãšãããããŸãïŒ ã¢ããªã±ãŒã·ã§ã³ã«é倧ãªã¡ã¢ãªãªãŒã¯ã¯ãããŸããã
- çããããªããžã§ã¯ããå³ã¯ãªãã¯ãã[æçãã¹ãGCã«ãŒãã«ããŒãž]ãéžæããŸã-ãã¹ãŠã®ãã¡ã³ãã /匱ã/ãœãããªã©ãé€å€ããŸãã åç §ã
- ããªãŒãéããŸãã 次ã®ãããªãã®ãåŸãããã¯ãã§ãã
äžçªäžã«ãçããããªããžã§ã¯ãã衚瀺ãããŸãã æäžéšã¯ã¬ããŒãžã³ã¬ã¯ã¿ãŒã®ã«ãŒãã§ãã äžå€®ã®ãã¹ãŠã¯ãçããããªããžã§ã¯ããã¬ããŒãžã³ã¬ã¯ã¿ãŒã®ã«ãŒãã«æ¥ç¶ãããªããžã§ã¯ãã§ãã ã¬ããŒãžã³ã¬ã¯ã¿ãŒãçããããªããžã§ã¯ããç Žå£ããããšãèš±å¯ããªãã®ã¯ããã®ãã§ãŒã³ã§ãã ãã®ãã§ãŒã³ã¯æ¬¡ã®ããã«èªãå¿ èŠããããŸãããªã¹ãã®äžã®ãªããžã§ã¯ãã®å€æ°ã¯å€ªåã§æžãããŠãããå€æ°åã®å³åŽã«ãããªããžã§ã¯ããžã®ãªã³ã¯ãå«ãŸããŠããŸãã ã€ãŸã ã¹ã¯ãªãŒã³ã·ã§ããã§ã¯ãMessageQueueãªããžã§ã¯ãã®å€æ°mMessagesã«ã¯ãMessageãªããžã§ã¯ããžã®åç §ãå«ãŸããŠããŸããMessageãªããžã§ã¯ãã«ã¯ããã®$ 0å€æ°å ã®HandlerActivityãªããžã§ã¯ããžã®åç §ãå«ãHandlerActivity $ 1ãªããžã§ã¯ããåç §ããã³ãŒã«ããã¯å€æ°ãå«ãŸããŠããŸãã ã€ãŸããçãããHandlerActivityãªããžã§ã¯ãã¯ãHandlerActivity.javaãã¡ã€ã«ã§å®£èšãããæåã®Runnableãä¿æããŸããããã¯ãpostãŸãã¯postDelayedã¡ãœããã䜿çšããŠãã³ãã©ãŒã«è¿œå ãããããã§ãã ã¢ããªã±ãŒã·ã§ã³ã®äžéšã§ãããªã¹ãã®äžçªäžããæåŸã®ã¯ã©ã¹ãèŠã€ããŠå³ã¯ãªãã¯ãã[ãœãŒã¹ãã¡ã€ã«ãéã]ãéžæããŸãã - çããããªããžã§ã¯ããäžèŠã«ãªã£ãæç¹ã§ãçããããªããžã§ã¯ããšã¬ããŒãžã³ã¬ã¯ã¿ãŒã®ã«ãŒãã®éã®ãã§ãŒã³ãåæããããã«ãã¢ããªã±ãŒã·ã§ã³ã³ãŒããä¿®æ£ããŸãã ãã®äŸã§ã¯ãonPause HandlerActivityã¡ãœããã§Handler.removeCallbacksïŒRunnable rïŒã¡ãœãããåŒã³åºãã ãã§ãã
- ãã¹ãŠã®çããããªããžã§ã¯ããåŠçããããã¹ããã1ããã¢ã«ãŽãªãºã ãç¹°ãè¿ããŠããã¹ãŠãæ£åžžã«æ©èœããŠããããšã確èªããŸãã
ãããã«
ã¢ããªã±ãŒã·ã§ã³ã®ãã¹ãŠã®ç»é¢ãã¯ãªãã¯ããŠãçããããªããžã§ã¯ããèŠã€ãããªãã£ãå Žåã99.9ïŒ ã®ç¢ºçã§ãã¢ããªã±ãŒã·ã§ã³ã«æ·±å»ãªã¡ã¢ãªãªãŒã¯ã¯ãããŸããã
ãããã®ãã§ãã¯ã¯ãã»ãšãã©ãã¹ãŠã®ã¢ããªã±ãŒã·ã§ã³ã«ãšã£ãŠæ¬åœã«ååã§ãã ã¢ããªã±ãŒã·ã§ã³ã«å®éã«åœ±é¿ãäžããå¯èœæ§ã®ããã¡ã¢ãªãªãŒã¯ã«ã®ã¿é¢å¿ããããŸãã æååuuidãš2ã3ã®çãè¡ãå«ããªããžã§ã¯ãã®æŒæŽ©ã¯ééãã§ãããä¿®æ£ããã ãã®äŸ¡å€ã¯ãããŸããã
åç
§è³æ
- RAM䜿çšéã®èª¿æ»
https://developer.android.com/tools/debugging/debugging-memory.html - Javaããã©ãŒãã³ã¹ããã°
http://kohlerm.blogspot.ru/2009/07/eclipse-memory-analyzer-10-useful.html - ã¡ã¢ãªãªãŒã¯ã®åé¿
http://android-developers.blogspot.co.uk/2009/01/avoiding-memory-leaks.html - Androidã¢ããªã±ãŒã·ã§ã³ã®ã¡ã¢ãªåæ
http://android-developers.blogspot.ru/2011/03/memory-analysis-for-android.html - ã¡ã¢ãªãªãŒã¯ã®æ€åº
http://blog.crowdint.com/2013/10/02/fixing-memory-leaks-in-android-applications.html - åå¿è
åãã®Androidã§ã®ã¡ã¢ãªãªãŒã¯ã®ãããã°ïŒããã°ã©ã ã«ããHEAPãã³ã
http://novoda.com/blog/memory-debugging-on-android-part-1 - ã¢ããªãã¡ã¢ãªããªãŒã¯ããŠãããã©ãããèå¥ããæ¹æ³
http://www.littleeye.co/blog/2013/04/24/identify-memory-leaks-android-apps/ - Androidã¡ã¢ãªãªãŒã¯ã®ä¿®æ£
http://therockncoder.blogspot.ru/2012/09/fixing-android-memory-leak.html - ã¢ããªã®ã¡ã¢ãªã管çãã
https://developer.android.com/training/articles/memory.html - ãªãŒã¯ã®ãã³ãã£ã³ã°ïŒAndroidã§ã®ã¡ã¢ãªç®¡ç
http://www.raizlabs.com/dev/2014/03/wrangling-dalvik-memory-management-in-android-part-1-of-2/
http://www.raizlabs.com/dev/2014/04/hunting-your-leaks-memory-management-in-android-part-2-of-2/ - Androidã§ã¢ããªã±ãŒã·ã§ã³ã®ã¡ã¢ãªäœ¿çšéãæ€åºããæ¹æ³
http://stackoverflow.com/questions/2298208/how-to-discover-memory-usage-of-my-application-in-android/2299813#2299813