Comparison of official schemes

SharedPreference

SP, for short, stores raw type data in key-value pairs, which are stored in XML files by default

  • Application scenario: The storage capacity is small and simple
  • Advantages and disadvantages: It has its own memory-level cache, which is fast to read when the data volume is small, but it is not safe for cross-process. When the data volume is large, it is slow to load, and full write is easy to cause ANR

SQLite

  • Application scenario: Complex data type and large data volume, tens of thousands of levels
  • Advantages and disadvantages: Compared with SP, it has advantages when there is a large amount of data. Data acquisition is slow, and transaction batch processing is required for a large number of writes, which has high performance. If the performance of frequent single insertion is not high, direct SQL operation is cumbersome, orM framework can be considered

File

  • Application scenario: As the name implies, it is suitable for the storage of file types, such as pictures or some simple text data or binary data
  • Advantages and disadvantages: the most basic storage method, it does not store the content of any formatting, all data is intact in the file

summary

As we currently use SP plan, our weaknesses, performance is poor, and the ANR question, consider a data storage solution, first of all our data storage scenario is not complex data types, quantity is not too big, so the priority is MMKV, in order to more clearly understand the advantages and disadvantages of MMKV, so contrast under reference

Comparison of Open Source Solutions

Brief introduction of various schemes

The name of the MMKV Realm WCDB Room
plan mmap nosql SQLCipher SQLite
version 1.0.24 6.0.2 1.0.8 2.2.3
size 0.15 MB 1.5 MB 0.7 MB 0.05 MB
The migration Easy migration from SP Migration from SQLite is easy
scenario Replace the SP Replace the SQLite Replace the SQLite Replace the SQLite

MMKV contains the lib_c++shared library, which is 0.3 MB

The performance comparison

Initialize the

Initialize the MMKV SP Realm Room WCDB
Time consuming 30ms 4ms 60ms 15ms 80ms
memory 1mb About 1 MB About 2 MB 1mb 2mb
Peak CPU 16% 20% 18% 15% 15%

Compare read/write speed and data file size

SP Number of executions Write to take Read the time The data file
string 200 51ms 4ms 8kb
string 1000 171ms 6ms 42kb
Java entities 1000 220ms 87ms 172kb
MMKV Number of executions Write to take Read the time The data file
string 200 8ms 5ms 4kb
string 1000 20ms 9ms 16kb
Java entities 1000 117ms 93ms 128kb
Realm Number of executions Write to take Read the time The data file
string 200 229ms 74ms 32kb
string 1000 606ms 122ms 288kb
Java entities 1000 760ms 130ms 576kb
Room Number of executions Write to take Read the time The data file
string 200 636ms 160ms 32kb
string 1000 1350ms 475ms 48kb
Java entities 1000 1298ms 432ms 68kb
wcdb Number of executions Write to take Read the time The data file
string 200 636ms 160ms 32kb
string 1000 1350ms 475ms 48kb
Java entities 1000 1298ms 432ms 68kb

Memory consumption

Memory consumption MMKV SP
Initialization + Query 1K items 1.7 MB 1.2 MB
Initialization + query 1w items 3.6 MB 2.8 MB
Initialization + query 5w items 10.7 MB (8 MB file) 8.5 MB file (8.6 MB)

Index remark

String data: 10 bytes containing a random number Running mobile phone: Huawei Mate10 Thread of execution: sub-thread Statistical method: Average three times Memory consumption: The memory increment after the business code reads the corresponding data and releases the local object. MMKV, SP read and write Java entity object data time includes entity class serialization and deserialization time, but due to the cache, the deserialization time is slightly smaller

Test arrangement

  • Initialization performance, CPU peak and memory growth are almost the same. In terms of time consumption, SP and Room are both native schemes of the system, so the time consumption is relatively short.
  • MMKV and SP added a new item of memory consumption, mainly because they have a memory-level cache and need to verify the impact on memory. According to the test data, the memory growth of these two schemes is similar and acceptable.
  • MMKV useprotobufStorage, SP usexml.protobufCompared with thexmlIn terms of performance, the volume will also decrease. However, in terms of real storage space, the expansion mechanism of MMKV (when the space is insufficient, it will apply for double space) may lead to the actual use of larger space. Compared with SP, the storage space occupied by SP is the real data size.

Summary of Open Source Solution

MMKV

