苹果官方关于内存管理的介绍(原文+翻译)(一)

简介: 苹果官方关于内存管理的介绍(原文+翻译)(一)

About Memory Management



关于内存管理



苹果原文档地址

Application memory management is the process of allocating memory during your program’s runtime, using it, and freeing it when you are done with it. A well-written program uses as little memory as possible. In Objective-C, it can also be seen as a way of distributing ownership of limited memory resources among many pieces of data and code. When you have finished working through this guide, you will have the knowledge you need to manage your application’s memory by explicitly managing the life cycle of objects and freeing them when they are no longer needed.


应用程序内存管理是一个在程序运行时分配内存、使用内存和释放内存的过程。一个优秀的程序应该尽可能少地使用内存。在OC中,内存管理被看作是在众多数据、代码中分配有限的内存资源所有权的方法。当你阅读完本指南以后,你将明确如何管理对象的生命周期,并且在不需要的时候释放它们。


# 重点词汇
*   [memory management] 内存管理
*   [allocating]分配,分派( allocate的现在分词 );  把…拨给
*   [runtime]运行时间
*   [freeing]解除 ; 免除,释放( free的现在分词 ) ; 使…免除,使…释放,使…摆脱
*   [done with]完毕
*   [distributing]散发 ; 分销; 分配( distribute的现在分词 ); 将…分类(into)
*   [working through]作业经历法,消解
*   [explicitly]明白地,明确地
*   [life cycle]生活周期; 经济周期
*   [no longer]不再,已不


Although memory management is typically considered at the level of an individual object, your goal is actually to manage object graphs. You want to make sure that you have no more objects in memory than you actually need.


虽然内存管理通常是被认为是单个对象级别的,但实际上你的目标是管理一个有向图。你要保证在内存中的对象没有你实际需要的多。


# 重点词汇
*   [typically]通常;  典型地; 代表性地
*   [want to]要; <口>应该


2466108-2c6dee177e457a50.webp.jpg


图片.png

At a Glance



一目了然



Objective-C provides two methods of application memory management.

  1. In the method described in this guide, referred to as “manual retain-release” or MRR, you explicitly manage memory by keeping track of objects you own. This is implemented using a model, known as reference counting, that the Foundation class NSObject provides in conjunction with the runtime environment. In the method described in this guide, referred to as “manual retain-release” or MRR, you explicitly manage memory by keeping track of objects you own. This is implemented using a model, known as reference counting, that the Foundation class NSObject provides in conjunction with the runtime environment.


  1. In Automatic Reference Counting, or ARC, the system uses the same reference counting system as MRR, but it inserts the appropriate memory management method calls for you at compile-time. You are strongly encouraged to use ARC for new projects. If you use ARC, there is typically no need to understand the underlying implementation described in this document, although it may in some situations be helpful. For more about ARC, see Transitioning to ARC Release Notes.


OC提供了两种应用程序内存管理的方法:


第一种是在本指南中被称作“手动引用释放”或者“MRR”的方法,通过跟踪你的对象来显式地管理内存。它是通过在运行时提供的基于NSObject的引用计数来实现的。


第二种是自动引用计数,或者叫“ARC”,它有着和MRC一样的引用计数系统,但是它会在编译时为你调用适当的内存管理方法。强烈建议你在新项目中使用ARC。如果你使用ARC,在通常情况下不需要理解本文档中描述的底层实现,但是在某些情况下还有有帮助的。想要更多关于ARC的信息,请看《Transitioning to ARC Release Notes》。(过渡到ARC的发布说明)


# 重点词汇
*   [referred to as]被称为…
*   [reference counting]参考[引用]计数
*   [in conjunction with]与…协力
*   [compile-time]编译时间
*   [underlying]潜在的,含蓄的; 基础的; 表面下的,下层的; 优先的;


Good Practices Prevent Memory-Related Problems

良好的实践可以避免引发和内存相关的问题


There are two main kinds of problem that result from incorrect memory management:


