{"id":795,"date":"2016-05-24T06:00:29","date_gmt":"2016-05-24T06:00:29","guid":{"rendered":"http:\/\/yoonhada.com\/wp\/?p=795"},"modified":"2023-12-27T03:09:56","modified_gmt":"2023-12-26T18:09:56","slug":"this-handler-class-should-be-static-or-leaks-might-occur","status":"publish","type":"post","link":"http:\/\/yoonhada.com\/?p=795","title":{"rendered":"This Handler class should be static or leaks might occur"},"content":{"rendered":"<p>\t\t\t\t<span style=\"font-family: '\ub9d1\uc740 \uace0\ub515'; color: #000000; font-size: small;\">\uc6d0\uae00 : <a href=\"http:\/\/www.androiddesignpatterns.com\/2013\/01\/inner-class-handler-memory-leak.html\">http:\/\/www.androiddesignpatterns.com\/2013\/01\/inner-class-handler-memory-leak.html<\/a><\/span><\/p>\n<p>[code lang=&#8221;js&#8221;]<br \/>\npublic class SampleActivity extends Activity<br \/>\n{<br \/>\n\tprivate final Handler mLeakyHandler = new Handler() {<br \/>\n\t    @Override<br \/>\n\t    public void handleMessage(Message msg) {<br \/>\n\t      \/\/ &#8230;<br \/>\n\t    }<br \/>\n\t  }<br \/>\n}<br \/>\n[\/code]<\/p>\n<blockquote><p>In Android, Handler classes should be static or leaks might occur.<\/p><\/blockquote>\n<p><span style=\"font-family: '\ub9d1\uc740 \uace0\ub515'; color: #000000; font-size: medium;\">\uc774\uac83\uc774 \ub098\uc624\ub294 \uc6d0\uc778\uc740<\/span><\/p>\n<div class=\"txc-textbox\" style=\"border: 1px dashed #9fd331; background-color: #e7fdb5; padding: 10px; margin-left: 20px;\"><span style=\"font-family: '\ub9d1\uc740 \uace0\ub515'; color: #000000; font-size: small;\">1. Android Application\uc774 \uc2e4\ud589\ub420 \ub54c, Framework\uc5d0\uc11c\ub294 Application\uc758 \uba54\uc778 \uc4f0\ub808\ub4dc\ub97c \uc704\ud55c Loop Object\ub97c \uc0dd\uc131 \ud55c\ub2e4. <span style=\"color: #ff6600;\">Looper<\/span>\ub294 \ub2e8\uc21c\ud55c \uba54\uc2dc\uc9c0 \ud050\ub97c \uc0dd\uc131\ud558\uc5ec <span style=\"color: #ff6600;\">Message<\/span>\ub4e4\uc744 \ucc98\ub9ac\ud558\uace0, Application\uc758 Lifecycle\uacfc \ub3d9\uc77c\ud55c Lifecycle\uc744 \uac16\ub294\ub2e4.<br \/>\n2. \uba54\uc778 \uc4f0\ub808\ub4dc\uc5d0\uc11c \uc0dd\uc131\ub41c <span style=\"color: #ff6600;\">Handler<\/span>\ub294 Looper\uc758 \uba54\uc2dc\uc9c0 \ud050\uc5d0 \uc18d\ud558\uac8c \ub41c\ub2e4. \uba54\uc2dc\uc9c0 \ud050\ub85c \ubcf4\ub0b4\uc9c4 <span style=\"color: #ff6600;\">Message<\/span>\ub4e4\uc740 Handler\uc5d0 \ub300\ud55c <span style=\"color: #ff6600;\">reference<\/span>\ub97c \uac16\uace0 \uc788\uc5b4\uc57c\ud568, Looper\uac00 \ud574\ub2f9 \uba54\uc2dc\uc9c0\ub97c \ucc98\ub9ac \ud560\uc218 \uc788\ub2e4. Looper\uac00 \uba54\uc2dc\uc9c0\ub97c \ucc98\ub9ac\ud560 \ub54c <span style=\"color: #ff6600;\">Handler#handleMessage(Message)<\/span>\ub97c \ud638\ucd9c\ud574\uc57c \ud558\uae30 \ub54c\ubb38.<br \/>\n3. Java\uc5d0\uc11c\ub294 non-static inner class\uc640 anonymous class\ub294 outer class\uc5d0 \ub300\ud55c implicit reference\ub97c \uac16\uace0, static inner class\uc758 \uacbd\uc6b0\uc5d0\ub294 \uac16\uc9c0 \uc54a\ub294\ub2e4.\u00a0<\/span><\/div>\n<p><span style=\"font-family: '\ub9d1\uc740 \uace0\ub515'; color: #000000; font-size: medium;\">Handler\uc640 Looper, Message Queue\uc758 \uad00\uac8c\uc744 \ud574\uacb0\ud558\uae30 \uc704\ud574 \uc704 \ubb38\uc81c\ub97c \ud574\uacb0\ud558\uae30 \uc704\ud574\uc11c\ub294 Handler\ub97c static inner class\ub85c \uc815\uc758\ud558\uac70\ub098, \uc0c8\ub85c\uc6b4 \ud30c\uc77c\uc5d0 subclass\ud574\uc57c \ud55c\ub2e4. static inner class\ub294 outer class\uc5d0 \ub300\ud55c\u00a0implicit reference\ub97c \uac16\uae30 \uc54a\uae30 \ub54c\ubb38\uc5d0 Activity Context\uac00 Garbage Collect\ub418\ub294 \uac83\uc744 \ubc29\ud574\ud558\uc9c0 \uc54a\uc744 \uac83\uc774\ub2e4. \ub9cc\uc57d Handler \ub0b4\ubd80\uc5d0\uc11c Activity\uc758 method\ub97c \ud638\ucd9c\ud574\uc57c \ud560 \uacbd\uc6b0 Activity\uc758 WeakReference\ub97c \uac16\ub3c4\ub85d \ud55c\ub2e4.<br \/>\n<\/span><\/p>\n<p>[code lang=&#8221;js&#8221;]<br \/>\npublic class SampleActivity extends Activity<br \/>\n{<\/p>\n<p>\t\/**<br \/>\n\t * Instances of static inner classes do not hold an implicit<br \/>\n\t * reference to their outer class.<br \/>\n\t *\/<br \/>\n\tprivate static class MyHandler extends Handler<br \/>\n\t{<br \/>\n\t\tprivate final WeakReference&lt;SampleActivity&gt; mActivity;<\/p>\n<p>\t\tpublic MyHandler( SampleActivity activity )<br \/>\n\t\t{<br \/>\n\t\t\tmActivity = new WeakReference&lt;SampleActivity&gt;( activity );<br \/>\n\t\t}<\/p>\n<p>\t\t@Override<br \/>\n\t\tpublic void handleMessage( Message msg )<br \/>\n\t\t{<br \/>\n\t\t\tSampleActivity activity = mActivity.get();<br \/>\n\t\t\tif( activity != null )<br \/>\n\t\t\t{<br \/>\n\t\t\t\t\/\/ &#8230;<br \/>\n\t\t\t}<br \/>\n\t\t}<br \/>\n\t}<\/p>\n<p>\tprivate final MyHandler mHandler = new MyHandler( this );<\/p>\n<p>\t\/**<br \/>\n\t * Instances of anonymous classes do not hold an implicit<br \/>\n\t * reference to their outer class when they are &quot;static&quot;.<br \/>\n\t *\/<br \/>\n\tprivate static final Runnable sRunnable = new Runnable()<br \/>\n\t{<br \/>\n\t\t@Override<br \/>\n\t\tpublic void run()<br \/>\n\t\t{<br \/>\n\t\t}<br \/>\n\t};<\/p>\n<p>\t@Override<br \/>\n\tprotected void onCreate( Bundle savedInstanceState )<br \/>\n\t{<br \/>\n\t\tsuper.onCreate( savedInstanceState );<\/p>\n<p>\t\t\/\/ Post a message and delay its execution for 10 minutes.<br \/>\n\t\tmHandler.postDelayed( sRunnable, 600000 );<\/p>\n<p>\t\t\/\/ Go back to the previous Activity.<br \/>\n\t\tfinish();<br \/>\n\t}<br \/>\n}<br \/>\n[\/code]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\uc6d0\uae00 : http:\/\/www.androiddesignpatterns.com\/2013\/01\/inner-class-handler-memory-leak.html [code lang=&#8221;js&#8221;] public class SampleActivity extends Activity { private final Handler mLeakyHandler = new Handler() { @Override public void handleMessage(Message msg) { \/\/ &#8230; } } } [\/code] In Android, Handler classes should be static or leaks might occur. \uc774\uac83\uc774 \ub098\uc624\ub294 \uc6d0\uc778\uc740 1. Android Application\uc774 \uc2e4\ud589\ub420 \ub54c, Framework\uc5d0\uc11c\ub294 Application\uc758 \uba54\uc778 \uc4f0\ub808\ub4dc\ub97c \uc704\ud55c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[17,28],"class_list":["post-795","post","type-post","status-publish","format-standard","hentry","category-programming","tag-android","tag-handler"],"_links":{"self":[{"href":"http:\/\/yoonhada.com\/index.php?rest_route=\/wp\/v2\/posts\/795","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/yoonhada.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/yoonhada.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/yoonhada.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/yoonhada.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=795"}],"version-history":[{"count":1,"href":"http:\/\/yoonhada.com\/index.php?rest_route=\/wp\/v2\/posts\/795\/revisions"}],"predecessor-version":[{"id":2197,"href":"http:\/\/yoonhada.com\/index.php?rest_route=\/wp\/v2\/posts\/795\/revisions\/2197"}],"wp:attachment":[{"href":"http:\/\/yoonhada.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=795"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/yoonhada.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=795"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/yoonhada.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=795"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}