My case for garbage collection in Swift
December 4, 2015
Now that Swift thas been open sourced - the ability to make a backend in Swift is much most accessible (since you can now run it on Linux).
Something I think Apple should do, is implement garbage collection for Swift. Even though the recent history of Apple with regards to garbage collection, didn't end well for Objective-C.
First here are the higher level reasons I think a garbage collector should be added by Apple:
While restarting the server or process whenever you run out of memory could be an okay solution, it hurts your rate of uptime - which is also far more important for server side software.
Another concern (which is far harder to provide much evidence of unfortunately), is that if a popular fork of Swift is one that implements garbage collection while the official one does not. The addition of garbage collection to a language is a relatively large project that would probably require a fair amount of changes to the Swift runtime - which could drastically reduce the portability of Swift code potentially more than other changes might.
Another set of concerns that I have are more related to day to day annoyances that I run into. For example - delegation is a common pattern in Cocoa and Cocoa apps, something like this:
Because we have to instruct the compiler what the retain counting semantics we have these two issues:
Ideally - I would love to be able to write this like:
In conclusion - I hope that someday we get garbage collection for Swift as I think it improves the viability of Swift on the server and provides a better programming model for software written in Swift.
Something I think Apple should do, is implement garbage collection for Swift. Even though the recent history of Apple with regards to garbage collection, didn't end well for Objective-C.
First here are the higher level reasons I think a garbage collector should be added by Apple:
- Memory leaks become a much larger problem in server side software
- I believe someone will have a fork of Swift that implements garbage collection
While restarting the server or process whenever you run out of memory could be an okay solution, it hurts your rate of uptime - which is also far more important for server side software.
Another concern (which is far harder to provide much evidence of unfortunately), is that if a popular fork of Swift is one that implements garbage collection while the official one does not. The addition of garbage collection to a language is a relatively large project that would probably require a fair amount of changes to the Swift runtime - which could drastically reduce the portability of Swift code potentially more than other changes might.
Another set of concerns that I have are more related to day to day annoyances that I run into. For example - delegation is a common pattern in Cocoa and Cocoa apps, something like this:
Because we have to instruct the compiler what the retain counting semantics we have these two issues:
- Our delegate must be
(as an expectation of delegation is the delegate could be the same object that owns the object without causing a retain cycle).weak
- Since our delegate must be
, the protocol must qualify that types that conform to it must be a class - so the delegate can not be a value type.weak
- We also are forced to make it both optional and mutable (because ARC sets it to
). This means there is an additional state you have to account for.nil
weak
you might suggest using unowned
- while this would prevent retain cycles, it could also leave a dangling pointer if that object is released; which would crash if you tried to call any methods on it or access any properties. Ideally - I would love to be able to write this like:
In conclusion - I hope that someday we get garbage collection for Swift as I think it improves the viability of Swift on the server and provides a better programming model for software written in Swift.