Paul Subhajit
Paul Subhajit

Reputation: 152

Memory allocation for NSString and NSMutableString

I have a doubt when i am creating an object of NSMutableString it's pointing to two separate memory location but when i am using NSString it's pointing to same location. Can any one please explain the logic behind this.

 NSMutableString *myString1 = [NSMutableString new];
 NSMutableString *myString2 = [NSMutableString new];
 NSLog(@"%p",myString1);0x00007fe572416da0
 NSLog(@"%p",myString2);0x00007fe5724114a0

NSString *myString3 = [NSString new];
NSString *myString4 = [NSString new];
NSLog(@"%p",myString3);0x10622e470
NSLog(@"%p",myString4);0x10622e470

Upvotes: 1

Views: 550

Answers (5)

Jobin Jose
Jobin Jose

Reputation: 288

I will explain it based on the memory used. Below I given details about retain count of these variables under non-ARC scenario

NSMutableString *myString1 = [NSMutableString new];

NSLog(@"String Retain Count: %lu", (unsigned long)[myString1 retainCount]); //String Retain Count: 1

NSString *myString3 = [NSString new];

NSLog(@"String Retain Count: %lu", (unsigned long)[myString3 retainCount]); //String Retain Count: 18446744073709551615

So in the second case it is considering as a constant string. It consider your string as a NSConstantString.

Upvotes: 1

avismara
avismara

Reputation: 5149

String allocation, like all object allocation, proves costly in both time and memory. The runtime performs some trickery while instantiating string literals to increase performance and decrease memory overhead. To cut down the number of String objects created in the runtime, the NSString class keeps a pool of strings. Each time your code create a string literal, the runtime checks the string literal pool first. If the string already exists in the pool, a reference to the pooled instance returns. If the string does not exist in the pool, a new String object instantiates, then is placed in the pool. Objective-C can make this optimization since strings are immutable and can be shared without fear of data corruption.

In your second code block,

NSString *myString1 = [NSString new];
NSString *myString2 = [NSString new];

both strings are initialized to empty strings. Hence they return the same reference of those strings from the string pool.

NSMutableString doesn't work that way. Runtime doesn't create a pool because these strings are mutable and there is always a fear of data corruption if the strings are mutable. And hence you see different references being printed out.

The point to take out of this example is that always use NSString if you don't want the string to change i.e., immutable for performance reasons. Resort to NSMutableString only if you want a mutable version of the string.

Upvotes: 2

Prashant Tukadiya
Prashant Tukadiya

Reputation: 16416

NSString *firstUserName = @"nick";

NSString *secondUserName = @"nick";


if (firstUserName  == secondUserName)
{
   NSLog(@"areEqual");
}
else
{
  NSLog(@"areNotEqual");
}

the output of it is areEqual surprising ?

Here’s why:

Comparing pointer values equates to checking if they point to the same object. Pointers will have the same value if and only if they actually point to the exact same object (whereas pointers to different objects will not have the same value, even if the objects they point to have the same value).

Upvotes: 0

gnasher729
gnasher729

Reputation: 52530

Objective-C will always feel free to use the exact same object when you try to create two immutable objects with the same contents. This is just one example. This extends for example to [NSNull null], @YES, @NO, any short immutable strings, empty arrays, identical NSNumbers etc.

Upvotes: 0

Avi
Avi

Reputation: 7552

  1. Because NSString is immutable, the class implementation tries to return the same instance whenever possible.
  2. Your second example is wrong because you are creating NSMutableStrings, and the log statements will print different values.

Upvotes: 0

Related Questions