Is using Serializable in Android bad?
Asked Answered
L

4

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?

Lareine answered 31/8, 2010 at 18:1 Comment(2)
Have you tested the speed difference on a lower powered device? The speed difference is real..Shayla
This linked question has more benchmarks: #3323574Simar
S
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. :}

Starstarboard answered 31/8, 2010 at 19:14 Comment(8)
You are absolutely right. However you can always persist it by converting it into a JSON string.Birgitbirgitta
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.Starstarboard
Not persist the parcel. But you can persist the object.Birgitbirgitta
The object that is parcelableBirgitbirgitta
Okay, yes you can write a secondary serialization method independent of Parcelable to persist its data.Starstarboard
can't the SDK itself do the extra work while building the app, so that no reflection will be used during runtime?Heisenberg
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.Gauleiter
@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/…Marinna
T
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?

Trakas answered 2/2, 2013 at 20:58 Comment(1)
Even better, according to my observations, usual Java serialization (if done right) is much faster, than Parcelable. See details in my answer below.Gauleiter
G
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

Gauleiter answered 29/3, 2015 at 16:8 Comment(6)
@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.Gauleiter
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/…Castrate
Hadn't noticed your comment was also an answer, my comment elsewhere applies: #3612343Marinna
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 #3323574Simar
this answer's link is dead now, may you fix it?Metastasize
@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 :)Gauleiter
L
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.

Leveller answered 3/6, 2015 at 8:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.