Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serialization Performance and Google Android

I'm looking for advice to speed up serialization performance, specifically when using the Google Android. For a project I am working on, I am trying to relay a couple hundred objects from a server to the Android app, and am going through various stages to get the performance I need.

First I tried a terrible XML parser that I hacked together using Scanner specifically for this project, and that caused unbelievably slow performance when loading the objects (~5 minutes for a 300KB file). I then moved away from that and made my classes implement Serializable and wrote the ArrayList of objects I had to a file. Reading that file into the objects the Android, with the file already downloaded mind you, was taking ~15-30 seconds for the ~100KB serialized file. I still find this completely unacceptable for an Android app, as my app requires loading the data when starting the application.

I have read briefly about Externalizable and how it can increase performance, but I am not sure as to how one implements it with nested classes. Right now, I am trying to store an ArrayList of the following class, with the nested classes below it.

public class MealMenu implements Serializable{
private String commonsName;
private long startMillis, endMillis, modMillis;
private ArrayList<Venue> venues;
private String mealName;
}

And the Venue class:

public class Venue implements Serializable{
private String name;
private ArrayList<FoodItem> foodItems;
}

And the FoodItem class:

public class FoodItem implements Serializable{
private String name;
private boolean vegan;
private boolean vegetarian;
}

IF Externalizable is the way to go to increase performance, is there any information as to how java calls the methods in the objects when you try to write it out? I am not sure if I need to implement it in the parent class, nor how I would go about serializing the nested objects within each object.

like image 651
Johan Henkens Avatar asked May 27 '10 00:05

Johan Henkens


1 Answers

Never use Serializable across architectures. You have no way of knowing if the Dalvik VM will have compatible serialization to your server's Java version. Even if it works today, it might not with upgrades on either end. Always choose something that is designed specifically to work across architectures.

Options include:

  • Protocol Buffers
  • Apache Thrift (the payload packaging, not necessarily the RPC stuff)
  • XML (you neglected to mention what parser you used -- if you used DOM, try SAX)
  • JSON (reportedly the Jackson JSON parser is faster than the org.json parser built into Android)

Also, loading 300K of data on app startup is a prescription for trouble. Please consider leveraging SQLite to allow you to work with just the bits of data you need at a time.

like image 163
CommonsWare Avatar answered Oct 19 '22 22:10

CommonsWare