Static variables and memory consumption

Logicsaurus Rex

With iOS, we always have to be concerned about memory consumption.

I have a class that may have hundreds of instances, and I haven't been able to locate a discussion that indicates whether declaring a static dictionary inside an instance method means that all instances of that class will share the same copy of that dictionary, or each instance will have its own copy, which of course would demolish memory.

BTW, would the answer be any different if this were a class method instead of an instance method?

-(BOOL)doohickeyThing
{
    static NSDictionary *someDictionary = [NSDictionary dictionaryWithObjectsAndKeys...

    // more code here
}

Thanks.

rmaddy

By definition, there is only one copy of a static variable. No matter how many instances of your class you have, there will only be one copy of the someDictionary. It will get initialized once and every time the method is used, regardless of class instance, the same exact dictionary instance will be used. In other words, it is shared.

This is true whether it is an instance method or a class method.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related