Is using Serializable in Android bad?

I've been reading a lot of posts and articles extolling the speed of Parcelable over Serializable. I've been using both for a while to pass data between Activities through Intents, and have yet to notice any speed difference when switching between the two. The typical amount of data I have to transfer is 5 to 15 nested objects with 2 to 5 fields each.

Since I have about 30 classes which must be transferable, implementing Parcelable requires a lot of boilerplate code that adds maintenance time. One of my current requirements is also that the compiled code should be as small as possible; I expect that I could spare some space by using Serializable over Parcelable.

Should I use Parcelable or is there no reason to use it over Serializable for such small amounts of data? Or is there another reason why I shouldn't use Serializable?


Solution 1:

For in-memory use, Parcelable is far, far better than Serializable. I strongly recommend not using Serializable.

You can't use Parcelable for data that will be stored on disk (because it doesn't have good guarantees about data consistency when things change), however Serializable is slow enough that I would strongly urge not using it there either. You are better off writing the data yourself.

Also, one of the performance issues with Serializable is that it ends to spin through lots of temporary objects, causing lots of GC activity in your app. It's pretty heinous. :}

Solution 2:

Continue to use Serialization. You'll see lots of people online who will tell you that Serialization is very slow and inefficient. That is correct. But, one thing you never want to do as a computer programmer is take any comment about performance as an absolute.

Ask yourself if serialization is slowing your program down. Do you notice when it goes from activity to activity? Do you notice when it saves/loads? If not, it's fine. You won't get a smaller footprint when you go to a lot of manual serialization code, so there is no advantage there. So what if it is 100 times slower than an alternative if 100 times slower means 10ms instead of 0.1ms? You're not going to see either, so who cares? And, why would anyone invest massive effort into writing manual serialization for 30 classes when it won't make any perceptible difference in performance?

Solution 3:

Everybody just blindly states that Parcelable is better and faster, than Serializable, but nobody has tried to support his statements with any proofs. I decided to test this myself and I got very interesting results.

Usual Java serialization on an average Android device (if done right) is about 3.6 times faster than Parcelable for writes and about 1.6 times faster for reads.

You can check my test project here: https://github.com/afrish/androidserializationtest