iOS5 프로그래밍에 대한 스터디 시작과 동시에 만나게 되는 것이 @autoreleasepool이라는 블럭이다. 소스 파일 main.m에 보인다.
#import <uikit/uikit.h>
#import "AppDelegate.h"
int main(int argc, char *argv[])
{
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegateclass]));
}
}
이는 메모리 관리 방식에 대한 선언이라고 볼 수 있는데, @autoreleasepool은 메모리 참조 수 계산 방식(Reference-counted Memory Management System)에서 쓰는 선언이고 쓰레기 수집 방식(Garbage Collection) 과 대조되는 방식이다.
Objective-C가 제공하는 3가지 메모리 관리 방식
MRR(Manual retain-release)
개발자에 의해서 의도적으로 관리되는 방식. 참조 수 계산 방식으로 관리 된다.
ARC(Automatic Reference Count)
MRR과 같은 참조 수 계산 방식을 사용하지만 컴파일 시 적절한 메모리 관리 함수 호출문을 넣어 처리한다.
Garbage Collection
더이상 참조되지 않는 객체를 자동으로 제거하는 방식으로 MMR, ARC와는 많이 다른 메타니즘을 제공한다. MAC OS X에서만 쓸 수 있다.
NSAutoreleasePool
Cocoa의 메모리 참조 수 계산 방식(Reference-counted Memory Management System)을 지원하는 클래스
Automatic Reference Counting(ARC)를 사용하면 autorelease pools를 직접 사용할 수 없기 때문에 @autoreleasepool 블럭을 사용한다.
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; // Code benefitting mode a local autorelease pool. [pool release] }
@autoreleasepool {
// Code benefitting mode a local autorelease pool.
}
ARC를 사용하지 않더라도 @autoreleasepool 블럭을 이용 가능 하다.
이 글은 스프링노트에서 작성되었습니다.


