ios - NSString, NSMutableString memory -


here have 2 string objects

    nsstring *name1 = @"julia";     nsstring *name2 = @"julia";      nslog(@"name1's memory: %p , name2's memory: %p", name1, name2); 

these 2 objects, because content same, automatically points same memory.

this log

    name1's memory: 0x100002320 , name2's memory: 0x100002320 

this same behavior exists in java programming language.

next, same test nsmutablestring.

    nsstring *name1 = [nsmutablestring stringwithstring:@"julia"];     nsstring *name2 = [nsmutablestring stringwithstring:@"julia"];      nslog(@"name1's memory: %p , name2's memory: %p", name1, name2); 

this log

    name1's memory: 0x10010a4c0 , name2's memory: 0x10010a550 

the results different. each of points own memory.

how explain this? nsstring more efficient?

in contrived example, same performance from:

nsstring *name1 = [nsmutablestring stringwithstring:@"julia"]; nsstring *name2 = name1; 

but yes immutable strings attempt better performance pointing same memory addresses when possible, can't happen mutable strings, since modifying 1 should not change other arbitrary string pointing same memory efficiency. cases mutable string going more efficient, sure in contrived example immutable string more efficient in terms of memory space.


Comments