views:

96

answers:

5

Hi,

I have two possibilities:

1) Store an object in a variable and use that variable in my code. But this uses memory to store the object right?

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
BOOL bool1 = [userDefaults boolForKey: key1];
BOOL bool2 = [userDefaults boolForKey: key2];

2) Don't store it in a variable and create it from scratch when I needed.

BOOL bool1 = [[NSUserDefaults standardUserDefaults] boolForKey: key1];
BOOL bool2 = [[NSUserDefaults standardUserDefaults] boolForKey: key2];

What would be recommended in this case? If there's a difference between objects then how would I know which one to use?

+1  A: 

You haven't been very specific, as to what kind of data. But irregardless, the things variables point at are what consume memory, not the variables themselves.

As for the rest of your question, if you leave a comment explaining your application centered around this question, as in what part your system is doing at this moment, I'll edit this to reflect that answer.

EDIT: Someone else answered and was accepted before I could update my answer. See the accepted answer.

jer
Thanks for the quick reply. I edited it. It's hard to explain what I'm trying to achieve. But I added an example of NSUserDefaults.
Shane King
A: 

If the operation executed is a very performance heavy method, it might be a performance loss to redo it every time, when you simply can save the information.

I feel you have to choices

  1. If you have a problem with allocated memory, redo it.
  2. If memory problem isn't an important factor and the executed operation isn't that heave that you have a performance loss, redo it.

I believe that doing iPhone-apps, the memory is not that super critical (might be now with multitask however), it's more critical to have the workload in mind, i.e. try not to redo same work. But as said, it all depends on the situation.

Fredrik_jakob
Thanks a lot...
Shane King
+3  A: 

As far as the particular example you showed in your question is concerned, there's no practical difference between those two ways of getting the NSUserDefaults. Unless you are dealing with large data objects (like UIImages) you should be concentrating on the clarity and readability of your code. If it turns out that you have a memory issue during your testing, come back and find ways to use less memory later.

kubi
Thank you for the info.
Shane King
A: 

The wording of your question suggests you may not fully understand C pointers. You're not "storing an object in a variable." The userDefaults variable in your first example is just a C pointer. The +standardUserDefaults method returns a pointer to the global user defaults object, which you assigned to userDefaults. In your second example, you retrieved a pointer to the object twice, once for each line.

The only real difference between the two is that in the second example, you're doing a redundant message-send to retrieve the object reference again. Your examples aren't performance-critical, but there are cases where you might want to cache a reference to an object to avoid sending redundant messages, such as in a loop. It all depends on the performance of your program.

If you're not comfortable with C pointers, definitely read up on them. It should clear up some things for you.

Preston
+1  A: 

You are not using any more of your program's memory in the first example. The NSUserDefaults object will be stored on the heap either way, and the storage for the variable itself will either be a) a register, or b) on the stack. Registers don't take up any of your app's real memory, and the memory for the stack is already reserved.

Also, pointers take up a very small amount of memory — 4 bytes on 32-bit and 8 bytes on 64-bit platforms. A quarter of a million of them would take less than 1 MB. Individual scalars like this are the very last place you should be looking for memory savings.

Chuck