News for hackers. Stuff that matters.
Re:Mac Apps That Use Garbage Collection Must Move to ARC by danbruc on Saturday February 21, 2015 @10:20AM (#9085702)
What is the reasoning behind that? Given the advances over the last decade or two automatic garbage collection is good enough for a lot of scenarios and this looks like a step back. Why not use automatic garbage collection as the default but offer additional means for manual resource management in case it is really needed?
Re:Mac Apps That Use Garbage Collection Must Move to ARC by pjmlp on Saturday February 21, 2015 @11:04AM (#9085821)
GC never worked properly in Objective-C.

Apple failed to produce a working GC that didn't crash all the time, because it required all Objective-C libraries to be compiled the same way.

Additionally the C subset only allows for conservative GC, which is not optimal when performance matters.

So in the end they went with ARC, with isn't nothing more than having the compiler produce the retain/release calls that Mac OS X frameworks were already expecting anyway.

So this has more to do with how technically feasible it is to have a proper GC in Objective-C, than GC in general.

Re:Mac Apps That Use Garbage Collection Must Move to ARC by ttflee on Saturday February 21, 2015 @10:39AM (#9085758)
IMHO, they want to get everything that has a UI as responsive as possible.

GC in theory is a good feature but it does not guarantee that constant time is used for each mark-and-sweep cycle. And together with multithreading that means uncertainty for rendering every frame of video, which is done in the main thread of an App 60 times per second. And for modern system, anything you see on screen is part of this rendering. Try to think about how could memory shared by main thread and a background thread be GCed. It could turn out to be: halting the process for GC, retaining some memory forever, or implementing some complicated algorithm that is too difficult to debug/maintain.

Re:Mac Apps That Use Garbage Collection Must Move to ARC by oconnor0 on Saturday February 21, 2015 @10:43AM (#9085766)
That can be resolved with a different GC.
Re:Mac Apps That Use Garbage Collection Must Move to ARC by fidotron on Saturday February 21, 2015 @10:52AM (#9085786)
Not really.

I've had to deploy lots of soft real time apps on GCed environments over the years, and it's always a problem. You can work around it with things like object pools, but some library or API will assume that the GC is OK and will be quietly spitting out objects continuously which will lead to a GC pause.

It's worth pointing out the Android devs finally started noticing this for Lollipop (probably due to their animations) and the API now has lots of places where it passes Java primitives instead of objects, which is the distinction between passing by value and by reference. Even if you're in C++ modern compilers can only make the most out of it if you pass by value, as this enables all sorts of other optimisations to kick in.

The key benefit of reference counting is it's predictable. Real time systems are also not strictly the lowest latency, they are defined by predictability. This becomes a preoccupation with minimising your worst case scenario.

Re:Mac Apps That Use Garbage Collection Must Move to ARC by mike_hearn on Saturday February 21, 2015 @12:10PM (#9086076)
Jellybean and Lollipop didn't get better smoothness by replacing objects with primitives, the Android API is fixed by backwards compatibility requirements. They did it through a mix of better graphics code and implementing a stronger GC.

If you look at the most advanced garbage collectors like G1 you can actually give them a pause time goal. They will do their best to never pause longer than that. If pauses are getting too long they increase memory usage to give more breathing room. If pauses are reliably less, they shrink the heap and give memory back to the OS.

Reference counting is not inherently predictable and can sometimes be less predictable than GC. The problem with refcounting is it can cause deallocation storms where a large object graph is suddenly released all at once because some root object was de-reffed. And then the code has to go through and recursively unref the entire object graph and call free() on it, right at that moment. If the user is dragging their finger at that time, tough cookies. GC on the other hand can take a hint from the OS that it'd be better to wait a moment before going in and cleaning up .... and it does.

It gets even worse when you consider that malloc/free are themselves not real time. Mallocs are allowed to spend arbitrary amounts of time doing bookkeeping, collapsing free regions etc and it can happen any time you allocate or free. With a modern GC, an allocation is almost always just a pointer increment (unless you've actually run out of memory).

The problem Apple has is that their entire toolchain is based on early 1990's era NeXT technology. That was great, 25 years ago. It's less great now. Objective-C manages to excel at neither safety nor performance and because it's basically just C with extra bits, it's hard to use any modern garbage collection techniques with it. For instance Boehm GC doesn't support incremental or generational collection on OS X and I'm unaware of any better conservative GC implementation.

Some years ago there was a paper showing how to integrate GC with kernel swap systems to avoid paging storms, which has been the traditional weakness of GC'd desktop apps. Unfortunately the Linux guys didn't do anything with it and neither has Apple. If you spend all day writing kernels "just use malloc" doesn't seem like bad advice.

Re:Mac Apps That Use Garbage Collection Must Move to ARC by tjl on Saturday February 21, 2015 @04:38PM (#9087084)
Objective-C uses autorelease pools so deallocation doesn't happen immediately when the refcount goes to zero. Its reference counting implementation is smarter than a simple naïve one.

Apple's GC implementation wasn't a Boehm GC [1].

It's true that it's hard to use a tracing GC with Objective-C, because of the C. But, if you want interoperability with C, you're kind of stuck.

[1] http://www.reddit.com/r/programming/comments/2wo18p/mac_apps...

Re:Mac Apps That Use Garbage Collection Must Move to ARC by coliveira on Saturday February 21, 2015 @08:18PM (#9087778)
> The problem with refcounting is it can cause deallocation storms where a large object graph is suddenly released all at once because some root object was de-reffed

This only happens if you choose to organize the data this way. This is a big difference from GC, where the whole memory layout and GC algorithm is out of your control.

Re:Mac Apps That Use Garbage Collection Must Move to ARC by Skinney on Sunday February 22, 2015 @05:48AM (#9089003)
Depends on the language. Go, for instance, gives you a lot of freedom when it comes to memory layout, and allows you stack allocate objects to avoid GC.
Re:Mac Apps That Use Garbage Collection Must Move to ARC by coliveira on Sunday February 22, 2015 @10:04PM (#9092326)
If you're using the stack, by definition you're not using GC, so this cannot be an argument in favor of GC.
Re:Mac Apps That Use Garbage Collection Must Move to ARC by Skinney on Tuesday February 24, 2015 @04:34PM (#9103686)
Im not arguing in favor of GC. The argument was that a GC takes away memory control, but memory control is up to the language.

Go doesn't restrict you to stack/heap allocation. You can create structs which embeds other structs. This simplifies the job the GC has to do, even if you don't allocate on the stack.

You can do something similar with Struct types in C#.

Re:Mac Apps That Use Garbage Collection Must Move to ARC by fidotron on Saturday February 21, 2015 @12:22PM (#9086116)
Better graphics code does mean what I'm on about. It's about removing any triggers for GC, which means removing allocations.

If you're blocking your UI thread with deallocating a giant graph of doom then you have other problems. Deferring pauses, however, is not a realistic option.

Re:Mac Apps That Use Garbage Collection Must Move to ARC by mike_hearn on Sunday February 22, 2015 @11:15AM (#9089809)
I was referring to the triple buffering, full use of GL for rendering and better vsyncing when I talked about graphics changes, not GC stuff. That was separate and also makes things smoother but it's unrelated.

Deferring pauses is quite realistic for many kinds of UI interaction and animation. If your animation is continuous/lasts a long time and requires lots of heap mutation then you need a good GC or careful object reuse, but then you can run into issues with malloc/free too. But lots of cases where you need something smooth don't fit that criteria.

Re:Mac Apps That Use Garbage Collection Must Move to ARC by krschultz on Saturday February 21, 2015 @12:03PM (#9086048)
It's worth pointing out the Android devs finally started noticing this for Lollipop (probably due to their animations) and the API now has lots of places where it passes Java primitives instead of objects, which is the distinction between passing by value and by reference.

This is simply not true. The API has always been heavily based on Java primitives. They didn't even use enums in the older APIs, preferring instead int constants. (I hate that one personally). GC pauses have always been a point of focus for the Android platform.

Re:Mac Apps That Use Garbage Collection Must Move to ARC by fidotron on Saturday February 21, 2015 @12:14PM (#9086085)
For example: http://developer.android.com/reference/android/graphics/Canv...

Notice the introduction of methods with API level 21 that recreate existing functionality without RectF objects being allocated. Touch events, for example, still spit ludicrous amounts of crap on to the heap.

All this is why the low latency audio is via the NDK as it's basically impossible to write Android apps which do not pause the managed threads at some point. Oddly this is stuff the J2ME people got right from day one.

Re:Mac Apps That Use Garbage Collection Must Move to ARC by MichaelGG on Saturday February 21, 2015 @11:34PM (#9088246)
That's terribly ugly. Can they not do escape analysis or something to avoid allocations in obvious places? Or only allocate when the value is moved off the stack?

Doesn't Android use a different flavor of Java anyways, allowing them to make these changes?

Re:Mac Apps That Use Garbage Collection Must Move to ARC by mike_hearn on Sunday February 22, 2015 @11:36AM (#9089886)
Yes, it's possible in theory, but Dalvik/ART don't do it. HotSpot does some escape analysis and the Graal compiler for HotSpot does a much more advanced form called partial escape analysis, which is pretty close to ideal if you have aggressive enough inlining.

The problem Google has is that Dalvik wasn't written all that well originally. It had problems with deadlocking due to lock cycles and was sort of a mishmash of C and basic C++. But then again it was basically written by one guy under tight time pressure, so we can give them a break. ART was a from scratch rewrite that moved to AOT compilation with bits of JITC, amongst other things. But ART is quite new. So it doesn't have most of the advanced stuff that HotSpot got in the past 20 years.

Re:Mac Apps That Use Garbage Collection Must Move to ARC by pjmlp on Monday February 23, 2015 @02:35AM (#9093111)
Yes, this is why I always find sad that language performance gets thrown around in discussions forums without reference to what implementations are actually being discussed.
Re:Mac Apps That Use Garbage Collection Must Move to ARC by pjmlp on Saturday February 21, 2015 @12:45PM (#9086178)
J2ME got some bad naming due to the firware problems that OEMs brought to the platform, but Android is becoming worse than it.
Re:Mac Apps That Use Garbage Collection Must Move to ARC by danbruc on Saturday February 21, 2015 @12:55PM (#9086210)
Reference counting does not provide any guarantees when objects get deallocated on its own either, any removed reference may make the counter zero and trigger a deallocation. It may of course be worse with a full blown garbage collector building up a huge pile of unused objects and then cleaning them up all at once. But that is not a necessary limitation, there are already garbage collectors performing the entire work in parallel with the normal application execution.
Re:Mac Apps That Use Garbage Collection Must Move to ARC by tjl on Saturday February 21, 2015 @04:31PM (#9087050)
Objective-C uses pools, so deallocation doesn't happen automatically when the reference count hits zero. Apple's reference counting implementation is fairly smart.

Over on the Reddit discussion there was a comment from Ridiculous Fish who at least was an Apple developer (and probably still is) and worked on adding GC to the Cocoa frameworks,

http://www.reddit.com/r/programming/comments/2wo18p/mac_apps...

Basically, because of interop with C, there's only so much you can do. Plus, the tracing GC wasn't on iOS so if you want unified frameworks (for those that make sense cross-platform), supporting the tracing GC along with ARC is added work.

Re:Mac Apps That Use Garbage Collection Must Move to ARC by ttflee on Saturday February 21, 2015 @10:48AM (#9085775)
Yes, I have heard of some algorithms that consume a lot more memory to balance timing.

E.g.: http://michaelrbernste.in/2013/06/03/real-time-garbage-colle...

Re:Mac Apps That Use Garbage Collection Must Move to ARC by riffraff on Saturday February 21, 2015 @10:28AM (#9085736)
my understanding: they have abandoned garbage collection in Obj-C years ago and think ARC is overall better.

And my guess: GC probably doesn't play nice with recent/future changes in OSX memory management (e.g. compressed memory) so the less apps use it the better the whole system is overall.

Hence, they probably want to get rid of the whole thing ASAP, forbidding it _for apps submitted to the mac app store_ is a reasonable step in that direction.

Re:Mac Apps That Use Garbage Collection Must Move to ARC by threeseed on Saturday February 21, 2015 @10:38AM (#9085755)
Probably because Apple is moving towards a unified iOS/OSX SDK. We have seen evidence of this with the Photos app which uses UXKit (unified AppKit/UIKit). I would imagine that Apple is pushing developers towards this new SDK landscape step by step starting with ARC.

There is a precedence for Apple doing this usually related to a new hardware platform e.g. x86, 64-bit and perhaps ARM Macs.

Re:Mac Apps That Use Garbage Collection Must Move to ARC by MaxGabriel on Saturday February 21, 2015 @10:34AM (#9085749)
Could be they just don't want to support the garbage collector anymore.
Re:Mac Apps That Use Garbage Collection Must Move to ARC by bitwize on Saturday February 21, 2015 @11:06AM (#9085827)
Everything GC can do, ARC can do in strict space and time constraints.
Re:Mac Apps That Use Garbage Collection Must Move to ARC by danbruc on Saturday February 21, 2015 @11:36AM (#9085936)
Plain (automatic) reference counting can not handle cycles without support from developers.
Re:Mac Apps That Use Garbage Collection Must Move to ARC by blub on Saturday February 21, 2015 @04:04PM (#9086932)
I find that a good thing, because developers are trained to think that they have to use weak references and think about object lifetimes.

Android developers tend to not think so much about lifetimes which leads to memory leaks because Dalvik apparently can't figure out some of those cycles by itself. Just do a search for "Android memory leak" to see some examples.

Re:Mac Apps That Use Garbage Collection Must Move to ARC by tomjen3 on Sunday February 22, 2015 @07:58AM (#9089223)
Try to parse any tree structure without creating cycles (say the XML/XHTML inside an ebook) - that may be possibly with just one or two devs on the project but as soon as every dev doesn't know every line of code there is an oppertunity for errors and you only need one link to leak memory.

That Dalvik can't handle the cycles is stupid but an issue with Dalvik (that is likely to be phased out with the new runtime) not an issue with GC. Computers are really, really, good about executing trivial tasks repeatedly without ever making a stupid mistake, humans not so.

And as Terminator taught us: never send a human to do a machines job.

Re:Mac Apps That Use Garbage Collection Must Move to ARC by smrtinsert on Saturday February 21, 2015 @07:13PM (#9087587)
A platform with 80% of mobile installations has developers who don't understand object lifecycles Got it.
Re:Mac Apps That Use Garbage Collection Must Move to ARC by kasabali on Saturday February 21, 2015 @07:40PM (#9087656)
I guess he meant Android application developers, not the people who wrote Dalvik.
Re:Mac Apps That Use Garbage Collection Must Move to ARC by danbruc on Saturday February 21, 2015 @01:15PM (#9086286)
And time constraints are also more of an illusion than real - unless your program has no input from the outside world you can not really know when a reference count will drop to zero and triggers a deallocation. Every time a reference goes away may be the time an (unexpected) deallocation happens.
Re:Mac Apps That Use Garbage Collection Must Move to ARC by chrisseaton on Saturday February 21, 2015 @07:56PM (#9087704)
Can ARC move objects to compact the heap?