메모리 관리 Memory management

Memory management is the programming discipline of managing the life cycles of objects and freeing them when they are no longer needed. 

메모리 관리는 오브젝트의 라이프 사이클 관리와 필요 없을때 해제 하는 프로그래밍 제어 방법을 말한다. 

Managing object memory is a matter of performance; if an application doesn’t free unneeded objects, its memory footprint grows and performance suffers. 

오브젝트 메모리 관리는 더 나은 퍼포먼스를 위해서 하는것이다; 어플리케이션이 필요없는 오브젝트를 해제 하지 않으면, 메모리공간을 차지 하고 퍼모먼스를 악화 시킨다. 

Memory management in a Cocoa application that doesn’t use garbage collection is based on a reference counting model. 

가비지 컬렉션을 사용하지 않는 Cocoa 어플리케이션의 메모리 관리는 레퍼런스 카운팅 모델을 바탕으로 하고 있다. 

When you create or copy an object, its retain count is 1. 

오브젝트를 create 하거나 copy 할때, retain count(유지 카운트) 는 1 이다. 

Thereafter other objects may express an ownership interest in your object, which increments its retain count. 

그 후에 다른 개체들은 당신의 객체에 ownership(소유권)을 명시(추가)할 수 있다,  이것은 retain count 를 증가 시킨다. (주고 retain 명령으로 추가함.)

The owners of an object may also relinquish their ownership interest in it, which decrements the retain count. When the retain count becomes zero, the object is deallocated (destroyed).

객체의 소유자들이 객체들의 ownership 연결이 줄어 들게 되면, retain count는 줄어 든다. retain count가 0이 되면, 객체는 해제 된다. 

To assist you in memory management, Objective-C gives you methods and mechanisms that you must use in conformance with set of rules.

메모리 관리를 돕기위해, Objective-C는 메소드와 방법을 제공할것이다. 당신은 적절한 해결 방법을 사용하면 된다.  

Note: In Mac OS X, you can either explicitly manage memory or use the garbage collection feature of Objective-C. Garbage collection is not available in iOS.

가비지 콜렉션은 iOS에서는 사용할수 없다. 


메모리 관리 규칙 Memory-Management Rules

Memory-management rules, sometimes referred to as the ownership policy, help you to explicitly manage memory in Objective-C code.

메모리 관리 규칙, 가끔 소유권 정책을 확인하자,  Objective-C 코드의 메모리 관리에 도움을 준다. 

  • You own any object you create by allocating memory for it or copying it.

      어떤 객체든 

    메모리에 할당하거나 복사해서 만들어 소유해야 한다. 
  • 관련 메소드들 Releated methodallocallocWithZone:copycopyWithZone:

    mutableCopymutableCopyWithZone:

  • If you are not the creator of an object, but want to ensure it stays in memory for you to use, you can express an ownership interest in it

    . 자신이 만든 객체가 아닌, 그러나 그것을 메모리에서 계속 사용하길 원한다면, 소유권(ownership)을 명시 해야한다. 

    Related method: retain 

  • If you own an object, either by creating it or expressing an ownership interest, you are responsible for releasing it when you no longer need it.

     만들거나 소유권을 획득한 객체를 소유하고 있다면, 더이상 사용하지 않을때는 해제를 해야할 책임이 있다. 

    Related methods: releaseautorelease

  • Conversely, if you are not the creator of an object and have not expressed an ownership interest, you mustnot release it.

    반대로, 만들거나 소유권을 가지고 있지 않은 객체는 해제 하지 않아도 된다. 

If you receive an object from elsewhere in your program, it is normally guaranteed to remain valid within the method or function it was received in. If you want it to remain valid beyond that scope, you should retain or copy it. If you try to release an object that has already been deallocated, your program crashes.

메모리 관리 형태 Aspects of Memory Management

The following concepts are essential to understanding and properly managing object memory: 다음의 개념들은 올바른 객체 메모리 관리를 이해하기 위한 기초이다 :

  • Autorelease pools. Sending autorelease to an object marks the object for later release, which is useful when you want the released object to persist beyond the current scope. Autoreleasing an object puts it in an autorelease pool (an instance of NSAutoreleasePool), which is created for a arbitrary program scope. When program execution exits that scope, the objects in the pool are released.

  • Deallocation. When an object’s retain count drops to zero, the runtime calls the dealloc method of the object’s class just before it destroys the object. A class implements this method to free any resources the object holds, including objects pointed to by its instance variables.

  • Factory methods. Many framework classes define class methods that, as a convenience, create objects of the class for you. These returned objects are not guaranteed to be valid beyond the receiving method’s scope.



참조 : 
footprint : 접지면, (컴퓨터) 가 차지하는 공간, 메모리 차지 공간을 얘기 하는 듯. 
relinquish : 버리다, 포기하다. 

+ Recent posts