Understanding Automatic Reference Counting - ARC

Posted by on in Blogs

Automatic Reference Counting is the memory management system used by Strings & Interfaces on all platforms, and all objects on Linux and Mobile. Unlike Garbage Collection, ARC is purely deterministic and keeps the developer in control. Join Delphi Product Manager Marco Cantu for this deep dive into Automatic Reference Counting.


 
More details to come.


About
Gold User, Rank: 7, Points: 457
Delphi and RAD Studio Product Manager at Embarcadero.

Comments

  • Alfred N40196
    Alfred N40196 Monday, 12 June 2017

    After having watched portions of the movie, I have one question and one comment regarding the [unsafe] attribute:

    In the movie, you show the following example:
    procedure TForm25.Button2Click(Sender: TObject);
    var

    [unsafe] myObjU: TMyClass;
    begin

    myObjU := TMyClass.Create;
    try
    myObjU.someData := 10;
    Show(myObjU.someData.ToString);
    finally
    myObjU.Free;
    end;
    end;

    Doesn’t that introduce a memory leak?

    The myObjU.Free (replaced by myObjU := nil by the compiler), will do nothing, as the reference is [unsafe].

    In a later example, myObjU.Free is replaced by myObjU.DisposeOf, which leads to my comment:

    I’ve noticed in several presentations, that you (and other at Embarcadero) suggests using [unsafe] is “exactly as traditional references”.

    I don’t argue with that. But, what I think is kind of misleading is that you never mention, that the instance the reference points to, isn’t handled as a “traditional instance”, but as any other ARC-object.

    myObjU.DisposeOf frees the instance because of ARC (DisposeOf calls __ObjAddRef and later __ObjRelease).

    Extending your example a bit, highlights another big problem:
    procedure Show(MyObj: TMyClass);
    begin

    end;

    myObjU := TMyClass.Create;
    try
    myObjU.someData := 10;
    Show(myObjU);
    myObjU.someData := 20; // n the ARC-platform the last years and there are two things, that I think would make (ARC-programming) life much easier:
    1. ARC on Win32 and Win64 (profiling on the other platforms are very hard)
    2. Better support for finding circular references (closures, for example, can easily cause unwanted hard references)

    /Alfred

  • Alfred N40196
    Alfred N40196 Monday, 12 June 2017

    Some lines were removed:

    myObjU.someData := 20; // ?Show” is called, the Ref.Count is increased (to 1) and then later decreased again (to 0), causing the object to be destroyed and myObjU to point at garbage data.

    As earlier said, I don’t argue that [unsafe] can be thought on as “traditional references”. But, I think the examples doesn’t represent “good usage practices” nor does the instances behave in a way, that I would expect from a “traditional referenced” instance.

    In the performance example, you show that [weak] doesn’t have big impact on performance.

    You mention that there are times where [weak] might not be preferable, but you do not give any example.

    I can give one, where [unsafe] makes a perfect choice over [weak]:

    [weak] references are stored in lists in each referred instance. If you create many objects, these lists can easily affect performance (and memory usage). In such cases, replacing [weak] with [unsafe], can greatly improve performance. (Been there, done that…)

    Finally, as I’m already going on , I’d like to say that ARC indeed isn’t that bad. It is indeed a great way forward, but introduces a new way thinking, when working with objects (instead of keeping track of freeing memory, one have to watch for object references).

    I’ve spent some quite extensive time on the ARC-platform the last years and there are two things, that I think would make (ARC-programming) life much easier:
    1. ARC on Win32 and Win64 (profiling on the other platforms are very hard)
    2. Better support for finding circular references (closures, for example, can easily cause unwanted hard references)

    /Alfred

  • Please login first in order for you to submit comments
  • Page :
  • 1

Check out more tips and tricks in this development video: