Objective-C:所有字符串文字是否总是加载到内存中? [英] Objective-C: Are all string literals always loaded into memory?

查看:52
本文介绍了Objective-C:所有字符串文字是否总是加载到内存中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

UIViewController子类中,经常看到:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = NSLocalizedString(@"Home", nil);
    }
    return self;
}

  1. 为什么不在-viewDidLoad中设置self.title?

是否所有的字符串字面量总是加载到内存中?

Are all string literals always loaded into memory?

推荐答案

字符串文字被编译到您的可执行文件中 - 它们不是资源.它们保存在可执行文件的初始化静态数据部分中.所以是的,在最基本的层面上,无论何时加载可执行文件,它们都在内存中 - 也就是说,每当程序运行时.

String literals are compiled into your executable file - they are not resources. They are kept in the initialized static data section in the executable. So yes, on the most basic level they are in memory whenever the executable is loaded - that is, whenever the program is running.

虽然有分页.有时,当内存不足时,系统可能会将正在运行的可执行文件的一部分丢出内存以释放一些内存,并在需要时重新加载它们.这个过程是自动的、透明的和不可预测的.因此,字符串在某个时间点物理上不在内存中的可能性很小,但是一旦您尝试访问它,它就会神奇地在那里.任何分页都不会基于每个字符串进行 - 它以 4-8 KB(页面")为单位进行.

There is paging though. Somtetimes, when the memory runs low, it's possible that the system throws parts of your running executable out of memory to free up some, and reloads them once they're needed. This process is automatic, transparent, and unpredictable. So there's a minor chance that the string is not physically in memory at some point in time, but once you try to access it, it will magically be there. Any paging is never done on per-string basis - it's done in units of 4-8 KB ("pages").

这篇关于Objective-C:所有字符串文字是否总是加载到内存中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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