当一个应用变得复杂,SQLite使用得频繁,就容易出现数据库泄漏 leaked:

A SQLiteConnection object for database '0033.db' was leaked! Please fix your application to end transactions in progress properly and to close the database when it is no longer needed.

当代码过多,怎么排查呢?

一个方法是,使用android.os.StrictMode。

从 GINGERBREAD 开始 Android 就提供了 StrictMode 工具协助开发人员检查是否不小心地做了一些不该有的操作。使用方法是在 Activity 里面设置 StrictMode,下面的例子是打开了检查泄漏的 SQLite 对象以及 Closeable 对象(普通 Cursor/FileInputStream 等)的功能,发现有违规情况则记录 log 并使程序强行退出。数据库的内存泄漏检测

数据库的内存泄漏检测

报错的 log 很详尽,仔细检查就可以一步步慢慢排查。蓝色标记处就是泄漏处的代码

数据库的内存泄漏检测数据库的内存泄漏检测E/StrictMode: A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks.
                                                            java.lang.Throwable: Explicit termination method 'close' not called
                                                                at dalvik.system.CloseGuard.open(CloseGuard.java:184)
                                                                at android.database.sqlite.SQLiteConnection.<init>(SQLiteConnection.java:175)
                                                                at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:195)
                                                                at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
                                                                at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
                                                                at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
                                                                at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:836)
                                                                at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:821)
                                                                at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:714)
                                                                at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:1261)
                                                                at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:268)
                                                                at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:223)
                                                                at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
                                                           
    at com.dao.WaybillDao.<init>(WaybillDao.java:21)
                                                                at com.ui.activity.ReceiveService.doOther(ReceiveService.java:395)
                                                                at com.ui.activity.ReceiveService$1.run(ReceiveService.java:73)
                                                                at java.lang.Thread.run(Thread.java:818)

相关文章:

  • 2021-12-12
  • 2021-09-26
  • 2021-09-13
  • 2022-02-04
  • 2022-01-08
  • 2021-07-25
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-12-22
  • 2022-02-19
  • 2022-01-24
  • 2022-12-23
  • 2022-12-23
  • 2021-05-02
相关资源
相似解决方案