difference between ARC and MRC
Asked Answered
T

4

10

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 ?

Tetramethyldiarsine answered 2/2, 2012 at 9:50 Comment(1)
possible duplicate of How does the new automatic reference counting mechanism work?Piscina
R
16

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
}
Rainer answered 2/2, 2012 at 9:56 Comment(6)
Ok.Thank U.Is there any difference among them?Tetramethyldiarsine
@user1157838: yes, notice the ARC case there is no [arr release];. Also note that on iOS garbage collection is not available.Piapiacenza
Is there any settings for use the ARC?Tetramethyldiarsine
Yes go to project settings->>>Objective C Automatic Reference Counting you can turn YES or NO thereRainer
But ankit said this is available from sdk 4.2 onwards only.Tetramethyldiarsine
Yes it is available in sdk 4.2Rainer
T
4

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.

Turbot answered 2/2, 2012 at 10:10 Comment(3)
In any version ARC support or not?Tetramethyldiarsine
U mean from sdk 4.2 onwards there is no need to release the objects.We can create any number of objects .Am i correct?Tetramethyldiarsine
yes, but you will have to enable ARC in your project when you create a new project otherwise it will work as MRCTurbot
H
0

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/

Heshvan answered 7/9, 2019 at 21:11 Comment(0)
W
0

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]

Weeden answered 17/5, 2021 at 20:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.