数据库的内存泄漏检测

当一个应用变得复杂,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)

猜你喜欢

转载自blog.csdn.net/u011414643/article/details/78899700
今日推荐