开发者

releasing variables to save memory

I have a beginner's question about releasing variables and not wasting memory...

I don't quite understand when to release variables. I understand that I should always do this if I have assigned them in my head开发者_JAVA百科er file (in my @interface section and my @property commands). I release them in my -(void)dealloc function.

However, what am I supposed to do with variables that I happen to use in some of my methods, e.g.:

for (int temp = 0; temp < 3; temp++) {
    // do something...
}

[temp release];

This is obviously wrong (at least xCode tells me so), as 'temp' is undeclared. But why? I've declared it as an int and temp thus takes up space in my memory, I'm sure. How do I tell the program to free up the space temp has taken after I don't need it anymore? I'm sure this is obvious, but I simply don't get it.

I'd be very happy for any suggestions for a beginner of how not to be a memory pig and to have 'memory leaking' everywhere in my apps...


You declared it as an int in the scope of the loop. Once the loop is done, it goes out of scope.

Also, you can not release an int, which is a primitive type. You can only release a subclass of NSObject. A good rule of thumb is that you eventually have to release anything that you called alloc or retain on.

Edit: For your edification, memory management only applies to objects allocated from the heap. That would be NSObjects obtained via "alloc" or must C-level memory allocated with something like "malloc()". Declaring a variable like "int x" is called an "auto" variable in that is is created on the stack and will AUTOmatically disappear then that block ends (the end of a "block" being the end of the function or perhaps the end of a {} pair or even the end of a for/if/while block.

Since Objective-C is basically just a special version of C (with messages), it does not create permanent objects unless you explicitly tell it to. This is different form languages like Python or Javascript.


You only need to release objects, and temp is an int, not an object.

As far as when to release objects, Apple's docs explain that better than I can: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html


You do only release objects, and not primitive types. For example you create an array with

NSArray *myArray = [[NSArray alloc] init];

This initialization allocated memory which you have to free after your done using your array, it's your responsibility, else there will be memory leaks. You can do that in the dealloc section of a controller, or at the end of a method, or even after you've enumerated through the array and no longer need it.

If you create instances of objects with other methods than alloc, copy or new(rarely used) you have to release them. If you call retain yourself on an object you have to release it as well.

Please refer to apples memory management rules, which have been posted earlier.


There are two ways to store information in RAM in C and c like things, primitives which are allocated automatically by the compiler, and memory chunks allocated by you in your program.

Variables allocated for you by the compiler are called "automatics" and can be marked by the essentially unused (because it is the default) "auto" keyword. Automatics are bound to the scope in which they are declared and go away when it ends. Regular variables, like "int x" are automatic.

Notably, pointers are typically automatic. However, the things they point to are not. Those would be the thing you asked to be allocated.

In objective-c, things are allocated with the alloc message. However, sometimes a class will call this for you, so you might not see it. To help make it clear what you should do, there is a tradition: If you get an object where you alloc'ed it, got it from a class method with the word "copy" in the method name, or sent it a "retain" message, then you own a share of it, and it won't go away until you send it a release message.

If you didn't get the object through one of those means, you must not release it, because you don't have a share in it.

So, in summary: regular variables (int, short, char, double, float, long) are automatic, no need to allocate it. Pointers are also automatic, however, the things they are pointing to are not. In obj-c, you own a share if you alloc'ed it, copy'ed it, or sent it a retain message.


You can't release an integer...

Release works only with instance of Objective-C classes.

Variables such as integers are placed on the stack, and they does not persist after a function/method call, unless allocated explicitely.


You only release objects. If you use the alloc, init procedure to create an object you must release it. If you retain an object you must release it. If you use a method that has the word "create" in it, you must release it. Anything else, the system will handle for you.


Primitives do not need to be released, only objects.

Biggest thing to keep in mind is that whenever you "alloc" or "retain" an object, put a corresponding "release" or "autorelease".


you see an error on the line [temp release]; because temp is not in scope. temp will not be seen outside of the for loop you created.

you do not need to clean up the memory of temp, since you have not claimed ownership of it(see Memory Management Rules): http://developer.apple.com/library/ios/#documentation/general/conceptual/DevPedia-CocoaCore/MemoryManagement.html

examples of objects where you do not need to release/manage memory:

  • NSArray *myArray = [NSArray array];
  • NSNumber *myNumber = [NSNumber numberWithInt:5];
  • NSString *myString = @"Hello World";
  • NSInteger i = 5;
  • int x = 2;

examples of objects where you do need to release/manage memory:

  • NSArray *myArray = [[NSArray alloc] initWithObjects:@"Hello", @"World", nil];
  • NSNumber *myNumber = [[NSNumber alloc] initWithInt:5];
  • NSString *myString = [[NSString alloc] initWithString:@"Hello World"];
  • -

typically, when your completely done using an object you own, you clean up its memory

see apple's docs for explanations on properties: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocProperties.html#//apple_ref/doc/uid/TP30001163-CH17-SW1

  • setting a property with assign or readonly: you should not worry about releasing its memory, as you don't own it
  • property with retain or copy: you claim ownership of the object and need to release it at some point

this answer won't answer/solve all memory management questions/concerns, but it may shove you in the right direction

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