为什么Android为序列化对象提供了两个接口?序列化对象与Android Binder和AIDL文件interopt ?
当前回答
打包比使用Binder序列化快得多,因为序列化使用反射并导致许多GC。Parcelable是为了优化传递对象而设计的。
这是参考链接。 http://www.developerphil.com/parcelable-vs-serializable/
其他回答
在Parcelable中,开发人员编写用于编组和反编组的自定义代码,因此与Serialization相比,它创建的垃圾对象更少。由于这个自定义实现,Parcelable over Serialization的性能显著提高(大约快两倍)。
Serializable是一个标记接口,这意味着用户不能根据自己的需求编组数据。在序列化中,使用Java反射API在Java虚拟机(JVM)上执行编组操作。这有助于识别Java对象的成员和行为,但最终也会创建大量垃圾对象。因此,与Parcelable相比,Serialization进程较慢。
编组和反编组的含义是什么?
简而言之,“编组”是指将数据或对象转换为字节流的过程,而“解组”是将字节流转换回原始数据或对象的相反过程。转换是通过“序列化”实现的。
http://www.jguru.com/faq/view.jsp?EID=560072
如果你在android studio中使用paracelable插件,parcelable的实现可以更快。搜索Android Parcelable代码生成器
If you want to be a good citizen, take the extra time to implement Parcelable since it will perform 10 times faster and use less resources. However, in most cases, the slowness of Serializable won’t be noticeable. Feel free to use it but remember that serialization is an expensive operation so keep it to a minimum. If you are trying to pass a list with thousands of serialized objects, it is possible that the whole process will take more than a second. It can make transitions or rotation from portrait to lanscape feel very sluggish.
来源:http://www.developerphil.com/parcelable-vs-serializable/
打包比使用Binder序列化快得多,因为序列化使用反射并导致许多GC。Parcelable是为了优化传递对象而设计的。
这是参考链接。 http://www.developerphil.com/parcelable-vs-serializable/
我将成为一个支持Serializable的人。由于这些设备比几年前好得多,速度上的差异已经不那么明显了,而且还有其他更微妙的差异。更多信息请参见我的博客文章。
推荐文章
- 警告:API ' variable . getjavacompile()'已过时,已被' variable . getjavacompileprovider()'取代
- 安装APK时出现错误
- 碎片中的onCreateOptionsMenu
- TextView粗体通过XML文件?
- 如何使线性布局的孩子之间的空间?
- DSL元素android.dataBinding。enabled'已过时,已被'android.buildFeatures.dataBinding'取代
- ConstraintLayout:以编程方式更改约束
- PANIC: AVD系统路径损坏。检查ANDROID_SDK_ROOT值
- 如何生成字符串类型的buildConfigField
- Recyclerview不调用onCreateViewHolder
- Android API 21工具栏填充
- Android L中不支持操作栏导航模式
- 如何在TextView中添加一个子弹符号?
- PreferenceManager getDefaultSharedPreferences在Android Q中已弃用
- 在Android Studio中创建aar文件