Hello,
I have a problem with my code for deleting data,
I write a small test app where I add some data to the realmdatabase and measure the needed time for insert.
After this I want to delete the data. First I call my method from RealmDataManager called deleteFahrt(String fahrtname) which should delete all data from the added fahrt. After that I call my RealmDataManager.reset method which should delete all stored data.
If I take a look in the settings after closing the app I can see that there is still some data. But why?
As I see that I tried my app without calling my delete and reset method. Then I saw that the used diskspace in settings is the same as before. So I think my delete and reset method do nothing.
I am using realmversion 2.3.0-SNAPSHOT on a nexus 5x with android 6.0.1 installed.
Because the whole project is to big for adding as attachement I will give you a link where you can download my project.
https://www.dropbox.com/s/7zb88p3m6s46uus/RealmDeleteProblem.zip?dl=0
Can you help me to find where is my mistake?
As I see that I tried my app without calling my delete and reset method. Then I saw that the used diskspace in settings is the same as before
You need to compact the Realm with Realm.compactRealm() to immediately reclaim spaces created by deletion, instead of just letting Realm re-use it for future writes.
@Zhuinden is the compactRealm() that useful? I mean if the instance gets closed at the right place, the alloced space will be reused anyway in the future. Is there any real case to you that you have to call compactRealm() to reclaim the disk space?
@beeender he wanted to delete the data, close the instance, and see immediate shrink in the file size, instead of "letting alloced space be reused anyway in the future".
So if he wants to see immediate shrink in the file size, he should call Realm.compactRealm()
uh ... OK ... @Miger88 Just like @Zhuinden said, if you want to see "immediate shrink" in the file size, you can use Realm.compactRealm(). But please be noticed, normally in a real life app, the space will be reused by Realm in the future write. If you find your db size unexpected grows a lot, that is probably caused by that you are not closing Realm instance in a non-looper thread. (I am NOT saying Realm instance in a looper thread doesn't have to be closed, they should be closed properly as well.)
Hy,
first I want to say thank you for this quick answers.
I changed my code in delete and reset method and add the following line.
Realm.compactRealm(realm.getConfiguration()); before I close the realm database.
After this the used space is still the same. What did I make wrong?
That compactRealm() works only if there are no open Realm instances on any threads.
I think it returns false if it didn't succeed.
Ah. okay. Now I tried the following after realm.close();
boolean compactcheck = Realm.compactRealm(Realm.getDefaultInstance().getConfiguration());
Log.i("RealmDataManager", "compactcheck = " + compactcheck);
This returns false for compactcheck. Any Idea why?
everytime I call realm = Realm.getDefaultInstance(); I call also realm.close at the end of this method.
You probably have an open Realm instance somewhere.
You can check with Realm.getGlobalInstanceCount()
ahh. Problem was that I called Realm.compactRealm(Realm.getDefaultInstance().getConfiguration()); with Realm.getDefaultInstance().getConfiguration().
I changed this to
RealmConfiguration config = realm.getConfiguration();
Realm.compactRealm(config);
this returns true and after that the need diskspace is only 124kB. I think this is not used by realm.
Thank you very much for this good and very quick support.
Most helpful comment
ahh. Problem was that I called Realm.compactRealm(Realm.getDefaultInstance().getConfiguration()); with Realm.getDefaultInstance().getConfiguration().
I changed this to
RealmConfiguration config = realm.getConfiguration();
Realm.compactRealm(config);
this returns true and after that the need diskspace is only 124kB. I think this is not used by realm.
Thank you very much for this good and very quick support.