skuenstler skuenstler - 1 year ago 106
iOS Question

How does the size of a realm-file develop?

How does the size of a realm-file develop ?

To start with: I have a realm-file with several properties and one of them being an array of 860 entries and each array-entry consists of a couple of properties again.
One array-property states the name of the entry.

I observed the following:

  • If the name-property sais "Criteria_A1" (until "Criteria_A860") - then the realm-file is 1.6 MB big

  • If the name-property sais "A1" (until "A860") - then the realm-file is only 786 kB big

Why is the extra letters in the array-name-property making the realm-file this much bigger ??

A second observation:

  • if I add more objects (each again having an array with 860 entries), then the file size gets 1.6MB big again (no matter how many objects I add; guess until a critical value again where the size tripples...or am I wrong??).

It almost seems to me that the realm-file at 786 kB is doubled in size as soon as something is added (either a property that has more letters or an object that is added). Why does the realm-file double at a critical value and not linearly increase in size with more content added ??

Thanks for a clarification on this.

Answer Source

It's pretty well observed. :-) The Realm file starts out at about 4k and will double in size once it runs out of free space. It keeps doubling until 128M and then adds constantly 128M thereafter.

The reason to double the file and not just grow linearly is only due to performance. It's a common algorithm for dynamic data structures to just keep doubling.

You can use the methods available as seen below to write a compacted copy removing all free space in the file. This can be useful if you don't add new data anymore, want to ship a static database or want to send the file over the network.

Those thresholds and algorithm mentioned are the current ones, and may change in future versions though.

Hope this clarifies?