MMKV provides a memory block that can be written at any time through mMAP memory mapping file. App only writes data into it, and the operating system is responsible for writing the memory back to the file. There is no need to worry about data loss caused by crash. SP and MMKV is a cross-platform solution, write performance is greatly improved compared with SP, suitable for the need for a universal key-value storage component, cross-process access requirements, especially for the current SP storage solution of students who are dissatisfied, MMKV also supports SP migration support, basically do not need to adjust too much code. Migration matters are detailed below.

Realm

Realm is a mobile NoSQL framework that is officially positioned as a replacement for relational databases such as SQLite, cross-platform, high-performance, and visual viewing tools. Shortcomings are also obvious, both the size of the integration package and the data file are a bit large. In addition, Realm requires that the current Bean object must inherit RealmObject directly, which is very intrusive. There are thread limits and data objects cannot be used across threads. In addition, I found a pit when writing the test demo. If one thread queries the list, another thread adds or deletes it, the local data files will be backed up, resulting in the rapid growth of database files, which will reach hundreds of MB, and will not be automatically cleaned. There is also an official explanation for this:

WCDB

WCDB is an efficient, complete and easy-to-use mobile database framework, based on SQLCipher, supporting iOS, macOS and Android

Room

Room is not a database. It provides an abstraction layer on top of SQLiteo, allowing users to take advantage of SQLite’s power while enjoying a more robust database access mechanism. The flexible interface adaptation layer is retained. Currently WCDB supports access to Room: github.com/Tencent/wcd…

ObjectBox

If you want to try ObjectBox, you can try ObjectBox, which is another masterpiece by GreenDao. It is said that its performance is better than that of Realm. Since this is the selection and replacement of SP storage solution in the old project, we will not compare ObjectBox for now. Devlabs. Bg /realm-objec… Chejdj. Making. IO/android / 201…

review

To sum up, sp and MMKV have their own memory level cache, so the query is very fast, but if the amount of data is too large, it will cause memory overflow, MMKV official also explained this, do not exceed 100MB, in fact, for normal data, certainly can not reach this limit, if more than 30MB of data, The above test data is not fair to the storage of the database type. It mainly applies to the scenario where a single query is frequently inserted. In the database scenario, multiple data are written in batches through transactions and a type of data set is also queried. In summary, the current implementation of using MMKV to replace SP has great benefits. It has advantages in performance and cross-process, and avoids the ANR problem of SP, and does not increase the volume much. For database scenarios, it is recommended to use Room, or try the objectBox described above but not compared in this test. Feel to pay attention to a lot of, but scattered sorting only so much…

MMKV migration reference

  • Migrating official documents: github.com/Tencent/MMK…

  • MMKV does not support Serializable Java objects, but you can consider converting Serializable Java objects to byte stores in MMKV

  • The initial storage space of MMKV is 4KB, and if it is insufficient, the capacity should be doubled at least. In addition, for efficiency, the disk storage space will not be reduced when the key value is removed. Therefore, the kv-trim method is officially provided to reduce the disk storage space. The timing is tricky, but it feels like a regular cleanup

  • SP# getAll SP# registerOnSharedPreferenceChangeListener method is not supported, called a runtime exception is thrown, need to pay attention to. GetAll does not support it because it erases the type. It feels that this is a relatively weak point of MMKV, which also increases the cost of migrating other storage schemes in the future

  • Some Android devices (API Level 19) may fail to install/update APK, causing libmmkv.so not to be found. Then I will meet Java. Lang. UnsatisfiedLinkError crash. There’s an open source library called ReLinker that addresses this, but there aren’t many apps that still support 19

  • For history project SP data may be too large, so directly using the official mmkv# importFromSharedPreferences methods directly on a file transfer, will take too long problem, the current Demo test data: For the test model Huawei Mate10, it takes about 80ms for a 4MB SP file to be migrated to MMKV, which should be longer online. Therefore, in order to avoid the experience problem of initial use, here are two solutions: 1. Unified migration before use, and give users a hint of data migration; 2. In the first use of the background silent migration. At present, we are using the second way of thinking. In this case, we need to ensure the accuracy of the data. If you are interested, you can leave a message to discuss

reference

Various official and online comparison test articles

  • Github.com/Tencent/MMK…
  • Github.com/Tencent/wcd…
  • Github.com/realm/realm…
  • Dxinl. Making. IO / 2018/03/29 /…
  • Andyken. Making. IO / 2017/10/26 /…
  • Juejin. Cn/post / 684490…
  • www.jianshu.com/p/f001acf3d…
  • Juejin. Cn/post / 684490…
  • Juejin. Cn/post / 684490…