Android:可打包和可序列化之间的区别?
为什么Android提供2个接口用于序列化对象?可序列化对象是否与 Android Binder
和 AIDL 文件互操作?
Why does Android provide 2 interfaces for serializing objects? Do Serializable objects interopt with Android Binder
and AIDL files?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
如果您在 android studio 中使用 paracelable 插件,parcelable 的实现会更快。搜索 Android Parcelable 代码生成器
Implementation of parcelable can be faster if you use paracelable plugin in android studio. search for Android Parcelable code generator
Serialized 接口的使用方式与 Parcelable 接口相同,从而带来(不多)更好的性能。
只需覆盖这两个方法来处理手动编组和解组过程:
不过,在我看来,在开发原生 Android 时,使用 Android api 是最佳选择。
请参阅:
The Serializable interface can be used the same way as the Parcelable one, resulting in (not much) better performances.
Just overwrite those two methods to handle manual marshalling and unmarshalling process:
Still, it seems to me that when developing native Android, using the Android api is the way to go.
See :
1. Serialized
接口是一个标记(没有抽象方法的接口),不需要重新定义任何东西。
2. Parcelable
具有抽象方法的接口。实现时,需要重新定义所有抽象方法,指定需要写入/读取哪些字段以及以什么顺序(通常工作室本身可以生成它们)。
实际上没有人用 Kotlin 编写。为此有一个特殊的注释,通过该注释将自动生成该接口的实现。要使用它,您需要添加一个特殊的插件。
您不必担心实现方法,您可以需要的是实现Parcelable接口并添加@Parcelize注释。
一切都会好起来的并且工作很快!
结果
如果实现 Parcelable 接口而不是 Serialized 接口,实现过程会更快。
1. Serializable
The interface is a marker (an interface without abstract methods), nothing needs to be redefined.
2. Parcelable
An interface that has abstract methods. When implementing it, you need to redefine all abstract methods, specifying which fields and in what order you need to write/read (Usually the studio itself can generate them).
Practically no one writes in Kotlin. There is a special annotation for this, thanks to which the implementation of this interface will be generated automatically. To use it, you need to add a special plugin.
You don't have to worry about implementing methods, all you need is to implement the Parcelable interface and add the @Parcelize annotation.
Everything will be fine and work quickly!
Results
The implementation process is faster if you implement the Parcelable interface instead of Serializable.
Parcelable 比使用 Binder Serialized 快得多,因为 Serialized 使用反射并导致许多 GC。 Parcelable 是为了优化传递对象而设计的。
这是参考链接。
Parcelable much faster than serializable with Binder, because serializable use reflection and cause many GC. Parcelable is design to optimize to pass object.
Here's link to reference.
http://www.developerphil.com/parcelable-vs-serializable/
我回复晚了,但发帖希望对其他人有帮助。
在速度方面,
Parcelable >可序列化
。但是,自定义序列化是例外。它几乎在 Parcelable 的范围内,甚至更快。参考:https://www.geeksforgeeks.org/customized-serialization -and-deserialization-in-java/
示例:
要序列化的自定义类
I am late in answer, but posting with hope that it will help others.
In terms of Speed,
Parcelable > Serializable
. But, Custom Serializable is exception. It is almost in range of Parcelable or even more faster.Reference : https://www.geeksforgeeks.org/customized-serialization-and-deserialization-in-java/
Example :
Custom Class to be serialized
您可以在意图中使用可序列化对象,但在序列化 Parcelable 对象时,它可能会给出严重的异常,例如 NotSerializedException。是否不建议将 Serialized 与 Parcelable 一起使用。因此,最好将 Parcelable 扩展为您想要与捆绑包和意图一起使用的对象。由于此 Parcelable 是 Android 特定的,因此它没有任何副作用。
:)
you can use the serializable objects in the intents but at the time of making serialize a Parcelable object it can give a serious exception like NotSerializableException. Is it not recommended using serializable with Parcelable . So it is better to extends Parcelable with the object that you want to use with bundle and intents. As this Parcelable is android specific so it doesn't have any side effects.
:)
Parcelable 将对象转换为字节流,以便在 Android 的进程之间传递对象。
序列化将 POJO 转换为字符串(JSON 字符串)并可跨平台用于传输对象信息。
Parcelable converts an object to byte stream to pass the object between processes in Android.
Serialization converts POJO to a String (JSON String) and be used across platforms to transfer object info.
Serialized
Serialized 是一个可标记的接口,或者我们可以将其作为一个空接口来调用。它没有任何预先实现的方法。可序列化是将对象转换为字节流。因此用户可以在一个活动之间将数据传递到另一个活动。 Serialized 的主要优点是创建和传递数据非常容易,但与 Parcelable 相比这是一个缓慢的过程。
Parcelable
Parcelable 比 Serialized 更快。 Parcelable 会将对象转换为字节流并在两个活动之间传递数据。与序列化相比,编写可打包的代码有点复杂。在两个活动之间传递数据时,它不会创建更多临时对象。
Serializable
Serializable is a markable interface or we can call as an empty interface. It doesn’t have any pre-implemented methods. Serializable is going to convert an object to byte stream. So the user can pass the data between one activity to another activity. The main advantage of serializable is the creation and passing data is very easy but it is a slow process compare to parcelable.
Parcelable
Parcel able is faster than serializable. Parcel able is going to convert object to byte stream and pass the data between two activities. Writing parcel able code is little bit complex compare to serialization. It doesn’t create more temp objects while passing the data between two activities.
在 Android 中,我们不能只将对象传递给 Activity。为此,对象必须实现
Serialized
或Parcelable
接口。可序列化
可序列化
是一个标准的Java接口。您只需使用Serialized
接口标记您的类即可。这种方法的问题是使用了反射,并且这是一个缓慢的过程。此方法创建了大量临时对象并导致大量垃圾收集。然而,Serialized
接口更容易实现。看下面的示例(可序列化):
Parcelable
Parcelable
过程比Serialized
快得多。原因之一是我们明确了序列化过程,而不是使用反射来推断它。按理说,代码已为此目的进行了大量优化。看下面的示例(Parcelable):
您可以传递 Parcelable 对象的
ArrayList
,如下所示:结论
Parcelable
比Serialized< /code> 接口
Parcelable
接口相比Serialized
接口需要更多时间来实现Serializing
接口更容易实现Serializable
接口创建了很多临时对象并导致相当多的垃圾收集Parcelable
数组可以通过android中的Intent传递In Android we cannot just pass objects to activities. To do this the objects must either implement
Serializable
orParcelable
interface.Serializable
Serializable
is a standard Java interface. You just mark your class with theSerializable
interface. The problem with this approach is that reflection is used and it is a slow process. This method creates a lot of temporary objects and causes quite a bit of garbage collection. However,Serializable
interface is easier to implement.Look at the example below (Serializable):
Parcelable
Parcelable
process is much faster thanSerializable
. One of the reasons for this is that we are being explicit about the serialization process instead of using reflection to infer it. It also stands to reason that the code has been heavily optimized for this purpose.Look at the example below (Parcelable):
You can pass
ArrayList
of Parcelable objects as below:Conclusion
Parcelable
is faster thanSerializable
interfaceParcelable
interface takes more time to implement compared toSerializable
interfaceSerializable
interface is easier to implementSerializable
interface creates a lot of temporary objects and causes quite a bit of garbage collectionParcelable
array can be passed via Intent in android可序列化是一个标准的Java接口。您只需通过实现接口来标记一个类可序列化,Java 将在某些情况下自动序列化它。
Parcelable 是 Android 特定接口,您可以在其中自行实现序列化。创建它的目的是为了比 Serialized 更高效,并解决默认 Java 序列化方案的一些问题。
我相信 Binder 和 AIDL 可以与 Parcelable 对象一起使用。
但是,您可以在 Intents 中使用可序列化对象。
Serializable is a standard Java interface. You simply mark a class Serializable by implementing the interface, and Java will automatically serialize it in certain situations.
Parcelable is an Android specific interface where you implement the serialization yourself. It was created to be far more efficient that Serializable, and to get around some problems with the default Java serialization scheme.
I believe that Binder and AIDL work with Parcelable objects.
However, you can use Serializable objects in Intents.
在 Parcelable 中,开发人员编写用于编组和解组的自定义代码,因此与序列化相比,它创建的垃圾对象更少。由于这种自定义实现,Parcelable 的性能相对于 Serialization 显着提高(大约快两倍)。
Serialized 是一个标记接口,这意味着用户无法根据自己的要求对数据进行编组。在序列化中,编组操作是使用 Java 反射 API 在 Java 虚拟机 (JVM) 上执行的。这有助于识别 Java 对象的成员和行为,但最终也会创建大量垃圾对象。 因此,与 Parcelable 相比,Serialization 过程要慢。
编组和解组的含义是什么?
简而言之,“编组”指的是转换的过程将数据或对象转换为字节流,“解组”是将字节流转换回原始数据或对象的逆过程。转换是通过“序列化”来实现的。
http://www.jguru.com/faq/view.jsp?EID=560072
In Parcelable, developers write custom code for marshalling and unmarshalling so it creates fewer garbage objects in comparison to Serialization. The performance of Parcelable over Serialization dramatically improves (around two times faster), because of this custom implementation.
Serializable is a marker interface, which implies that users cannot marshal the data according to their requirements. In Serialization, a marshalling operation is performed on a Java Virtual Machine (JVM) using the Java reflection API. This helps identify the Java object's member and behaviour, but also ends up creating a lot of garbage objects. Due to this, the Serialization process is slow in comparison to Parcelable.
What is the meaning of marshalling and unmarshalling?
In few words, "marshalling" refers to the process of converting the data or the objects into a byte-stream, and "unmarshalling" is the reverse process of converting the byte-stream back to their original data or object. The conversion is achieved through "serialization".
http://www.jguru.com/faq/view.jsp?EID=560072
Parcelable 是 Android 开发中的一种标准。但并不是因为速度,
Parcelable 是推荐的数据传输方法。但是,如果您按照此存储库中所示正确使用可序列化,您会发现可序列化有时甚至比可打包更快。或者至少时间安排是可比较的。
Parcelable 比 Serialized 更快吗?
不会,如果序列化正确的话。
那么,如果 Serialized 更快、更容易实现,为什么 Android 还要有 Parcelable 呢?
原因是本机代码。 Parcelable 的创建不仅仅是为了进程间通信。它还可用于代码间通信。您可以从 C++ 本机层发送和接收对象。就是这样。
你应该选择什么?两者都会很好用。但我认为 Parcelable 是更好的选择,因为它是 google 推荐的,并且正如您从该线程中看到的那样,它更受赞赏。
Parcelable is sort of a standard in Android development. But not because of speed
Parcelable is recommended approach for data transfers. But if you use serializable correctly as shown in this repo, you will see that serializable is sometimes even faster then parcelable. Or at least timings are comparable.
Is Parcelable faster then Serializable?
No, if serialization is done right.
So, if serializable is faster and easier to implement, why android has parcelable at all?
The reason is native code. Parcelable is created not just for interprocess communication. It also can be used for intercode communication. You can send and recieve objects from C++ native layer. That's it.
What should you choose? Both will work good. But I think that Parcelable is better choice since it is recommended by google and as you can see from this thread is much more appreciated.
来源:http://www.developerphil.com/parcelable-vs-serialized/
Source to this point: http://www.developerphil.com/parcelable-vs-serializable/
实际上,我将成为第一个提倡可序列化的人。速度差异不再那么巨大,因为这些设备比几年前好得多,而且还存在其他更微妙的差异。请参阅我的博客有关此问题的帖子了解更多信息。
I'm actually going to be the one guy advocating for the Serializable. The speed difference is not so drastic any more since the devices are far better than several years ago and also there are other, more subtle differences. See my blog post on the issue for more info.
1.可序列化
@see
http://docs.oracle.com/javase/7/docs/api/ java/io/Serialized.html
接口是什么?
速度
2. Parcelable
@see
http://developer.android.com/reference/android/os/Parcelable.html
接口什么?
速度
>结论
请注意,Serialized 是标准 Java 接口,而 Parcelable 是用于 Android 开发的
1. Serializable
@see
http://docs.oracle.com/javase/7/docs/api/java/io/Serializable.html
Interface of what?
Speed
2. Parcelable
@see
http://developer.android.com/reference/android/os/Parcelable.html
Interface of what?
Speed
> In Conclusion
Be aware that Serializable is a standard Java interface, and Parcelable is for Android Development
编组和解组方面存在一些性能问题。 Parcelable 比 Serialized 快两倍。
请访问以下链接:
http:// /www.3pillarglobal.com/insights/parcelable-vs-java-serialization-in-android-app-development
There is some performance issue regarding to marshaling and unmarshaling. Parcelable is twice faster than Serializable.
Please go through the following link:
http://www.3pillarglobal.com/insights/parcelable-vs-java-serialization-in-android-app-development