user1157838
user1157838

Reputation: 131

difference between ARC and MRC

I am confused with Apple material.

In 3 ways we manage the memory, they are :

  1. automatic referance counting.
  2. manual reference counting.
  3. garbage colletion.

My doubt is what is the difference between automatic reference counting and manual referance counting.

Can someone explain me ?

Upvotes: 10

Views: 17196

Answers (4)

yoAlex5
yoAlex5

Reputation: 34235

MRC vs ARC

ARC inserts retain, release, autorelease instead of developer in compile time. Now you don't worry about manual memory management

[Under the hood]

Upvotes: 0

gabrielrf97
gabrielrf97

Reputation: 317

In MRC, you were responsible for keeping track and making sure that all references of objects were incremented, decremented and deallocated properly. In Obj-C you have basically a set of rules to help you not get any memory leaks or dangling pointers, and it was a considerable effort to make sure that everything was working great, and that could've been automated by something, like some other languages used to.

That's is when ARC get's into the game.

ARC came as an incisive alternative to how things worked with MRC. With ARC, instances are deallocated when there's no strong reference to them, and every instance keeps track of the number of strong and weak/unowned references kept to itself. Although it might look like a similar behaviour, the amount of effort used in both cases are hugely different, in MRC you had to keep track of everything, whereas in ARC the only thing you should do is avoid retain cycles.

Some differences between ARC and Garbage Collector are:

  • Garbage collector is part of the runtime structure. In ARC, the Swift compiler does code-cleaning and reference tracking insertion in your app bundle.
  • Garbage collector does not reclaims the memory as soon as the instance loses its references, ARC does.
  • If some objects cycle (or graph) has references to themselves, but are not accessible through the root node, GC can clean the hole graph, whereas in ARC they would never get deallocated because they hold strong references to it other(retain cycle).

If you want to check for more information, I've found this article to be very helpful:https://swift007blog.wordpress.com/2017/01/14/what-is-arc-in-ios/

Upvotes: 0

Ankit Srivastava
Ankit Srivastava

Reputation: 12405

In ARC the OS looks after the memory management, so you don't have to worry about releasing the objects. It's pretty neat for beginners. Whereas in Manual counting you will have to keep track of releasing the memory and if you don't do it right you will end up crashing your app. ARC and MRC are available in ios where as garbage collection is limited to MAC-OSX hope this helps. Inder has given a good example.

Upvotes: 4

Inder Kumar Rathore
Inder Kumar Rathore

Reputation: 39988

In ARC you don't have to release/autorelease the memory allocated by you where as in case of manual you have to take care of this. e.g. manual case

-(void)someMethod
{ 
    NSMutableArray *arr = [[NSMutableArray alloc] init];
    //use array
    [arr release]; //when array is in no use
}

ARC case

-(void)someMethod
{
    NSMutableArray *arr = [[NSMutableArray alloc] init];
    //use array
}

Upvotes: 16

Related Questions