【问题标题】:How to find where my sqlite is leaking in Android如何在 Android 中找到我的 sqlite 泄漏的位置
【发布时间】:2016-07-14 00:23:44
【问题描述】:

当我运行应用程序并执行一些操作时,

数据库“/data/user/0/project/database.db”的 SQLiteConnection 对象被泄露!请修复您的应用程序以正确结束正在进行的事务并在不再需要时关闭数据库。

我如何调试这个

【问题讨论】:

  • 将您的代码发布到您使用数据库的位置。

标签: android sqlite android-debug


【解决方案1】:

自从 GINGERBREAD 以来,ANDROID 引入了一种功能,供开发人员检测可关闭的泄漏对象。

private static final boolean DEVELOPER_MODE = true;

if (DEVELOPER_MODE) {
        StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
                .detectLeakedSqlLiteObjects()
                .detectLeakedClosableObjects()
                .penaltyLog()
                .penaltyDeath()
                .build());
    }

在您想要检测泄漏的可关闭对象的活动中使用此代码块。

希望对你有帮助...

【讨论】:

    【解决方案2】:

    无论何时执行getWritableDatabase()getReadableDatabase(),都必须在处理完成后关闭数据库连接 -

    getWritableDatabase().close();
    

    【讨论】:

      【解决方案3】:

      检查您是否有一些未提交的交易 (一旦开始,您应该始终关闭事务)。 确保关闭光标然后关闭数据库。

      【讨论】:

        【解决方案4】:

        科特林

        var db: SQLiteDatabase? = null
        var cursor: Cursor? = null
        try {
        
           db = getReadWriteDB()
           val query = "Select * From Table"
           cursor = db.rawQuery(query, null)
        
         } catch (e: Exception) {    
           e.printStackTrace()    
         } finally {    
        //     First close the cursor
           if (cursor != null && !cursor.isClosed()) {    
              cursor.close()    
           }
        //     second close the database
        
           if (db != null && db.isOpen) {
               db.close()
        //     Release the database reference
               db.releaseReference()
           }
        }
        

        【讨论】:

          猜你喜欢
          • 2014-08-26
          • 1970-01-01
          • 1970-01-01
          • 2020-08-19
          • 1970-01-01
          • 1970-01-01
          • 2012-05-16
          • 2011-05-19
          • 1970-01-01
          相关资源
          最近更新 更多