57

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?

2

4 Answers 4

68

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. :}

8
  • You are absolutely right. However you can always persist it by converting it into a JSON string.
    – johan
    Oct 18, 2011 at 20:53
  • If you are saying to persist a parcel into a JSON string... no, no you can't. Re-encoding it as a JSON string doesn't change the fact that the data in it doesn't have guarantees about data consistency with things change or have a format that is guaranteed to be the same across platform versions.
    – hackbod
    Oct 19, 2011 at 1:40
  • 1
    can't the SDK itself do the extra work while building the app, so that no reflection will be used during runtime? Feb 26, 2014 at 12:07
  • 5
    Do you have any proofs to support your statement "For in-memory use, Parcelable is far, far better than Serializable"? According to my tests it is much slower than Serializable.
    – afrish
    Mar 29, 2015 at 16:13
  • 1
    @nucleo The usual comparison is between automatic serialization vs parcelable (developerphil.com/parcelable-vs-serializable). Manual serialization is faster than automatic serialization but has the same maintenance disadvantages of Parcelable. Regarding Parcelable vs manual Serializable, the culprit might be with Parcel.writeValue, see issue: bitbucket.org/afrishman/androidserializationtest/issues/1/… Oct 1, 2016 at 21:51
58

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?

1
  • 2
    Even better, according to my observations, usual Java serialization (if done right) is much faster, than Parcelable. See details in my answer below.
    – afrish
    Mar 29, 2015 at 16:10
20

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

6
  • 3
    @damson That's why I call it "serialization done right". In the article above, which everybody refers to, serialization is not used in its full potential. This is unfair comparison. In my benchmark I tried to make it more fair, and results are completely different. The problem is that everybody "forgets" about writeObject() / readObject() methods that allow to make Java built-in serialization lightning fast wihtout messing with platform specific things like Parcelable.
    – afrish
    Apr 7, 2015 at 13:39
  • 3
    I totally agree. And actually I feel like, there are both usefull & quiet equivalents, but just depending of the scenario of your app. Here another article putting ligth this time on Serializable nemanjakovacevic.net/blog/english/2015/03/24/…
    – damson
    Apr 8, 2015 at 12:45
  • Hadn't noticed your comment was also an answer, my comment elsewhere applies: stackoverflow.com/questions/3611843/… Oct 1, 2016 at 21:52
  • This is a nice benchmark, but I don't think it reflects the way Serializable tends to get used in Android projects i.e., without the writeObject method "overrides". Without these overrides reflection is used and serializable will be slower as per the answers in stackoverflow.com/questions/3323074/… Jun 29, 2020 at 1:35
  • 1
    @DmitriyPavlukhin This is a very old project, more than 6 years old. I transferred it to GitHub though and edited the link. Not sure it still runs :)
    – afrish
    Jul 6, 2021 at 13:54
1

Has anyone considered serialization using JSON and passing the data as a string? GSON and Jackson should be efficient enough to be a competitor to Parcelable as well as Serializable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.