<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    統(tǒng)計(jì)

    留言簿(1)

    DB

    Others

    QA

    Tech Website

    閱讀排行榜

    評(píng)論排行榜

    【轉(zhuǎn)】Avoiding Memory Leaks


    剛開始學(xué)習(xí)Anroid開發(fā),沒(méi)有什么實(shí)際經(jīng)驗(yàn),最后提出的四條建議,第二條還是不很理解,做個(gè)記號(hào),以后解決。為了深入學(xué)習(xí),打算閱讀《深入Java虛擬機(jī)》。
    四條優(yōu)化建議:
  • Do not keep long-lived references to a context-activity (a reference to an activity should have the same life cycle as the activity itself)
  • Try using the context-application instead of a context-activity
  • Avoid non-static inner classes in an activity if you don't control their life cycle, use a static inner class and make a weak reference to the activity inside. The solution to this issue is to use a static inner class with a WeakReference to the outer class, as done in ViewRoot and its W inner class for instance
  • A garbage collector is not an insurance against memory leaks

  • ------------------

    Android applications are, at least on the T-Mobile G1, limited to 16 MB of heap. It's both a lot of memory for a phone and yet very little for what some developers want to achieve. Even if you do not plan on using all of this memory, you should use as little as possible to let other applications run without getting them killed. The more applications Android can keep in memory, the faster it will be for the user to switch between his apps. As part of my job, I ran into memory leaks issues in Android applications and they are most of the time due to the same mistake: keeping a long-lived reference to a Context.

    On Android, a Context is used for many operations but mostly to load and access resources. This is why all the widgets receive a Context parameter in their constructor. In a regular Android application, you usually have two kinds of Context, Activity and Application. It's usually the first one that the developer passes to classes and methods that need a Context:

    @Override
    protected void onCreate(Bundle state) {
    super.onCreate(state);

    TextView label = new TextView(this);
    label
    .setText("Leaks are bad");

    setContentView
    (label);
    }

    This means that views have a reference to the entire activity and therefore to anything your activity is holding onto; usually the entire View hierarchy and all its resources. Therefore, if you leak the Context ("leak" meaning you keep a reference to it thus preventing the GC from collecting it), you leak a lot of memory. Leaking an entire activity can be really easy if you're not careful.

    When the screen orientation changes the system will, by default, destroy the current activity and create a new one while preserving its state. In doing so, Android will reload the application's UI from the resources. Now imagine you wrote an application with a large bitmap that you don't want to load on every rotation. The easiest way to keep it around and not having to reload it on every rotation is to keep in a static field:

    private static Drawable sBackground;

    @Override
    protected void onCreate(Bundle state) {
    super.onCreate(state);

    TextView label = new TextView(this);
    label
    .setText("Leaks are bad");

    if (sBackground == null) {
    sBackground
    = getDrawable(R.drawable.large_bitmap);
    }
    label
    .setBackgroundDrawable(sBackground);

    setContentView
    (label);
    }

    This code is very fast and also very wrong; it leaks the first activity created upon the first screen orientation change. When a Drawable is attached to a view, the view is set as a callback on the drawable. In the code snippet above, this means the drawable has a reference to the TextView which itself has a reference to the activity (the Context) which in turns has references to pretty much anything (depending on your code.)

    This example is one of the simplest cases of leaking the Context and you can see how we worked around it in the Home screen's source code (look for the unbindDrawables() method) by setting the stored drawables' callbacks to null when the activity is destroyed. Interestingly enough, there are cases where you can create a chain of leaked contexts, and they are bad. They make you run out of memory rather quickly.

    There are two easy ways to avoid context-related memory leaks. The most obvious one is to avoid escaping the context outside of its own scope. The example above showed the case of a static reference but inner classes and their implicit reference to the outer class can be equally dangerous. The second solution is to use the Application context. This context will live as long as your application is alive and does not depend on the activities life cycle. If you plan on keeping long-lived objects that need a context, remember the application object. You can obtain it easily by calling Context.getApplicationContext() or Activity.getApplication().

    In summary, to avoid context-related memory leaks, remember the following:

    • Do not keep long-lived references to a context-activity (a reference to an activity should have the same life cycle as the activity itself)
    • Try using the context-application instead of a context-activity
    • Avoid non-static inner classes in an activity if you don't control their life cycle, use a static inner class and make a weak reference to the activity inside. The solution to this issue is to use a static inner class with a WeakReference to the outer class, as done in ViewRoot and its W inner class for instance
    • A garbage collector is not an insurance against memory leaks

    轉(zhuǎn)自:http://developers.androidcn.com/resources/articles/avoiding-memory-leaks.html

    posted on 2011-06-01 14:52 XXXXXX 閱讀(345) 評(píng)論(0)  編輯  收藏 所屬分類: Android Dev

    主站蜘蛛池模板: 免费A级毛片无码A∨免费| 国产啪精品视频网免费| 少妇性饥渴无码A区免费| 无遮免费网站在线入口| 亚洲裸男gv网站| 亚洲午夜电影在线观看| 黄色毛片免费观看| 久久午夜夜伦鲁鲁片免费无码影视 | 久久久无码精品亚洲日韩按摩 | 亚洲hairy多毛pics大全| 久久免费国产精品| 毛片免费视频播放| 四虎国产精品免费久久影院| 久久狠狠高潮亚洲精品| 91九色视频无限观看免费| 亚洲色婷婷一区二区三区| 亚洲AV无码专区在线电影成人| 免费视频成人片在线观看| 久久国产成人精品国产成人亚洲| 国内精品久久久久影院亚洲| 久久99国产综合精品免费| 国产V亚洲V天堂无码| 亚洲一级片免费看| 亚洲精品无码久久毛片| 97人妻精品全国免费视频| 亚洲精品福利在线观看| 全部免费毛片在线播放| 91亚洲自偷在线观看国产馆| 午夜一级毛片免费视频| 伊人久久亚洲综合影院首页| 成人免费视频69| 亚洲欧洲尹人香蕉综合| 污污网站免费观看| 久久亚洲精品成人| 全免费a级毛片免费看| 亚洲高清国产AV拍精品青青草原| 91精品导航在线网址免费| 春暖花开亚洲性无区一区二区| 国产在线不卡免费播放| 国产亚洲精品91| 亚洲成a人片在线观看老师|