Ilya Gazman
Ilya Gazman

Reputation: 32281

"Memory is managed automatically" - how?

I know that the biggest difference between GC and ARC is that GC is run time process while ARC is operates in compile time. So when working with ARC the developer need to take care of memory in some scenarios.

How ever according to this, there is no place left for developer interaction in SWFT memory management architecture.

So how they do this? Do they have a run time process for cleaning up the memory, or there some thing else?

Upvotes: 4

Views: 1628

Answers (3)

J D
J D

Reputation: 48707

I know that the biggest difference between GC and ARC is that GC

Note that ARC is a form of GC.

is run time process while ARC is operates in compile time.

Both tracing GC and ARC do things at both compile time and at run time. ARC injects code that increments and decrements reference counts and, when the count falls to zero, collects the object and decrements all of the references it was pointing to recursively (potentially causing an unbounded amount of work to be done at run time as an arbitrarily large object graph is collected).

So when working with ARC the developer need to take care of memory in some scenarios.

Yes. You must always be careful to avoid cycles because they will never be collected.

Upvotes: 0

Mike
Mike

Reputation: 9835

How ARC Works

Every time you create a new instance of a class, ARC allocates a chunk of memory to store information about that instance. This memory holds information about the type of the instance, together with the values of any stored properties associated with that instance.

Additionally, when an instance is no longer needed, ARC frees up the memory used by that instance so that the memory can be used for other purposes instead. This ensures that class instances do not take up space in memory when they are no longer needed.

However, if ARC were to deallocate an instance that was still in use, it would no longer be possible to access that instance’s properties, or call that instance’s methods. Indeed, if you tried to access the instance, your app would most likely crash.

To make sure that instances don’t disappear while they are still needed, ARC tracks how many properties, constants, and variables are currently referring to each class instance. ARC will not deallocate an instance as long as at least one active reference to that instance still exists.

To make this possible, whenever you assign a class instance to a property, constant, or variable, that property, constant, or variable makes a strong reference to the instance. The reference is called a “strong“ reference because it keeps a firm hold on that instance, and does not allow it to be deallocated for as long as that strong reference remains.

https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/AutomaticReferenceCounting.html

Upvotes: -1

Nikolai Ruhe
Nikolai Ruhe

Reputation: 81878

Swift uses ARC in a similar way as Objective-C does. ARC has been discussed extensively.

In short:

  1. There is no garbage collector.
  2. Objects live as long as (strong) references exist.
  3. Strong references can't be cyclic, otherwise you leak memory. Use weak references to break cycles.

Upvotes: 5

Related Questions