1.Freeing or overwriting data that is still in use

This causes memory corruption, and typically results in your application crashing, or worse, corrupted user data.


2.Not freeing data that is no longer in use causes memory leaks

A memory leak is where allocated memory is not freed, even though it is never used again. Leaks cause your application to use ever-increasing amounts of memory, which in turn may result in poor system performance or your application being terminated.


有两种问题是由错误的内存管理所引起的:


1.释放或者重写仍在使用的数据

这会导致内存损坏,并且通常会导致你的程序崩溃,甚至更糟,用户数据丢失。


2.不释放不再使用的数据会导致内存泄漏

内存泄漏就是即使分配的内存不再使用了也不被释放。内存泄漏会导致你的应用程序占用也来越多的内存,这转而可能会导致系统性能变差,或者使你的应用程序被终止。


#重点词汇
*   [overwriting]盖写法,重写法;
*   [corruption]腐败; 贪污; 贿赂; 变体;
*   [or worse]或者更坏,或者更糟;
*   [corrupted] 破坏; 败坏( corrupt的过去式和过去分词 ) ; 腐化; 引起错误;
*   [ever-increasing]不断增长的
*   [in turn]依次; 转而; 轮流地; 相应地;


Thinking about memory management from the perspective of reference counting, however, is frequently counterproductive, because you tend to consider memory management in terms of the implementation details rather than in terms of your actual goals. Instead, you should think of memory management from the perspective of object ownership and object graphs.


然而,从引用计数的角度考虑内存管理往往适得其反,因为您会倾向于考虑内存管理的实现细节,而不是它的实际目标。取而代之地,你应该从对象的依赖关系和对象图的角度去考虑内存管理。


#重点词汇
*   perspective   透镜,望远镜; 观点,看法; 远景,景色; 洞察力;
*   [counterproductive] 反生产的,使达不到预期目标的
*   [in terms of] 根据;用…的话; 就…而言; 以…为单位;


Cocoa uses a straightforward naming convention to indicate when you own an object returned by a method.

当你拥有一个函数返回的对象时,Cocoa 使用一个简单的命名规则。


See Memory Management Policy.

参考 Memory Management Policy


Although the basic policy is straightforward, there are some practical steps you can take to make managing memory easier, and to help to ensure your program remains reliable and robust while at the same time minimizing its resource requirements.


虽然基本策略很简单,但您可以采取一些切实可行的策略来使内存管理更容易,并帮助确保程序保持可靠和健壮,同时最小化其资源需求。


See Practical Memory Management.(实用内存管理)

参考 Practical Memory Management


Autorelease pool blocks provide a mechanism whereby you can send an object a “deferred” release message. This is useful in situations where you want to relinquish ownership of an object, but want to avoid the possibility of it being deallocated immediately (such as when you return an object from a method). There are occasions when you might use your own autorelease pool blocks.


自动释放池语法块提供了一种机制,通过这种机制你可以给一个对象发送一个“延迟的”释放消息。当你想放弃一个对象的所有权,但是又想避免对象被立即释放的情况下,这种机制非常有用(例如当你从一个方法中返回一个对象时)。有些时候你可能会使用你自己的自动释放池语法块。


See Using Autorelease Pool Blocks.

参考 Using Autorelease Pool Blocks


#重点词汇
*   [straightforward] 直截了当的; 坦率的; 明确的; 直截了当地;坦率地;
*   [naming convention] 命名约定
*   [indicate] 表明,标示,指示; 象征,暗示,预示;
*   [robust] 精力充沛的; 坚定的; 粗野的,粗鲁的; 需要体力的;
*   [minimizing] 极小化,求最小参数值; 把…减至最低数量[程度];
*   [mechanism] 机制,机能,[乐]机理; 结构,机械装置[作用],结构;
*   [whereby] 借以; 通过…; 与…一致
*   [deferred] 延期的,缓召的; 拖延,延缓,推迟
*   [relinquish] 放弃; 让出; 放开,松手; 撤离;


