android – 150k字文本文件是0.8mb,其中领域db大小是18mb

我在领域db中插入150000个对象. Object只有一个属性是string.

与此同时,我正在为每个字符串创建一个带有换行符的字符串构建器

最后将其写入文本文件.

最后文本文件大小为0.8mb.领域db大小为18mb.它的原因是什么.如何最小化领域db大小.你能告诉我吗?这是领域插入代码

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);
}

最佳答案 对于科学,您可以尝试以下方法:

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);
}
点赞