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?

Alpha Hydrae
  • 2,891
  • 6
  • 26
  • 24
  • Have you tested the speed difference on a lower powered device? The speed difference is real.. – Cheryl Simon Aug 31 '10 at 18:25
  • This linked question has more benchmarks: https://stackoverflow.com/questions/3323074/android-difference-between-parcelable-and-serializable – David Rawson Jun 29 '20 at 01:27

4 Answers4

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

hackbod
  • 90,665
  • 16
  • 140
  • 154
  • You are absolutely right. However you can always persist it by converting it into a JSON string. – johan Oct 18 '11 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 '11 at 01:40
  • Not persist the parcel. But you can persist the object. – johan Oct 19 '11 at 06:43
  • The object that is parcelable – johan Oct 25 '11 at 20:33
  • Okay, yes you can write a secondary serialization method independent of Parcelable to persist its data. – hackbod Oct 26 '11 at 04:59
  • 1
    can't the SDK itself do the extra work while building the app, so that no reflection will be used during runtime? – android developer Feb 26 '14 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 '15 at 16:13
  • 1
    @nucleo The usual comparison is between automatic serialization vs parcelable (http://www.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: https://bitbucket.org/afrishman/androidserializationtest/issues/1/writelist-vs-open-coding – Blaisorblade Oct 01 '16 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?

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
Charles
  • 1,084
  • 11
  • 14
  • 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 '15 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

afrish
  • 3,167
  • 4
  • 30
  • 38
  • 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 07 '15 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 http://nemanjakovacevic.net/blog/english/2015/03/24/yet-another-post-on-serializable-vs-parcelable/ – damson Apr 08 '15 at 12:45
  • Hadn't noticed your comment was also an answer, my comment elsewhere applies: http://stackoverflow.com/questions/3611843/is-using-serializable-in-android-bad/3612364#comment66914906_3612364 – Blaisorblade Oct 01 '16 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 https://stackoverflow.com/questions/3323074/android-difference-between-parcelable-and-serializable – David Rawson Jun 29 '20 at 01:35
  • this answer's link is dead now, may you fix it? – Dmitriy Pavlukhin Jul 06 '21 at 11:30
  • 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 06 '21 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.

Johan
  • 176
  • 7