Here is my code:
NSString *xyz=[NSString stringWithFormat:@"%i %@",10,@"Sagar"];
Now I am taking other string, as follows.
NSString *x2=[xyz copy];
I don’t know exactly what will happen here? Is it something like, x2 has the ref of xyz’s ref?
NSString *x3=[xyz retain];
What will happen here, x3 has a new memory having copied string or [xyz copy] does that?
Now, how to remove all these three strings from memory?
This will create autoreleased instance of NSString – it will be released when the autorelease pool is drained (typically on the next run loop).
In theory
-copymessage will create a new instance of the object with retain count 1 (that is you must release it somewhere), but as NSString object is immutable then [xyz copy] will be optimized to [xyz retain] and thus it will point to the same instance.x3 will point to the same instance as xyz (and x2), and its retain count will be incremented – you must release your object somewhere.
Make sure that you pair all retain (copy) messages with release and memory will be freed.
Read Objective-c memory management guide for more details.