Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

好像没有看到realm.close #38

Closed
palmyer opened this issue Dec 15, 2016 · 3 comments
Closed

好像没有看到realm.close #38

palmyer opened this issue Dec 15, 2016 · 3 comments

Comments

@palmyer
Copy link

palmyer commented Dec 15, 2016

realm文档看到说使用完realm后需要close,否则可能内存泄漏
代码中貌似没看到

我的项目中需要删除数据库操作,Realm.deleteRealm(configuration)
总是提示先close,所以关注了下这个问题

@codeestX
Copy link
Owner

@palmyer Hello,按照我个人的理解,目前整个应用共享一个Realm的实例,它是Application级别的,没有单个页面级别的Realm实例,应该不会导致内存泄漏吧,realm.close()这个方法之前的确是没注意到

@palmyer
Copy link
Author

palmyer commented Dec 19, 2016

感谢回复,realmhelper我差不多也是这么写的,
但是用到Realm.deleteRealm(configuration);时必须先close
然后我就这样close一次,然后crash了,错误日志显示我还存在没有关闭的realm
我只能try()catch{}里循环close。。。才能维持不crash,这种方式不太好

private void closeAndDeleteRealm(){
        Realm realm = RealmHelper.getInstance().getRealm();
        RealmConfiguration configuration = realm.getConfiguration();
        try {
            realm.close();
            Realm.deleteRealm(configuration);
        } catch (Exception e) {
            closeAndDeleteRealm();
        }
}
java.lang.IllegalStateException: It's not allowed to delete the file associated with an open Realm. Remember to close() all the instances of the Realm before deleting its file: /data/data/com.codeest.geeknews/files/myRealm.realm

RealmHelper代码

   //Application中执行一次
    public static void createConfiguration() {
        RealmConfiguration config = new RealmConfiguration.Builder()
                .name(RealmHelper.DB_NAME)
                .deleteRealmIfMigrationNeeded()
                .schemaVersion(1)
                .build();
        Realm.setDefaultConfiguration(config);
    }
    public static RealmHelper getInstance() {
        if (instance == null) {
            synchronized (RealmHelper.class) {
                if (instance == null)
                    instance = new RealmHelper();
            }
        }
        return instance;
    }
    public Realm getRealm() {
        if (mRealm == null||mRealm.isClosed()) {
            instanceNum++;
            Logger.e("instanceNum= " + instanceNum);
            mRealm = Realm.getDefaultInstance();
        }
        return mRealm;
    }

然后我在您的代码中设置页面添加了delete的操作也出现了这个crash
再然后加上try()catch{}里加上统计close次数

12-19 15:36:50.667 11879-11879/com.codeest.geeknews E/close_num____: 1
12-19 15:36:50.668 11879-11879/com.codeest.geeknews E/close_num____: 2
12-19 15:36:50.669 11879-11879/com.codeest.geeknews E/close_num____: 3
12-19 15:36:50.669 11879-11879/com.codeest.geeknews E/close_num____: 4

说明有4个没关闭(之前页面随便点点);

每个fragment写单独realm太分散,不知道怎么写优雅点

@codeestX
Copy link
Owner

codeestX commented Jan 3, 2017

@palmyer Hi,刚把应用更新到1.3.0版,把AppComponent不是单例的问题修复了,我在设置页面又测了下这个realm.close()方法,已经可以一次性关闭删除了,没有走到catch里

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants