chegwin
chegwin

Reputation: 99

Speed Issue Deserializing Arraylist Android

I have an ArrayList allFeeds and a method to read in serialized data.

@SuppressWarnings("unchecked")
public void loadFile() {

    final String FILENAME = "feeds.ser";
    File file = fileContext.getApplicationContext().getFileStreamPath(FILENAME);

    try {           
        FileInputStream fis = new FileInputStream(file);
        ObjectInputStream ois = new ObjectInputStream(fis);
        allFeeds = (ArrayList<Feed>) ois.readObject();
    }
    catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    catch (IOException e) {
        e.printStackTrace();
    }
    catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
} 

The serialized data is stored in a 0.49mb size file, made up of 33 Feed objects. The structure for a Feed object is as follows, with the HashMap holding up to 900 key-pair values:

int id;
String name;
int datatype; 
String tag;
Long time;
Double value;
String dpInterval;  
HashMap<Long, Double> historicFeedData;

It takes a considerable amount of time to load the data back into the arraylist (average time is 13.5 seconds). GC_CONCURRENT repeatedly frees up memory during the execution.

Is there a way of increasing the speed of this operation, or is it simply just the amount of data it's processing? If so, would it be advisable to push this into an aSyncTask or background service?

Upvotes: 0

Views: 155

Answers (2)

chegwin
chegwin

Reputation: 99

As Tone suggested, I modified the code to include a BufferedInputStream and passed it to the ObjectInputStream:

BufferedInputStream bis = new BufferedInputStream(fis);
ObjectInputStream ois = new ObjectInputStream(bis);

This has reduced the load time dramatically to a little over 3 seconds.

Upvotes: 1

Tone
Tone

Reputation: 26

Buffer the inputReader. Should save 75%ish.

Upvotes: 1

Related Questions