【问题标题】:Basic steps for memory management in objective-c [closed]Objective-C中内存管理的基本步骤[关闭]
【发布时间】:2012-04-30 02:59:43
【问题描述】:

我对 Objective-c 语言没有太多经验。我对目标 C 中的内存管理很困惑。我知道内存管理是非常重要的因素,所以我们在开发时必须非常关注这一点。

我的问题是我们必须遵循哪些基本的事情来尽可能减少内存的使用?

【问题讨论】:

标签: iphone objective-c ios xcode memory-management


【解决方案1】:

这是一个很好的问题,因为在 Objective-C 中没有垃圾收集器。我们必须手动处理内存。

当你 alloc it、copy it 或 new it 时,你在 Objective-C 中拥有一个对象。例如(我从http://interfacelab.com/objective-c-memory-management-for-lazy-people/复制了这个示例代码):

    -(void)someMethod
{
  // I own this!
  SomeObject *iOwnThis = [[SomeObject alloc] init];

  [iOwnThis doYourThing];

  // I release this!
  [iOwnThis release];
}

-(void)someOtherMethod:(SomeObject *)someThing
{
  // I own this too!
  SomeObject *aCopyOfSomeThing = [someThing copy];

  [aCopyOfSomeThing doSomething];

  // I release this!
  [aCopyOfSomeThing release];
}

-(void)yetAnotherMethod
{
  // I own this too!
  SomeObject *anotherThingIOwn = [SomeObject new];

  [anotherThingIOwn doSomething];

  // I release this!
  [anotherThingIOwn release];
}

【讨论】:

  • 你完全忽略了ARC的存在。
  • 感谢您的回复,我会检查此链接。
  • @Till:我喜欢这个事实,因为 ARC 允许新手在不知道自己在做什么的情况下制作应用程序 - 并使用低质量的应用程序向 AppStore 发送垃圾邮件。知道自己在做什么的人不需要 ARC。
  • 知道自己在做什么的人使用 ARC 正是因为他们 (a) 知道自己在做什么,并且 (b) 意识到 ARC 解决了一系列问题,而这些问题在 MRR 中无法在不暴露容易出错的情况下解决,臭虫,锁定模式。 ARC 支持垃圾应用的想法很愚蠢。
  • @Jenox 哇。 ARC 是一个有价值的工具,可用于使应用程序更无错误并更快地编写。像所有工具一样,阅读说明手册后,其中一种受益最大。制作一个好的应用程序的关键不是你是否使用 ARC,而是你付出了多少努力来学习。你的评论简直就是拖钓。
【解决方案2】:

也许我见过的最清晰的建议(ARC 之前)来自 Brent Simmons:How I Manage Memory

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-28
    • 1970-01-01
    相关资源
    最近更新 更多