The text file of text words 150k is 0.8mb, where realm db size is 18mb

I am inserting 150,000 objects in realm db. An object has only one property, which is a string. At the same time, I create a string builder with a new line for each line and finally write it to a text file.

At the end, the text file size is 0.8mb. Where the size of the db area is 18 mb. What is the reason for this. How to minimize the size of the db area. Could you help me. Here is the area insert code

private void insertWord() {
    long time = System.currentTimeMillis();
    StringBuilder builder=new StringBuilder();

    RealmConf conf = RealmConf.getInstance(true);
    int i = 0;
    RealmUtils.startTransaction(conf);


    while (i < 150000) {
        i++;
        String word = "Word:" + i;
        EB eb = new EB(word);
        builder.append(word+"\n");

        RealmUtils.saveWord(eb, conf);
        Log.i("word check" + i++, "seelog:" + word);
    }
    RealmUtils.commitTransaction(conf);
    writeStringIntoFile(builder.toString(),0);
}
+4
source share
2 answers

You can try the following: for science:

private void insertWord() {
    long time = System.currentTimeMillis();
    StringBuilder builder=new StringBuilder();

    RealmConf conf = RealmConf.getInstance(true);
    int i = 0;
    int batchCount = 0;


    while (i < 150000) {
        if(batchCount == 0) {
            RealmUtils.startTransaction(conf);
        }
        batchCount++
        i++;
        String word = "Word:" + i;
        EB eb = new EB(word);
        builder.append(word+"\n");

        RealmUtils.saveWord(eb, conf);
        Log.i("word check" + i++, "seelog:" + word);
        if(batchCount == 3000) {
            RealmUtils.commitTransaction(conf);
            batchCount = 0;
        }
    }
    if(batchCount != 0) {
        RealmUtils.commitTransaction(conf);
    }
    writeStringIntoFile(builder.toString(),0);
}
+1
source

Perhaps because you forgot to call Realm.close(). See this document for more details.

https://realm.io/docs/java/latest/#faq

. , Realm , SQLite, , , Realm . , Realm , .

Realm , , . .

, . .

, :

1) .

, Realm Realm . , . . , Realm Logcat. , , .

2) , .

Realm , . , , Realm, .

0

Source: https://habr.com/ru/post/1693622/


All Articles