Comparison of the Binary Preferences library with standard Shared Preferences

By | 31.07.2017

In the Android system, key-value pairs are used to store information. With their help, you can store the values of variables, or various information that the application may need in the future.

And although the SDK already has a ready interface, called SharedPreferences, which provides methods for saving and reading settings in the application, there are various libraries that implement the task in other ways. One such library is Binary Preferences.

This library is a quick and easy replacement for SharedPreferences. Its feature is that it works with disks via an asynchronous NIO (Non-blocking I/O) and stores each key-value pair in a separate file. In addition, the library has the following advantages:

  • Light, without unnecessary dependences;
  • Faster than SharedPreferences;
  • Small amount of occupied memory during serialization/deserialization of data;
  • No copy of the memory area;
  • Contains only binary data, not XML or JSON;
  • Supports data encryption;
  • Full backward compatibility with the SharedPreferences interface;
  • Keeps any primitives;
  • Fully optimized support for interprocess communication (IPC);
  • Handling of various exceptions.

More information about this library can be found on the repository page.

Compare the work of SharedPreferences and Binary Preferences, for this we write an application that performs the same operations first by standard means, and then using the library.

To do this, create a SharedPreferences object and add variables of different types to it and save it.

SharedPreferences prefs = getSharedPreferences("shared_preferences", MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("test_string", "Test message");
editor.putBoolean("test_boolean", true);
editor.putInt("test_int", 2536);
editor.putFloat("test_float", (float) 1000.53);
editor.apply();

Run the application and write the data.

Now let’s see how much space the data on the device took, it will require root access to get to the /data/data/androidtools.pef_test folder, which is located at the root of the system. More information on how to get root can be found here.

Inside this folder is the shared_prefs folder, where our data is stored. Data is stored in an XML file whose name is identical to the first parameter getSharedPreferences() (in this case – shared_preferences.xml).

As you can see from the screenshot, the data occupy 284 bytes in memory.

Let’s compare now with the work of Binary Preferences. Because the interface is the successor of SharedPreferences and SharedPreferences.Editor, the differences in the code will be minimal.

The main emphasis in the library is on storage of complex objects consisting of many fields through the Persistable interface.

Thus, small overheads are achieved for serialization/deserialization of data, i.e. there is no transfer to xml and json, which can be critical in some projects.

Preferences prefs = new BinaryPreferencesBuilder(getApplicationContext()).build();
PreferencesEditor editor = prefs.edit();
editor.putString("test_string", "Test message");
editor.putBoolean("test_boolean", true);
editor.putInt("test_int", 2536);
editor.putFloat("test_float", (float) 1000.53);
editor.apply();

Similarly, run the application and write the data.

Let’s look at the files, only this time we need a folder not shared_prefs, but files. The values inside it are stored in preferences/pref_name/values.

As mentioned in the features of the library, each key-value pair is in a separate file, and in total they occupy only 48 bytes.

From here we can conclude that the more data you need to store the application, the more will be the gain from the device’s memory.

In addition, you can measure the size of the APK of both versions, for this we need the utility APK Analyzer, integrated into Android Studio. Comparison of the resulting APK can be seen in the table below.

Shared Preferences Binary Preferences
Uncompressed size 803,7 KB 894,9 KB
Compressed size 613 KB 626 KB
How many classes are there 684 755
How many methods are there 5611 5944

The number of methods in the Binary Preferences itself can be found here http://www.methodscount.com/?lib=com.github.iamironz%3Abinaryprefs%3A0.9.9.9

Standard Android SDK methods can not always perform their tasks efficiently, and then user libraries come to the rescue, which can work faster and more efficiently, improving the overall performance of the application.

Leave a Reply

Your email address will not be published. Required fields are marked *