关闭并重新打开Realm实例对性能有害吗? [英] Is closing and reopening Realm instances bad for performance?

查看:108
本文介绍了关闭并重新打开Realm实例对性能有害吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用SQLite时,我通常每个应用程序都有一个SQLiteOpenHelper实例,我永远不会关闭它,因为它的数据库被许多其他类连续使用,关闭/重新打开它会更慢,更复杂。

When using SQLite I usually have a single SQLiteOpenHelper instance per application and I never ever close it, since its database is used continuously by many other classes and closing/reopening it would be slower and more complicated.

现在我正在玩Realm,我打算只从Data Access Objects访问Realm实例。每个调用都将来自一个工作线程。

Now I'm toying with Realm and I'm planning to access Realm instances only from Data Access Objects. Every call will be made from a worker thread.

我一直在阅读这些示例,他们通常会根据每个Activity或后台任务调用getInstance / close。由于Realm将数据保存在像SQLite这样的文件中,为每个操作调用getInstance / close是个好主意吗?我的意思是,调用关闭实际关闭文件连接,从而使下一个 getInstance 调用更慢?我应该在应用程序级别缓存一个Realm实例并将其注入到DAO中吗?

I've been reading the examples and they usually call getInstance/close per Activity or background task. Since Realm persists the data in a file like SQLite, is it a good idea to call getInstance/close for each operation? I mean, would calling close actually close the file connection and thus make the next getInstance call slower? Should I cache a Realm instance at application level and inject it into the DAOs instead?

推荐答案

Realm使用引用计数的线程本地缓存+优化的模式验证。这意味着只要你在调用 Realm.getInstance()的线程上至少打开一个实例,它就只是一个HashMap查找。

Realm uses a reference counted thread local cache + optimized schema validation. It means that as long as you have at least one instance open on a thread calling Realm.getInstance() it is just a HashMap lookup.

如果你在任何线程上打开了一个实例,我们就会跳过其他线程上的模式验证,即使它是在那里打开的第一个实例。

If you have one instance open on any thread, we skip schema validation on other threads even though it is the first instance opened there.

如果关闭给定线程上的所有实例,我们将释放线程本地内存,并且需要为该线程上的下一个实例重新分配它。

If you close all instances on a given Thread we will free the thread local memory and it will need to be reallocated for the next instance on that thread.

如果你关闭所有线程上的所有实例,你将有一个冷启动,这是最昂贵的,因为我们需要分配内存+进行模式验证。

If you close all instances on all threads you will have a "cold boot" which is the most expensive as we need to allocate memory + do a schema validation.

最佳做法是在您的线程存在时保持Realm实例处于打开状态。对于使用此处描述的模式最简单的UI线程: https://realm.io/docs/java/latest/#controlling-the-lifecycle-of-realm-instances

Best practice is to keep the Realm instance open for as long as your thread lives. For the UI thread that is easiest done using the pattern described here: https://realm.io/docs/java/latest/#controlling-the-lifecycle-of-realm-instances

对于工人在开始时打开Realm实例并在退出时关闭它的线程将是最优的:

For worker threads opening the Realm instance at the beginning and closing it when exiting would be the most optimal:

new Thread(new Runnable() {
  public void run() {
    Realm realm = Realm.getDefaultInstance();
    doWork(realm);
    realm.close();
  }
}).start();

这篇关于关闭并重新打开Realm实例对性能有害吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