Hi Alex
Too slow for what?
I'm using Java serialization on Android a lot and it's not slow at all but
of course that depends on what you are doing and how you implement it.
I'm serializing/deserializing neural networks with several thousands of
neurons and connections and yes it takes a couple of seconds to load the
network but doing it in a separate thread keeps the app responsive.
I'm also serializing/deserializing objects of medium complexity in the gui
thread with no impact on responsiveness at all.
Of course using standard Java serialization might be too slow and might also
lead to StackOverflowExceptions.
But fortunately there are ways to speed up serialization (and to prevent
stack overflows):
1. Instead of using ObjectOutputStream.writeObject(ob) you can do an
ob.writeObject(ObjectOutputStream out) with writeObject being your custom
method to write the object.
This is MUCH faster than using standard Java Serialization (not primarily
because of your custom writeObject method but because you avoid all the
highly reflective code that runs between the
ObjectOutputStream.writeObject(ob) method call and your
writeObject(ObjectOutputStream out) method). Deserializing an object can be
done by implementing an according readObject(ObjectInputStream in) method
and calling it directly (ob.readObject(in)) or by implementing a constructor
MyObject(ObjectInputStream in) (my preferred method).
2. Of course a custom writeObject method is also much faster than the
standard implementation because your own implementation won't use
reflection.
3. If your data structure is recursive you might run into stack overflow
problems that can be addressed by writing objects iterative instead of
recursively (as the standard serialization code would do).
4. Having your own implementation also keeps the serialized objects small
because fields can be merged into smaller data structures (several booleans
into integers, shorts etc. or large arrays into a stream of bits, even using
a ZipOuputStream is an option). This can make a huge difference if you
serialize thousands of objects as blobs into a database.
Note 1: whenever you use Java Serialization you want to version the
serialized objects because sometimes in the near future you might want to
change your serialization code but you still have to read serialized objects
in the "old" format.
Note 2: if you use ProGuard don't forget to add a section like the following
to your proguard.cfg (readObject/writeObject methods are normally private
and would be stripped out by ProGuard):
-keepclassmembers class * implements java.io.Serializable {
static final long serialVersionUID;
private static final java.io.ObjectStreamField[] serialPersistentFields;
private void writeObject(java.io.ObjectOutputStream);
private void readObject(java.io.ObjectInputStream);
java.lang.Object writeReplace();
java.lang.Object readResolve();
}
BTW you might want to read:
http://java.sun.com/developer/technicalArticles/Programming/serialization/
Best Regards
Emanuel Moecklin
1gravity LLC
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en