tags:

views:

1113

answers:

3

I have an ArrayList of custom, simple Serializable objects I would like to cache to disk and read on re-launch. My data is very small, about 25 objects and at most 5 lists so I think SQLite would be overkill. In the iPhone world I would use NSKeyedArchiver and NSKeyedUnarchiver which works great. On Android I've attempted to do this with with a FileOutputStream and ObjectOutputStream and while the result is the same, the performance is terrible. Is there a better (read faster) way to cache small objects to the file system in Android?

A: 

Perhaps you'd get faster serialisation by implementing Android's Parcelable interface, but it seems odd that you're getting such poor performance just from native serialisation.

I would say it's easiest to use a SharedPreferences object to store data, but that doesn't really help for complex object types.

Anyway, I've worked on an app that uses SQLite extensively and the access times are ridiculously fast. For the time it takes to implement, you could give it a go.

Christopher
According to the Android documentation Parcelable is not the right solution here, from http://developer.android.com/reference/android/os/Parcel.html::Parcel is not a general-purpose serialization mechanism. This class (and the corresponding Parcelable API for placing arbitrary objects into a Parcel) is designed as a high-performance IPC transport. As such, it is not appropriate to place any Parcel data in to persistent storage: changes in the underlying implementation of any of the data in the Parcel can render older data unreadable."
Greg Martin
Ah yes, good point.. I even read that documentation recently! Oops.
Christopher
+2  A: 

For what it worth I cache some of my String data to disk using BufferedWriter/BufferedReader and it's very fast. Matter of fact it is faster than storing the same data to SharedPreferences. The code goes something like this (note that things happen faster when you provide buffer size)

final BufferedWriter out = new BufferedWriter(new FileWriter(file), 1024);
out.write(stuff);
out.close();
DroidIn.net
Yeah, the problem is I want "stuff" to be my serialized objects which for whatever reason seems to be the issue.
Greg Martin
Well if your objects are simple enough you can overwrite readObject and writeObject
DroidIn.net
So I ended up just writing the raw JSON text out to a file using this method and then re-parsing it when I launched. Since the JSON is small it seems to perform OK, though I'm still not completely happy with not being able to serialize my objects to disk.
Greg Martin
There could be a lot going on behind the scenes with Serialization. I've usually found much better performance by using the Externalizable interface. You end up having to code a little more, but I've always seen a huge increase. This could be even more dramatic on a mobile device. http://stackoverflow.com/questions/817853/what-is-the-difference-between-serializable-and-externalizable-in-java
GrkEngineer
+1  A: 

It's hard to know without profiling but my guess is your poor performance is down to using ObjectOutputStream. Have you tried writing your own writeObject(ObjectOutputStream) and readObject(ObjectOutputStream) methods as this may help performance.

You could use the traceview tool to see exactly where the application is running slow. Have a look at this question for instructions on how to use traceview.

Dave Webb