zoukankan      html  css  js  c++  java
  • 为什么需要在TypedArray后调用recycle

    当我们没有在使用TypedArray后调用recycle,编译器会提示“This TypedArray should be recycled after use with #recycle()”。

    官方的解释是:回收TypedArray,以便后面重用。在调用这个函数后,你就不能再使用这个TypedArray。

    在TypedArray后调用recycle主要是为了缓存。当recycle被调用后,这就说明这个对象从现在可以被重用了。TypedArray 内部持有部分数组,它们缓存在Resources类中的静态字段中,这样就不用每次使用前都需要分配内存。你可以看看TypedArray.recycle()中的代码:

     1 /**
     2  * Give back a previously retrieved StyledAttributes, for later re-use.
     3  */
     4 public void recycle() {
     5     synchronized (mResources.mTmpValue) {
     6         TypedArray cached = mResources.mCachedStyledAttributes;
     7         if (cached == null || cached.mData.length < mData.length) {
     8             mXml = null;
     9             mResources.mCachedStyledAttributes = this;
    10         }
    11     }
    12 }

    参考链接

    http://stackoverflow.com/questions/13805502/why-do-you-have-to-recycle-a-typedarray

    http://developer.android.com/reference/android/content/res/TypedArray.html#recycle%28%29

  • 相关阅读:
    CentOS7安装MySQL5.7
    .gdbinit文件配置
    Linux 动态库加载
    GDB常用调试命令(二)
    git删除缓存区中文件
    git添加空文件夹
    Linux 打开core dump功能
    C++ 预处理器
    C++ 模板
    C++ 命名空间
  • 原文地址:https://www.cnblogs.com/kissazi2/p/4049982.html
Copyright © 2011-2022 走看看