Recently in my Android programming escapades I found something interesting and bothersome in an API I had written. I had created a singleton class for the main renderer of my graphics API. With the first app I was making I had no problem, and it in fact was quite helpful and easy to write, after all each app using this API would only have access to one renderer.
The problem arose out of some strangeness with the Android VM. It turns out that static fields of a class can but not necessarily cross from one app to another, effectively sharing one singleton across more than one activity. This might not sound too bad but it can be a problem if you use an API in multiple apps and they happen to run on the same device. This can result in undesirable effects.
So I removed the singleton property from the renderer object, and thus had to pass it around in method parameters like a cheap whore. It’s not an ideal solution but it worked, and if you believe some people on the internet it’s probably a better design. Whilst there was minimal impact on the API, an app I was writing needed to be hacked a little to make it work properly. The funny thing is if there was a separate JVM or classloader for every instance of all apps it would have been fine to have the renderer as a singleton and it would work.
So why are singletons a problem on Android? It seems that there is a single JVM running all the Android apps together. This means that static variables (including singleton classes) can end up hanging around long after an activity ends. If a class is shared with another app they can end up using the same singleton class. The other half of the problem is the Android OS will sometimes destroy an app or use a different class loader for a new activity thus making the static/singleton unreliable for any kind of storage or communications across activities or apps.
So it’s best to avoid the use of singletons on Android.
There seems to have been some debate about singletons and their use. Whilst it is true that many programmers over use the pattern and they can be a problem, they do have a place in a developers toolbox. There are common interfaces such as OpenGL and others which cannot be accessed in an easily nice OO way.
OpenGL in particular is a good example, it is not thread safe and basically requires all rendering be done in a single thread. It also is not Object oriented and as such there is only one instance of it per application at best. Because of its nature it is essentially an interface to the hardware acceleration of a machine, which is better suited to a singleton/procedural style interface.
The thing with OO design is it isn’t suited 100% to all programming problems. There are in fact many instances where using an OO design is a bad idea, such as embedded systems and applications that require high performance. The trouble is all the OO loveliness comes at a performance and memory size cost, that luckily isn’t a problem for most applications. This leads to the situation where something OO needs to interface to something that isn’t, this is where singletons are a useful construct.
Something my Dad always said is “a bad tradesmen blames the tools”. It seems there are many people out there blaming the singleton as if it is at fault for being used incorrectly. Sure there may be many cases of people using it poorly, but that just means they are poor designers/coders. The singleton pattern is just a tool, used appropriately it can make your coding life easier, used badly it can make your life harder. This is true for all the basic design patterns, it’s up to the designers and coders how good their designs and code are.
Recent Comments