在Android中的堆内存中是否始终可以使用strings.xml的所有字符串?

Sha*_*rad 11 string android

假设在apk中我们在strings.xml中有1000个字符串.

因此,当我们在设备上运行此应用程序时,所有字符串始终在堆内存中可用.或者当我们调用getString()Context的方法来加载字符串时,它们会加载到内存中.

运行时是否所有字符串都在堆中?

Nac*_*chi 8

这是一个很好的问题.我们可以通过研究Android源代码来尝试找到答案.

所有呼叫getString()路由到了下面的方法android.content.res.AssetManager:

/**
 * Retrieve the string value associated with a particular resource
 * identifier for the current configuration / skin.
 */
/*package*/ final CharSequence getResourceText(int ident) {
    synchronized (this) {
        TypedValue tmpValue = mValue;
        int block = loadResourceValue(ident, (short) 0, tmpValue, true);
        if (block >= 0) {
            if (tmpValue.type == TypedValue.TYPE_STRING) {
                return mStringBlocks[block].get(tmpValue.data);
            }
            return tmpValue.coerceToString();
        }
    }
    return null;
}
Run Code Online (Sandbox Code Playgroud)

loadResourceValue是一个本机函数,可以在android_util_AssetManager.cpp中找到

本机方法getResource()ResTable 这里调用类.

这里ResTable调用的构造函数读取应用程序中每个包ID的资源.此表稍后用于执行资源查找.addInternal()

所以要回答你的问题,是的,所有字符串(实际上所有资源)都从文件系统中解析出来并加载到本机堆中的查找表中.


归档时间:

查看次数:

414 次

最近记录:

8 年,10 月 前