Giter VIP home page Giter VIP logo

Comments (3)

codeestX avatar codeestX commented on May 17, 2024

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

from geeknews.

palmyer avatar palmyer commented on May 17, 2024

感谢回复,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太分散,不知道怎么写优雅点

from geeknews.

codeestX avatar codeestX commented on May 17, 2024

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

from geeknews.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.