Slow binary data writing on an old Android device

34 Views Asked by At

I wonder if anyone could offer any suggestions to improve the performance of binary data writing on an old enterprise Android 5.1 device.

FileOutputStream fileOutputStream = new FileOutputStream(fileDescriptor);
BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);
byte[] abData = new byte[1024*1024];
long lStart = System.currentTimeMillis();
for(int i= 0; i < 30; i++){
    bos.write(abData);
}
long lDuration = System.currentTimeMillis() - lStart;
utility.logDebug("OEDebug: duration (MS): " + lDuration);

The execution of the above testing code to write 30MB data takes about 1.2 seconds. If FileOutputStream is used directly instead of the BufferedOutputStream, the outcome is virtually the same. We deal with files with sizes of hundreds of MB. Writing them takes well over 10 seconds each. Is there anything that I can try to improve the speed?

1

There are 1 best solutions below

0
Aditya Dube On

Improving the performance of binary data writing on an old Android 5.1 device can be challenging due to hardware limitations and the age of the operating system. However, there are several strategies you can try to optimize the writing process:

  1. Increase Buffer Size: The default buffer size might not be optimal for your use case. Try increasing the size of the buffer in BufferedOutputStream to see if it improves performance. Experiment with different sizes to find the best fit.
  2. Use Direct I/O: If your device supports it, using direct I/O can bypass the OS cache and write directly to disk. This can be faster for large files, though it may not be supported on all devices or file systems.
  3. Optimize Disk Access Patterns: Writing large chunks of data at once can be more efficient than writing many smaller chunks. If possible, try to organize your data so that you can write large contiguous blocks.
  4. Avoid Frequent Disk Writes: If your application logic allows, accumulate data in memory and write it in bulk to the disk less frequently. This can reduce the overhead associated with disk I/O operations.
FileOutputStream fileOutputStream = null;
BufferedOutputStream bos = null;

try {
    fileOutputStream = new FileOutputStream(fileDescriptor);
    
    // Increased buffer size for performance improvement
    int bufferSize = 8 * 1024 * 1024; // 8 MB
    bos = new BufferedOutputStream(fileOutputStream, bufferSize);
    
    byte[] abData = new byte[1024 * 1024]; // 1 MB
    long lStart = System.currentTimeMillis();

    for (int i = 0; i < 30; i++) {
        bos.write(abData);
    }

    long lDuration = System.currentTimeMillis() - lStart;
    utility.logDebug("OEDebug: duration (MS): " + lDuration);
} catch (IOException e) {
    // Handle exceptions appropriately
    e.printStackTrace();
} finally {
    try {
        if (bos != null) {
            bos.close();
        }
        if (fileOutputStream != null) {
            fileOutputStream.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Notes:

  1. Buffer Size: I've increased the buffer size to 8 MB. You should experiment with this value to find the optimal size for your specific use case.
  2. Exception Handling: The code now includes proper exception handling and ensures that the streams are closed in the finally block.
  3. Asynchronous Writing: To implement asynchronous writing, you would need to move the writing logic to a separate thread. This can be done using AsyncTask, Thread, or other concurrency tools in Android, but it's more complex and depends on the overall structure of your application.