Objective-C: Are all string literals always loaded into memory?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-05 19:00:17

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.

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").

In general, string literals are part of the data section in the compiled Mach-O file. Since all of the code is loaded "into memory" when being executed, that means that the string literals are always loaded into memory as well. This being said, it is still a good idea to retain/release strings just as you would other objects, even if you know that they are going to be literals.

In the example you provided, the NSLocalizedString call is being used for Internationalization. This has nothing to do (memory wise) with the string that you are supplying.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!