在Android的堆内存中,strings.xml的所有字符串是否始终可用? [英] Are all strings of strings.xml are always available in heap memory in Android?

查看:73
本文介绍了在Android的堆内存中,strings.xml的所有字符串是否始终可用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Suppose in an apk we have 1000 strings in strings.xml.

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

So when we run this app on device at that time all the strings are always available in heap memory. Or they loaded in memory when we call getString() method of Context to load the Strings.

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

Are all strings in the heap during runtime?

推荐答案

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

This is a great question. We can try to find the answer by studying the Android source code.

所有对 getString()的调用都会路由到

All calls to getString() get routed to the following method inside 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;
}

loadResourceValue 是本机函数,可以在

loadResourceValue is a native function and can be found in android_util_AssetManager.cpp

本机方法在类 ResTable 中调用 getResource()

The native method calls getResource() inside the class ResTable here.

ResTable 的构造函数调用 addInternal()

The constructor for ResTable calls addInternal() here which reads the resources for each package id in the app. This table is later used to perform lookups of resources.

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

So to answer your question, yes, all strings (and indeed all resources) are parsed from the filesystem and loaded into a lookup table in the native heap.

这篇关于在Android的堆内存中,strings.xml的所有字符串是否始终可用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