Objective-C(iOS)中的基本内存管理 [英] Basic memory-management in objective-c (ios)

查看:87
本文介绍了Objective-C(iOS)中的基本内存管理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于Objective-C和iOS开发,我还很陌生,我目前正在尝试掌握如何进行内存管理.我在非ARC btw中的应用.

I am pretty new to Objective-C and iOS-development, and I am currently trying to grasp how to do memory-management. My app in non-ARC btw.

除了行belove之外,没有在代码中的任何地方(不是.h或其他任何东西)声明此对象.使用完后是否需要以任何方式释放/释放此对象以清除其空间,还是自动完成?

This object is not declared anywhere in the code (not .h or anything) other than the line belove. Do I need to release/dealloc this object in any way to clear the space for it when I am done using it, or is this done automatically?

NSMutableURLRequest *restRequest = [[NSMutableURLRequest alloc] init];


这一点也是如此.不知道这是否是同一个问题,但是在这里我不使用alloc&初始化后再使用它.这有什么区别吗?


The same goes for this one. Not sure if this is the same question, but here I don't use the words alloc & init before using it. Does that make any difference?

NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];


在这种情况下,我将在.h文件中定义该对象并将其保留.这是否意味着除非我释放/取消分配该变量,否则该变量将始终位于内存中(一旦被初始化一次).我想是这样的话,那是我应该在卸载视图时在视图中做的事情吗?


In this case, I am defining the object in the .h-file as well as retaining it. Does this mean that the variable will always be in memory (when initialized once obsly) unless I release/dealloc it? I guess if that is the case, that is something I should do in views when the view is unloaded?

@interface Storeage : NSObject {
    NSString *deviceToken;
}
@property (nonatomic, retain) NSString *deviceToken;

在.m文件中,我将像在第一种或第二种情况下那样分配和使用该对象(似乎没有什么区别).

In the .m-file I will alloc and use this object like in the first or second case (does not seems to make any difference).

如果这个问题很愚蠢,请多多包涵.我习惯于使用GC进行低级Java编程.

Please bear with me if this question is stupid. I am used to low level Java-programming with GC.

推荐答案

使用完该对象后,是否需要以任何方式释放/释放该对象以清除其空间,还是自动完成此操作?

Do I need to release/dealloc this object in any way to clear the space for it when I am done using it, or is this done automatically?

由于未使用ARC,因此需要手动向其发送release消息以处理其所有权. (很好的建议:不要以释放内存"的方式思考.引用计数意味着您增加和减少引用计数,拥有和停止拥有对象,丢失所有引用后释放对象)是自动完成的.换句话说,release不一定意味着解除分配.)

Since you are not using ARC, you need to manually send it a release message in order to dispose of its ownership. (Good piece of advice: don't think in terms of "freeing memory". Reference counting means that you increase and decrease reference counts, you get to own and cease to own objects, the deallocation of an object upon having lost all its references is done automatically. In other words, release does not necessarily mean deallocation.)

这一点也是如此.不知道这是否是相同的问题,但是在这里我不使用alloc&初始化后再使用它.这有什么区别吗?

The same goes for this one. Not sure if this is the same question, but here I don't use the words alloc & init before using it. Does that make any difference?

是的.您仅拥有使用allocnewcopymutableCopy创建的对象或使用retain引用的对象.您在这里都不做任何事,因此也不必担心将其发布. (从技术上讲,这是一个自动释放的实例,将被返回,运行循环将负责处理它.)

It does. You only own objects that you create using alloc, new, copy, mutableCopy or reference using retain. You do neither one here, so you don't have to worry about releasing it either. (Technically, it's an autoreleased instance that will be returned, the run loop will take care of it.)

在.m文件中,我将像在第一种或第二种情况下那样分配和使用该对象(似乎没有什么区别).

In the .m-file I will alloc and use this object like in the first or second case (does not seems to make any difference).

区分实例变量和属性.如果将属性设置器方法声明为retainstrong,它将保留(增加引用计数)对象.但这仅在使用accessor方法时才是正确的,而在直接访问实例变量时则不然.如果您是这样写的:

Make the difference between instance variables and properties. A property setter method, if declared as retain or strong, will retain (increase the reference count of) your object. But that's true only if you use the accessor method, and not when you access the instance variable directly. If you wrote this:

variable = [[SomeObject alloc] init];

然后,您需要像使用其他任何(本地)变量一样释放它:

then you need to release it just like you would do with any other (local) variable:

[variable release];

如果使用访问器方法进行设置:

If you use the accessor method to set it:

self.variable = [[[SomeObject alloc] init] autorelease];

然后在创建它时必须使用autorelease(否则它将具有2的引用计数,并且您将泄漏内存).

then you have to use autorelease when creating it (else it will have a reference count of 2 and you'll leak memory).

在两种情况下,您都可以使用self.variable = nil;放弃所有权. 这仅适用于物业.

In both cases, you can also use self.variable = nil; to relinquish ownership. This only works for properties.

所有这些随着ARC的引入而发生了根本性的变化,出于以下三个原因,我在这里不作解释:

All this radically changes with the introduction of ARC, which I don't explain here for three reasons:

  1. 无论如何我都不是ARC专家;

  1. I'm not an ARC expert by any means;

我想鼓励您在尝试ARC之前完全学习MRC(您似乎打算这样做);

I'd like to encourage you to learn MRC (which you seem to have the intention to) perfectly before trying ARC;

这不是问题.

这篇关于Objective-C(iOS)中的基本内存管理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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