Fixed-size RealmList and object reuse

55 views
Skip to first unread message

Ben Sandee

unread,
Dec 10, 2015, 12:20:00 AM12/10/15
to Realm Java
I've got a 500 element RealmList as a field in a RealmObject and I'm appending a record every second. At the same time I'm removing the first element. So the list is always 500 items but the items are cycling through in a window of sorts.

I'm curious if there are any theoretical performance differences between the case where the objects get removed from list + db and new ones appended vs. taking the object popped off the front of the list and recycling it (e.g. updating the values and appending it to the back). It would seem that the latter could be more performant, but I'm really not sure if that case is possibly less optimized than the more common "appending new item to the list" case.

Thanks,
Ben

Kenneth Geisshirt

unread,
Dec 10, 2015, 5:21:42 AM12/10/15
to Ben Sandee, realm...@googlegroups.com
Hi Ben,

It's an interesting use case, you bring up. Do you remove the object from your Realm or only from the RealmList? Relationships (RealmLists) are only maintaining references to the target objects, so removing from and adding objects to a RealmList are fairly cheap operations. Removing an object from a Realm isn't a costly operation (but not as cheap as removing an object from a RealmList) but you might get better performance by recycling the object. I imaging recycling will increase the complexity of your code.

So I can provide any hard numbers but only a theoretical answer :-)

Best,
Kenneth

--
Kenneth Geisshirt
Member of technical staff
{#HS:147918450-2437#}
--
You received this message because you are subscribed to the Google Groups "Realm Java" group.
To unsubscribe from this group and stop receiving emails from it, send an email to realm-java+...@googlegroups.com.
To post to this group, send email to realm...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/realm-java/63dc2f57-8cbf-40db-8a96-714583f63a6f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



Ben Sandee

unread,
Dec 10, 2015, 10:47:52 AM12/10/15
to realm...@googlegroups.com
Cool, thanks Kenneth. Yes, I am removing the objects when they fall off the front of the list.

I implemented recycling of the objects as a test and it doesn't seem to have hurt any but without taking time to benchmark it's hard to say if it's improved significantly. To be honest, performance is plenty good so far. I'm mostly trying to keep the blocking/io/gc/churning pressure to a minimum as this task will run constantly. For 500 items it would probably be more efficient to keep the data in memory but we can't afford to lose anything if the app crashes or the OS decides we are not worthy to continue living.

Loosely related:
It's a bit unnerving to see all the explicit GC behavior associated with a realm transaction, particularly one that runs on a tight schedule, constantly:

Explicit concurrent mark sweep GC freed 2607(98KB) AllocSpace objects, 0(0B) LOS objects, 40% free, 23MB/38MB, paused 1.875ms total 31.557ms

But I understand that this is a tradeoff and that by bumping the GC you are saving time later by not updating queries/etc. that are no longer referenced. 

I appreciate the feedback.
Ben

Kenneth Geisshirt

unread,
Dec 11, 2015, 5:15:45 AM12/11/15
to Ben Sandee, realm...@googlegroups.com
Hey Ben,

If you can't afford loosing your objects, Realm is a nice choice. When you return from commitTransaction(), your objects are stored on disk.

At high memory pressure, you will probably see that GC is running often. Depending on how you query your Realm and how "long" your transactions are, you will see different GC patterns.


Best,
Kenneth

--
Kenneth Geisshirt
Member of technical staff
{#HS:147918450-2437#}
Reply all
Reply to author
Forward
0 new messages