Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Realm large file size

I am new to android programming. I implemented Realm for my simple grade tracker, however, the file size has grown from 1.5mb to 5mb. Is that normal?

What I simply did was add in an instance of realm on every class with oncreate, added in realm.close() whenever there was a chance that the activity would end. Also wrapped the begin and commit transaction around each object creation.

Is there something I did wrong that ended up with the massive file size?

Thanks!

Edit: I mean apk size

like image 944
Petersicecream Avatar asked Dec 11 '15 06:12

Petersicecream


People also ask

Where is the realm file stored android?

When you open the local path in Finder, where you saved it, you can tap into that by selecting “Show Package Contents” in the context menu of the finder, when you select the file. A new finder window will open, where you find your Realm inside in the following path (e.g.): AppData/Documents/default.

Is realm DB free?

The realm is also open source since 2016 and it is free for developers.

What is realm Nosql database in Android?

Realm Database is a service which is provided by MongoDb which is used to store data in users device locally. With the help of this data can be stored easily in users' devices and can be accessed easily. We can use this database to store data in the user's device itself.


2 Answers

@geisshirt is right. Realm have introduced the method public static boolean compactRealm(RealmConfiguration configuration) to compact the realm database. Calling this method reduced the size of my database from +350MB to <1MB

Documentation available here: https://realm.io/docs/java/latest/api/io/realm/Realm.html#compactRealm-io.realm.RealmConfiguration-

like image 87
tobalr Avatar answered Sep 22 '22 08:09

tobalr


I've researched&tried everything, so don't waste your time. There are 2 possible solutions:

1) Realm.compact(RealmConfiguration): You need to close all connections to Realm database that you have opened, from activities or where ever you created it. Then you should call Realm.compact(yourDb.getConfiguration()). If every connection is closed before this compact operation, result will be true, and database will be smaller, else it won't resize it, and result will be false.

2) my kind of compactDb: Second solution is based on to do some kind of "swap mechanism" that maybe will have slower performance(at some large files, counted in milisecond), but definitely will solve problem of large files and leave only necessary size of realm to your database.

I created my personal function "compactDb()" that will: 1. take my db(realm), 2. copy it into some new File called default-compact.realm 3. delete my db(realm) 4. copy-back file default-compact.realm to my db(new File with extension realm) 5. delete default-compact.realm since we don't need them anymore

This "copy thing" will remove older versions of database and copy only last one(the real size of your database). Since next step is to delete your current db, then you will get rid of those files... :)

Here is my code:

    public void compactDb(String dbName){
    try{
        //move compacted db to new one...
        Realm db = Realm.getInstance(getConfig(dbName));
        File compactedFile = new File(db.getConfiguration().getRealmDirectory(), "default-compacted.realm");
        compactedFile.delete();
        db.writeCopyTo(compactedFile);
        db.close();

        Realm compactedDb = Realm.getInstance(getConfig("default-compacted.realm"));
        File dbFile = new File(compactedDb.getConfiguration().getRealmDirectory(), dbName);
        dbFile.delete();
        compactedDb.writeCopyTo(dbFile);
        compactedDb.close();
        compactedFile.delete();

    }catch (Exception e){
        e.printStackTrace();
    }
}

helper function that will give me desired configuration that I use through code:

private RealmConfiguration getConfig(String name) {
    return new RealmConfiguration.Builder()
            .name(name)
            .schemaVersion(0)
            .build();
}

Usage of this function is:

compactDb("default.realm");

and your Realm database will be resized to real size... Have a happy coding :)

like image 39
Elvis Rudonja Avatar answered Sep 20 '22 08:09

Elvis Rudonja