您的位置:首页 > 移动开发 > Android开发

从源码看Android中sqlite是怎么读DB的(转)

2015-10-31 11:52 711 查看

执行query

执行SQLiteDatabase类中query系列函数时,只会构造查询信息,不会执行查询。

1  //SQLiteCursor
2
3 super.close();
4 synchronized (this) {
5     mQuery.close();
6     mDriver.cursorClosed();
7 }
8
9
10 //AbstractCursor
11
12 public void close() {
13     mClosed = true;
14     mContentObservable.unregisterAll();
15     onDeactivateOrClose();
16 }
17
18 protected void onDeactivateOrClose() {
19     if (mSelfObserver != null) {
20         mContentResolver.unregisterContentObserver(mSelfObserver);
21         mSelfObserverRegistered = false;
22     }
23     mDataSetObservable.notifyInvalidated();
24 }
25
26
27 //AbstractWindowedCursor
28
29 /** @hide */
30 @Override
31 protected void onDeactivateOrClose() {
32     super.onDeactivateOrClose();
33     closeWindow();
34 }
35
36 protected void closeWindow() {
37     if (mWindow != null) {
38         mWindow.close();
39         mWindow = null;
40     }
41 }
42
43
44
45 //SQLiteClosable
46
47 public void close() {
48     releaseReference();
49 }
50
51 public void releaseReference() {
52     boolean refCountIsZero = false;
53     synchronized(this) {
54         refCountIsZero = --mReferenceCount == 0;
55     }
56     if (refCountIsZero) {
57         onAllReferencesReleased();
58     }
59 }
60
61 //CursorWindow
62
63 @Override
64 protected void onAllReferencesReleased() {
65     dispose();
66 }
67
68 private void dispose() {
69     if (mCloseGuard != null) {
70         mCloseGuard.close();
71     }
72     if (mWindowPtr != 0) {
73         recordClosingOfWindow(mWindowPtr);
74         nativeDispose(mWindowPtr);
75         mWindowPtr = 0;
76     }
77 }


View Code

跟CursorWindow有关的路径里,最终调用nativeDispose()清空cursorWindow;

当Cursor被GC回收时,会调用finalize:



1 @Override
2 protected void finalize() {
3     try {
4         // if the cursor hasn't been closed yet, close it first
5         if (mWindow != null) {
6             if (mStackTrace != null) {
7                 String sql = mQuery.getSql();
8                 int len = sql.length();
9                 StrictMode.onSqliteObjectLeaked(
10                     "Finalizing a Cursor that has not been deactivated or closed. " +
11                     "database = " + mQuery.getDatabase().getLabel() +
12                     ", table = " + mEditTable +
13                     ", query = " + sql.substring(0, (len > 1000) ? 1000 : len),
14                     mStackTrace);
15             }
16             close();
17         }
18     } finally {
19         super.finalize();
20     }
21 }




然而finalize()并没有释放CursorWindow,而super.finalize();里也只是解绑了观察者,没有去释放cursorwindow

所以不调用cursor.close(),最终会导致cursorWindow所在的共享内存(1M或2M)泄露。
http://www.cnblogs.com/hellocwh/p/4924732.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: