Cocoa memory management 101

Cocoa memory management 101,第1张

概述  from:http://www.dikant.de/2007/08/23/cocoa-memory-management-101/ Ok, so here it comes, my first Cocoa posting. I am pretty new to Cocoa and therefore this might be something you are more than famil

 

from:http://www.dikant.de/2007/08/23/cocoa-memory-management-101/

Ok,so here it comes,my first Cocoa posting. I am pretty new to Cocoa and therefore this might be something you are more than familiar with,but for me it is handy to have a quick reference written down where I can look up things. So today I would like to talk about memory management. My coding background lies about 70% in Java,20% in PHP and 10% in C/C++. So in the past mostly I did not have to worry about memory management topics,although I am familiar with the low level C/C++ way of allocating and releasing memory. Cocoa uses an approach somehow in the middle between automatic garbage collection and manual management. Once you get used to it,it is a very powerful tool and you have a much better control over the memory consumption in your applications than in Java while being less error prone than the C/C++ mechanism.

If you want to dive into the details of Cocoa memory management,I recommend you read the Memory management Programming Guide For Cocoa from Apple. For me it was the main source of information about this topic.

 

The retain cycle

Cocoa objects use an Attribute called retainCount which keeps track how many other objects claim ownership to it. Whenever you want to keep a reference to an object you send it the retain message,which will increase its retainCount. When you don’t need the reference any more you send a release message and the count will decrease. Once theretainCount drops to 0,the object will be disposed and the memory freed.

The most important rule you have to remember is:

Whenever you create an object via methods that contain allocnew andcopy in their name,or you increase the retainCount via retain you need to release that object in the same context.

For example let’s take a look at the following method:

+ (voID)doSomething{    ID *myObject = [[SomeObject alloc] init]// do something with the object     [myObject release];}

We are creating a method variable pointing to a freshly created object. The variable will be disposed when we exit the method,therefore we need to make sure to release the object it is pointing to before exiting the method.

The same principal applIEs to instance variables of a class. Here you need to make sure that you release all the instance variables this class might have created,before disposing the class object. This is usually done in the dealloc method of the class.

When working with instance variables you should follow the best practice to access the variables only through their accessor methods. That way you can ensure a transparent handling of the release and retain operations. Let’s take a look at a typical setter method:

- )setname:(Nsstring *)aname{ aname [aname copy]; [name release]; name = aname;}

So the setter first creates a new copy of the object,then releases the old object and last it will store the reference to the new object in the instance variable. The order of the operations is especially importing. Because if we would first release the old object,and the new object would be a pointer to the old object,we would loose our data.

Now that the setter always allocates a new object,we need to make sure to release it when the class is destroyed:

)dealloc{ [super dealloc}

We don’t have to worry about the name object being nil,because Objective-C can send messages to nil-objects without generating null-Pointer exceptions or crashing.

autorelease objects

So naturally the next question is: How do we return objects we have created insIDe a method when we have to release them before we exit the method?

This is the domain of the autorelease objects. Instead of sending an object the releasemessage,you Could send it the autorelease message,which will mark that object for laterdeletion. So,the retainCount will be reduced,but the object will not be disposed and we can use it in the code that called our method.

So how does this work? Basically it adds the object to a so called autorelease pool. This pool is initialized automatically every time an event cycle is triggered (for example via a MouseDown event). During the event cycle,all autorelease objects will be added to that pool and they will be disposed once the event cycle has finished. This means that you don’t have to worry about retaining an autorelease object as long as you use it only during a single user action. You only need to retain it if you want to store it between user actions.

If you stick to the convention to access your instance variables via the getter and setter methods,you do not need to worry about working with autoreleased objects,because the setter methods would automatically retain the object. That’s a best practice which really helps keep your code clear.

BesIDes the automatic autorelease pools,you can also create your own pool by creating a new instance of the object NSautoreleasePool. The creation of your own autoreleass pool is mandatory in the following cases:

You are writing a command line tool You are spawning a new thread

There is another use for custom pools. When you are working with a lot of autorelease objects insIDe a loop statement,this will lead to all those objects being stored in the pool until the event loop finishes. It is much more efficIEnt to release the memory at the end of each loop cycle. In that case we would create a autorelease pool insIDe the loop:

for (i = 0; i < 1000; i++) { NSautoReleasePool *myPool [NSautoReleasePool alloc]*someVariable [Nsstring stringWithString:@"Some text"// do some more stuff with lots of autorelease objects  [myPool release}

In the above example,all the autorelease objects which have been created insIDe the loop,will be disposed of as soon as we release the pool object. This is a very powerful concept to keep your memory consumption low.

Note on collections

Collection objects retain the objects which are stored in the collection. You can release them once you added them to the collection,but keep in mind,that once the collection object is released,it will also release all objects stored in that collection.

This entry was posted in  Cocoa and tagged  Cocoa,  Coding by  Peter. Bookmark the  permalink.
2 THOUGHTS ON “COCOA MEMORY MANAGEMENT 101

Oliver on    saID:

Hi,

also useful (and short) reference:
http://www.gnustep.org/resources/documentation/Developer/Base/ProgrammingManual/manual_3.html#SEC43

Regards from Hamburg 

 ,

Oliver

 

Pingback: iPhone Memory Management Reading List | markjnet

总结

以上是内存溢出为你收集整理的Cocoa memory management 101全部内容,希望文章能够帮你解决Cocoa memory management 101所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1053723.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)