问题
I am adding some data into realm database after every 15 sec through a service.
After a whole night, the size of app become 350mb due to realm, its confirm..
But if i delete that data from realm, after some condition, the data deleted but the size of app still shown 350mb.
The question is why the app size is not shrinking now.
回答1:
Realm currently doesn't automatically reclaim the space used by your database, but it will be reused if you later add data again.
If you wish to free the disk space you can use Realm.compactRealm(): https://realm.io/docs/java/latest/api/io/realm/Realm.html#compactRealm-io.realm.RealmConfiguration-
However, a file size of 350 MB sounds like quite a lot. Are you really inserting that much data?
回答2:
Realm realm = null;
RealmConfiguration config = new RealmConfiguration.Builder(MyApplication.appInstance().getApplicationContext())
.deleteRealmIfMigrationNeeded()
.build();
try {
realm = Realm.getInstance(config);
} catch (Exception e) {
}
RealmResults<MqttLocationModel> result2 = realm.where(MqttLocationModel.class).equalTo("uid", objectId).findAll();
Log.e("Delted object id",""+objectId);
Log.e("Delted DB Size",""+result2.size());
realm.beginTransaction();
result2.clear();
realm.commitTransaction();
RealmResults<MqttLocationModel> result3 = realm.where(MqttLocationModel.class).equalTo("uid", objectId).findAll();
Log.e("Delted final DB Size",""+result3.size());
realm.close();
realm.compactRealm(config);
回答3:
Your service executes on a background thread, so you probably have a single realm open on a BACKGROUND THREAD for the whole duration of the sync procedure, thus creating multiple versions of the Realm.
If this is true, you should either close and reopen the Realm after a transaction, or use waitForChange ().
来源:https://stackoverflow.com/questions/37453353/app-size-increase-due-to-realm-android