32 static WeakReference<GcWatcher> sGcWatcher = new WeakReference<GcWatcher>(new GcWatcher());
34 static ArrayList<Runnable> sGcWatchers = new ArrayList<>();
38 static final class GcWatcher {
39 @Override
40 protected void finalize() throws Throwable {
41 handleGc();
42 sLastGcTime = SystemClock.uptimeMillis();
43 synchronized (sGcWatchers) {
44 sTmpWatchers = sGcWatchers.toArray(sTmpWatchers);
45 }
46 for (int i=0; i<sTmpWatchers.length; i++) {
47 if (sTmpWatchers[i] != null) {
48 sTmpWatchers[i].run();
49 }
50 }
51 sGcWatcher = new WeakReference<GcWatcher>(new GcWatcher());
52 }
53 }
複製程式碼
Java世界: Binder -> Parcel.writeStrongBinder -> android_os_Binder -> ibinderForJavaObject(env, obj) -> (Binder.Object<->JavaBBinderHolder.get()<->JavaBBinder)
JavaBBInder分析(BinderProxy較簡單略過):
1.在java中new Binder時其建構函式init在native執行時,建立了一個JavaBBinderHolder,並放到了Binder.mObject域中;
2.JavaBBinderHolder如名字所描述,只是一個快取,當不需要起內部快取的JavaBBinder時,是不會去構造它的;
3.JavaBBinder構造的時機,是在上層應用在Parcel.java呼叫writeStrongBinder時構造的。
由上面三點可以分析得出:Java上層構建了Binder之後,如果要將Binder序列化傳遞,在native底層中會相應的構建一個JavaBBinderHolder內部包含一個繼承於BBinder的JavaBBinder,並放到Binder.mObject當中。 因此Java層的Binder,在native中間層的表述是JavaBBinder,底層最終還是BBinder。