是采用序列化的安卓不好?不好、序列化

2023-09-12 02:44:17 作者:阳光下的白鸽

我已经读了很多帖子和文章颂扬Parcelable的速度在序列化的。我一直在同时使用了一段时间,通过意图传递活动之间的数据,并且还没有在两者之间切换时发现任何速度上的差异。数据的典型量我不得不转移为5〜15 2〜5字段嵌套对象每

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.

由于我有大约30个教学班,必须转让,实施Parcelable需要大量的样板code,增加了维修时间。我的一个电流的要求也是编译code应该尽可能小;我希望我可以通过序列化在Parcelable腾出一些空间。

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.

我应该使用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?

推荐答案

有关内存使用,Parcelable是远远比序列化更好。我强烈建议不要使用序列化。

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

您不能使用Parcelable为将要存储在磁盘上(因为它没有关于数据的一致性很好的保证,当事情的变化)的数据,但是序列化是足够慢,不是使用它有任我强烈要求。你最好自己写的数据。

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.

此外,与序列化的性能问题之一是,它结束旋转通过大量的临时对象,导致大量GC活动在你的应用程序。这是pretty的令人发指。 :}

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