Use Analysis Tools to Debug Memory Problems

使用分析工具来调试内存问题


To identify problems with your code at compile time, you can use the Clang Static Analyzer that is built into Xcode.

你可以使用Xcode内建的Clang静态语法分析工具,在编译期间发现代码的问题。


If memory management problems do nevertheless arise, there are other tools and techniques you can use to identify and diagnose the issues.

如果内存管理问题仍然出现,你还可以使用其它工具和技能来识别和诊断问题。


  • Many of the tools and techniques are described in Technical Note TN2239, iOS Debugging Magic, in particular the use of NSZombie to help find over-released object.


在Technical Note TN2239, iOS Debugging Magic中描述了许多工具和技术,尤其是NSZombie的使用,可以帮助发现被多度释放的对象。


你可以使用Instruments来跟踪引用计数事件和查找内存泄露。参考Collecting Data on Your App


# 重点词汇
*   [nevertheless] 不过; 然而; 仍然; 尽管如此;


目录
相关文章
|
1月前
|
存储 安全 Java
jdk21的外部函数和内存API(MemorySegment)(官方翻译)
本文介绍了JDK 21中引入的外部函数和内存API(MemorySegment),这些API使得Java程序能够更安全、高效地与JVM外部的代码和数据进行互操作,包括调用外部函数、访问外部内存,以及使用不同的Arena竞技场来分配和管理MemorySegment。
46 1
jdk21的外部函数和内存API(MemorySegment)(官方翻译)
|
存储 Go iOS开发
苹果官方关于内存管理的介绍(原文+翻译)(二)
苹果官方关于内存管理的介绍(原文+翻译)(二)
110 0
|
JavaScript 前端开发 程序员
|
3月前
|
存储 编译器 C语言
【C语言篇】数据在内存中的存储(超详细)
浮点数就采⽤下⾯的规则表⽰,即指数E的真实值加上127(或1023),再将有效数字M去掉整数部分的1。
381 0
|
28天前
|
存储 C语言
数据在内存中的存储方式
本文介绍了计算机中整数和浮点数的存储方式,包括整数的原码、反码、补码,以及浮点数的IEEE754标准存储格式。同时,探讨了大小端字节序的概念及其判断方法,通过实例代码展示了这些概念的实际应用。
59 1
|
1月前
|
存储
共用体在内存中如何存储数据
共用体(Union)在内存中为所有成员分配同一段内存空间,大小等于最大成员所需的空间。这意味着所有成员共享同一块内存,但同一时间只能存储其中一个成员的数据,无法同时保存多个成员的值。
|
1月前
|
存储 弹性计算 算法
前端大模型应用笔记(四):如何在资源受限例如1核和1G内存的端侧或ECS上运行一个合适的向量存储库及如何优化
本文探讨了在资源受限的嵌入式设备(如1核处理器和1GB内存)上实现高效向量存储和检索的方法,旨在支持端侧大模型应用。文章分析了Annoy、HNSWLib、NMSLib、FLANN、VP-Trees和Lshbox等向量存储库的特点与适用场景,推荐Annoy作为多数情况下的首选方案,并提出了数据预处理、索引优化、查询优化等策略以提升性能。通过这些方法,即使在资源受限的环境中也能实现高效的向量检索。
|
1月前
|
存储 编译器
数据在内存中的存储
数据在内存中的存储
42 4
|
1月前
|
存储 Java
JVM知识体系学习四:排序规范(happens-before原则)、对象创建过程、对象的内存中存储布局、对象的大小、对象头内容、对象如何定位、对象如何分配
这篇文章详细地介绍了Java对象的创建过程、内存布局、对象头的MarkWord、对象的定位方式以及对象的分配策略,并深入探讨了happens-before原则以确保多线程环境下的正确同步。
56 0
JVM知识体系学习四:排序规范(happens-before原则)、对象创建过程、对象的内存中存储布局、对象的大小、对象头内容、对象如何定位、对象如何分配