News for hackers. Stuff that matters.
Re:Mac Apps That Use Garbage Collection Must Move to ARC by philippnagel on Saturday February 21, 2015 @11:22AM (#9085892)
Any other languages that use Automatic Reference Counting? Is ARC strictly better than GC?

(Sorry for the question, I am not an iOS, Mac dev.)

Re:Mac Apps That Use Garbage Collection Must Move to ARC by xenadu02 on Saturday February 21, 2015 @04:24PM (#9087019)
Yes, ARC is better.

Objective-C is a superset of C. If you think about what it means to have a garbage-collected version of C for just a few minutes it should become immediately clear why abandoning GC for Objective-C is necessary and appropriate. The GC can't always reliably detect a reference (pointer) so it has to be conservative. It also can't compact the heap, which means you don't get the nice "allocation is just advancing a pointer" behavior that makes allocations nearly instantaneous in managed languages.

ARC is essentially just the old rules for manual reference counting (MRC), but the compiler analyzes your code and inserts the retain/release/autorelease calls automatically. This gives you similar deterministic behavior as MRC but while writing the code it feels like a garbage collected language because you mostly ignore management of memory and everything "just works".

The downside to ARC is the inability to detect retain cycles and a slight performance hit compared to MRC in some scenarios. The performance is actually pretty good... What ARC loses compared to hand-coding it gains by using special runtime functions (not slower ObjC message sends) that elide unnecessary retain/release calls in many situations.

objc_retainAutoreleaseReturnValue is inserted in a caller and callee (typically a property getter). It examines the return address on the stack to detect if the caller is going to invoke objc_retainAutoreleaseReturnValue on the returned value; if so it just does a retain and sets a flag the caller uses to skip doing retain+autorelease, leaving the caller's release only. Instead of the object staying alive until the next run loop iteration and getting autoreleased it can safely be released immediately, plus at least one extra pair of function calls can be eliminated.

Re:Mac Apps That Use Garbage Collection Must Move to ARC by asveikau on Sunday February 22, 2015 @02:31AM (#9088614)
In C++, shared_ptr and similar "smart pointer" concepts are very similar to ARC. The key difference is it's a library feature, built atop destructors and copy-constructors, and not a language change like ARC.

Microsoft's C++/CX introduced in 2011 is also very similar; the hat syntax[1] is to COM's AddRef/Release as ARC is to -retain/-release. In both schemes, what used to be manually refcounted objects are now managed by the compiler. I don't recommend C++/CX though, it is confusing to C++ devs. Smart pointers are a better recognized idiom.

[1] Unrelated to the hat syntax from MS's previous attempts, Managed C++ and C++/CLI, which are GC and not refcounting as in C++/CX.

Re:Mac Apps That Use Garbage Collection Must Move to ARC by jakobegger on Saturday February 21, 2015 @11:46AM (#9085974)
The most important difference on OS X is that ARC is compatible with retain/release, but GC requires explicit support by every framework linked in your app. Some frameworks never added GC compatibility.

In the general case, it is estimated that GC uses on average twice as much memory (high water mark), but direct comparisons are of course very application specific. GC also has the dreaded collection pauses which can make performance unreliable. (Eg stuttering animations)

Biggest drawback of ARC is that it doesn't detect cycles, so the developer needs to have a global understanding of an app to avoid hard to debug leaks.

Re:Mac Apps That Use Garbage Collection Must Move to ARC by mikaraento on Saturday February 21, 2015 @11:40AM (#9085953)
Perl, Visual Basic 6 and to some extent Python are examples of languages using the moral equivalent of ARC.
Re:Mac Apps That Use Garbage Collection Must Move to ARC by jimrandomh on Saturday February 21, 2015 @11:46AM (#9085976)
Python has been garbage collected since 2.0.

(Edit: Perl is garbage collected since version 6, and Visual Basic is garbage collected since version 7. I am not aware of any language besides Objective C moving in the opposite direction.)

Re:Mac Apps That Use Garbage Collection Must Move to ARC by sitharus on Saturday February 21, 2015 @01:14PM (#9086280)
CPython is actually hybrid reference counted and garbage collected. This is really an implementation detail for historical reasons, no other Python implementation I'm aware of uses reference counting.

There is no Visual Basic 7. Visual Basic .NET is garbage collected as it runs on the CLR, but is quite a different language to VB6.

Re:Mac Apps That Use Garbage Collection Must Move to ARC by Roboprog on Saturday February 21, 2015 @04:27PM (#9087035)
One could argue that Perl 6 isn't what most people think of as "Perl", and probably should have been called something else.

Similarly, VB vs VB.NET.

Re:Mac Apps That Use Garbage Collection Must Move to ARC by rsynnott on Saturday February 21, 2015 @12:08PM (#9086065)
C++ and Rust both provide reference counting pointer-like-things (shared_ptr and Rc respectively). C++ has been around for quite a while, of course, but Rust is pretty new, and deliberately eschewed garbage collection.
Re:Mac Apps That Use Garbage Collection Must Move to ARC by pjmlp on Saturday February 21, 2015 @12:49PM (#9086192)
C++ RC requires good behavior from whole developers in the team and third party libraries.

It is hard to get right with existing code bases.

Additionally C++11 got a GC API, but requires explicit programmer control.

http://www.stroustrup.com/C++11FAQ.html#gc-abi

C++'s memory model is tainted by C's compatibility, as such it is quite limited in the GC algorithms that can be implemented.

Re:Mac Apps That Use Garbage Collection Must Move to ARC by coliveira on Saturday February 21, 2015 @08:33PM (#9087808)
In C++, reference counting is in a per object basis. There is no need of cooperation at all from other libraries or code bases.
Re:Mac Apps That Use Garbage Collection Must Move to ARC by pjmlp on Sunday February 22, 2015 @03:28AM (#9088727)
Really?!?

How can you guarantee that the component library that your company just bought, available only in binary form isn't doing something like this?

    void cool_stuff_with_widget(const std::shared_ptr<UI> ptr)
    {
       UI* evilPtr = const_cast<UI*>(ptr->get());
       // now store evilPtr somewhere else and try to use it any time the library feels like it
    }

No one is going through disassembly to check such behaviors.

C++ RC is a very welcoming addition to the language, but it only works if everyone plays by the rules, 100% of the time.

Re:Mac Apps That Use Garbage Collection Must Move to ARC by tonfa on Sunday February 22, 2015 @06:49AM (#9089082)
It's not really an issue, if the library is buggy and leaks or keeps reference to things it's not supposed to, it will be the same whether it's RC or just plain memory management.

It's just better documented and easier to spot the bugs if you're doing RC.

Re:Mac Apps That Use Garbage Collection Must Move to ARC by pjmlp on Sunday February 22, 2015 @09:27AM (#9089517)
It not about bugs, rather C++'s lack of support to enforce that RC is not misused and programmers cannot grab RC internals and corrupt the data structures.
Re:Mac Apps That Use Garbage Collection Must Move to ARC by coliveira on Sunday February 22, 2015 @09:56PM (#9092307)
C++ is a language that allows any kind of memory manipulation, if you really want (or need) to do it. So your example doesn't prove anything about the adequacy of shared_ptr in particular.
Re:Mac Apps That Use Garbage Collection Must Move to ARC by pjmlp on Monday February 23, 2015 @02:27AM (#9093084)
My point was that if a language does RC via library types that exposure the pointer that they own, there is no safety guarantee about memory safety.

It is impossible to prove that all memory access to a given address are done via the RC wrapper object, specially in the presence of third party libraries in binary form.

Sadly due to its C compatibility, there is no way around this in C++.

This is mostly a problem in large projects with various skill levels across team members, having high attrition levels.

Re:Mac Apps That Use Garbage Collection Must Move to ARC by coliveira on Monday February 23, 2015 @10:10PM (#9098485)
As I mentioned above, the goal of C++ is not to provide memory safety guarantees. What it provides is a mechanism for RC implementation. You're free to use or misuse it if you want. It will work only for those objects that you think should be managed with RC, nothing more or less.
Re:Mac Apps That Use Garbage Collection Must Move to ARC by DrJokepu on Saturday February 21, 2015 @11:35AM (#9085935)
In my opinion, if you understand how memory management works, ARC is definitely superior to GC. There are things it's not very good at, typically things that usual reference counting is not very good at either, such as handling cyclical data structures.
Re:Mac Apps That Use Garbage Collection Must Move to ARC by danbruc on Saturday February 21, 2015 @12:00PM (#9086031)
Resource management is a necessary evil but usually not a primary concern of most applications. Therefore in an ideal world it should be abstracted away and you should not have to care about it ever. In this respect I consider a full blown garbage collector as closer to the ideal than automatic reference counting. But automatic reference counting is of course also a kind of automatic garbage collection mechanism, just with a bit of a different focus.

All solutions are not optimal, but unless you have really many allocations and dealloctions going on, have tight resource constraints or hard timing constraints, current garbage collectors are really good enough. Not that you can not shoot your feet by accidentally keeping large object graphs alive, but that is easy to detect, though sometimes hard to fix.

Some developers seem to have some kind of resentments against garbage collectors because real developers of course manage their resources on their own, but many of their arguments are not well founded. Garbage collectors nondeterministically pausing program execution to free unused resources is a common point, but they usually don't realize that automatic reference counting has the same problem - you never know when a reference count drops to zero and triggers the deallocation of the object possibly with a large object graph attached.

Re:Mac Apps That Use Garbage Collection Must Move to ARC by jahewson on Saturday February 21, 2015 @12:45PM (#9086180)
Not true. A reference counted program is deterministic, it is known in advance for a given set of inputs exactly when deallocation will occur.

Also, its not really fair to sweep "hard timing constraints" under the rug as a minority use case when these are essential for smooth modern GUIs.

Sure, GC is pretty much ideal in terms of absolving developers of resource management responsibilities, but it comes with some significant downsides which can easily effect the final product and should be understated. Rust is a great example of a new language which avoids GC for this reason.

Re:Mac Apps That Use Garbage Collection Must Move to ARC by danbruc on Saturday February 21, 2015 @01:03PM (#9086239)
That is a pretty theoretical scenario, at least I usually have no idea in advance what the user input will be. And garbage collectors pausing program execution is not a fundamental problem, there are garbage collectors operating entirely in parallel with normal program execution. Even further, garbage collection itself seem not to be the main source of those pauses, but heap compactification, which you also lose if you use automatic reference counting (or have to do manually).
Re:Mac Apps That Use Garbage Collection Must Move to ARC by jahewson on Saturday February 21, 2015 @01:49PM (#9086415)
I didn't say you had to know the inputs in advance. What's known in advance is when deallocation will occur, i.e. its deterministic. You know exactly when deallocation will occur.

Heap compaction is another part of GC which results in long non-deterministic pauses. But the real problem is having fragmentation in the first place, this is something which manual allocation strategies can go a long way to mitigate.

Re:Mac Apps That Use Garbage Collection Must Move to ARC by danbruc on Saturday February 21, 2015 @02:13PM (#9086490)
How can you know when a deallocation will occur? Did the user add this product to a single invoice and the product object will get deallocated when closing this invoice form or is there a second invoice still referencing the product? At best you can know when a dealloction might possibly occur, but this is whenever a reference changes or goes away and usually not pretty helpful.

For any sufficiently complex real world application you will not in general know when a deallocation occurs and that is one of the reasons you decided to use automatic reference counting in the first place. Some short lived objects within a function are non-issues to begin with and are easily handled manually if you want to, but the interesting bits and pieces are objects with unpredictable life time and usage pattern because they are heavily influenced by inputs.

Re:Mac Apps That Use Garbage Collection Must Move to ARC by jahewson on Saturday February 21, 2015 @02:55PM (#9086640)
When will a deallocation occur? When release is called and the referece count is zero. This is deterministic, unlike GC which will perform the deallocation when it feels like it.

I think you've confused determinism with regards to a program and the inputs to that program. Any retain/release program is deterministic because it always behaves in the same manner. Given any possible input I can tell you exactly when and on which lines of code the deallocations will occur. You can't know that with GC, especially when other libraries and threads enter the equation.

Which deallocations occur and in which order depend on the input to the program, but we still know in advance all possible lines of code where specific deallocations can occur and we have complete control over that. With GC we wouldn't know that and we couldn't control it.

Knowing when deallocations can occur and having control over that is the reason to choose ARC over GC in the first place. Not the other way around. With respect to manual retain/release, ARC doesn't take anything away: I can still retain an extra count of a large object to prevent its untimely release and share that object via ARC at the same time.

Re:Mac Apps That Use Garbage Collection Must Move to ARC by Skinney on Sunday February 22, 2015 @05:39AM (#9088978)
Two things. First, most GC doesn't actually deallocate anything, but that's a minor nitpick. The second, more important note, is that you are now describing a system where everything is garbage collected. This needn't be the case. In a language like Go, you have control over what get's stack allocated and what is placed in a GC. When that choice can be made, then you really can tell when given objects are deallocated. Of course, since the stack is a fixed size construct, I guess you don't really deallocate these things either...

It's also worth noting that GCs rarely does anything because it feels like it, but because you are allocating past a certain limit. Knowing the size of objects, and given that the allocator can provide you with the current size of allocated objects, you should be able to determine if a GC is likely to incur in the next lines of code. This determination becomes harder when threads enter the picture, unless you're using a GC scheme like Erlang, which have one GC per process (lightweight thread). Some GCs also allow you to pause and resume them, for when you really want to avoid GC cycles, this can of course lead your program to crash if you allocate past your limit.

As always in computer science, 'it depends'.

Re:Mac Apps That Use Garbage Collection Must Move to ARC by mst on Saturday February 21, 2015 @11:55PM (#9088282)
> At best you can know when a dealloction might possibly occur, but this is whenever a reference changes or goes away and usually not pretty helpful.

After a while working with refcounting, I found myself writing code whose natural flow meant I was actually pretty sure, at least for deallocations large enough to care about. I don't even think about it any more, really, I just continue to enjoy my objects going away when I expect them to.

Re:Mac Apps That Use Garbage Collection Must Move to ARC by pjmlp on Saturday February 21, 2015 @12:52PM (#9086198)
You cannot know how much the deallocation impact will be, if a big datastructure is getting its count to 0.
Re:Mac Apps That Use Garbage Collection Must Move to ARC by jahewson on Saturday February 21, 2015 @12:57PM (#9086218)
Of course you can know that, because you know the type of every object. If you're deallocating a big data structure, then the impact will be big. Its easy to choose not to deallocate such objects at an inconvinient time by holding references to them. Compare this with GC where the collection can occur at any time after deallocation, e.g. during an animation.
Re:Mac Apps That Use Garbage Collection Must Move to ARC by pjmlp on Saturday February 21, 2015 @01:22PM (#9086317)
It is also possible to know, approximately, when a GC collection will take place, this is what GC profilers are for.

The thing is that most discussions tend to be reduced to RC vs GC, as if all GCs or RCs where alike, whereas the reality is more complex than that.

RC, usually boils down to dumb RC, deferred RC or RC with couting elision via the compiler. Additionally it can have weak references or rely on a cycle collector.

Whereas GC, can be simple mark-and-sweep, conservative, incremental, generational, concurrent, parallel, real time, with phantom and weak references, constrainted pause times, coloured....

As for frame drops during animations, I think pauses in a missile control system is not something one wishes for:

http://www.militaryaerospace.com/articles/2009/03/thales-cho...

http://www.spacewar.com/reports/Lockheed_Martin_Selects_Aoni...

Re:Mac Apps That Use Garbage Collection Must Move to ARC by jahewson on Saturday February 21, 2015 @02:08PM (#9086477)
Sure, but then real-time systems are a very special use case with their own languages, tools and techniques. Real time GCs are severely limited and would not be suitable for use in a general purpose context.
Re:Mac Apps That Use Garbage Collection Must Move to ARC by danbruc on Saturday February 21, 2015 @01:09PM (#9086263)
You can do almost the same with a garbage collector - keep references alive until a convenient point is reached, than kill those last references and manually force a collection. Not that it seems a good idea, but if you need a level of control as described by you, then you are already actively fighting against automatic reference counting and there seems no point to use any kind of automatic resource management in the first place, at least for the relevant objects.
Re:Mac Apps That Use Garbage Collection Must Move to ARC by jahewson on Saturday February 21, 2015 @02:24PM (#9086527)
It's not possible to do that in Java, though it would work in C#. Retaining references really is not "fighting against automatic reference counting", it's the opposite: the reference clearly and unambiguously defines the lifetime of the object, including deallocation. This is by design and ARC can still be used for any shared references to the object. Forcing GC on the other hand, would be fighting against it because you're circumventing its usual operation.
Re:Mac Apps That Use Garbage Collection Must Move to ARC by cromwellian on Sunday February 22, 2015 @06:52AM (#9089084)
I think technically you could achieve this by having multiple heaps, kind of like custom allocators in other languages.

If Java had a placement syntax like

new (HeapReference) SomeClass(); => allocate in custom Heap

Then you could just turn off GC for that heap, and then blow the whole thing away when it got full. Or, you could have a more fine grained API to allow GC on these custom heaps. Perhaps you could even allow copying objects back to the main heap. You'd have to ban cross heap references or have a smart API that lets users pin an object in the main heap while it is known to be referenced by an object in custom heap.

Most of the time in client applications, you only care about GC pauses on the main UI rendering thread, but GC pauses in other threads are less obvious because there's no jank, just that latency may go up for some operations.

You could have a kind of best of both worlds with languages that support non-GC allocation for your UI thread, but GC everywhere else.

Re:Mac Apps That Use Garbage Collection Must Move to ARC by pjmlp on Sunday February 22, 2015 @08:52AM (#9089397)
You just did a partial description how real time Java works.
Re:Mac Apps That Use Garbage Collection Must Move to ARC by Skinney on Sunday February 22, 2015 @05:43AM (#9088992)
It wouldn't work reliably in C# either. Both platforms allow you to inform the GC that now would be a good time to collect, but in both systems the GC can ignore your advice. C# has structs which will try to allocate on the stack, but that is potentially a big (or impossible) refactoring on your program (unless you plan ahead).
Re:Mac Apps That Use Garbage Collection Must Move to ARC by Skinney on Sunday February 22, 2015 @05:41AM (#9088986)
A GC cannot occur at any tie after deallocation. Well, I guess it depends on the collector really, but usually it happens on allocation. If you avoid allocations, you also avoid collections.
Re:Mac Apps That Use Garbage Collection Must Move to ARC by blub on Saturday February 21, 2015 @04:19PM (#9086988)
Garbage collectors don't solve the problem of "resource" management, but of memory management. Languages like Java have mechanisms such as try with resources (not available in Android) or try finally blocks which seem error prone to me, since the programmer has to know that that particular resource should be released explicitly. They also make developers lazy, they get used to not thinking about object lifetimes and the costs of allocation. YMMV of course.
Re:Mac Apps That Use Garbage Collection Must Move to ARC by mike_hearn on Sunday February 22, 2015 @11:32AM (#9089872)
Try with resources is available in Jellybean+