首页 > 代码库 > 【OC语法快览】四、基础内存管理

【OC语法快览】四、基础内存管理

Basic Memory Management                                                       

   基础内存管理

If you‘re writing an application for Mac OS X, you have the option to enable garbage collection. In general, this means that you don‘t have to think about memory management until you get to more complex cases. 
如果你正在写运行在Mac OS X上的程序,你可以选择开启垃圾回收功能。除非你遇到更复杂的情况,这样你就不用考虑内存管理了。

However, you may not always be working with an environment that supports garbage collection. In that case, you need to know a few basic concepts. 
但是,你的工作环境不一定都支持垃圾回收。这种情况下,你需要知道一些基本内存管理的概念。

If you create an object using the manual alloc style, you need to releasethe object later. You should not manually release an autoreleased object because your application will crash if you do. 
如果使用人工alloc方式创建实例对象,稍后需要释放掉它。不应该人工释放一个可以自动释放的对象,否则将导致程序奔溃。

Here are two examples:
这有两个例子: 

// string1 will be released automatically
NSString* string1 = [NSString string];

// must release this when done
NSString* string2 = [[NSString alloc] init];
[string2 release];



For this tutorial, you can assume that an automatic object will go away at the end of the current function. 
在这份材料中,你可以假设,一个自动释放的实例对象将会在当前函数结束后自动消失。

There‘s more to learn about memory management, but it will make more sense after we look at a few more concepts.
还有很多需要学习的内存管理知识,但是在我们掌握更多概念之后,学习起来更有效果。






原文:learn_objective_C part 4