一眠蚂、使用嚴(yán)格模式:
StrictMode嚴(yán)苛模式砌滞,是Android提供的一種運(yùn)行時(shí)檢測(cè)機(jī)制调炬,一般用來檢測(cè)在主線程做一些耗時(shí)動(dòng)作唯鸭,比如IO讀寫派草、數(shù)據(jù)庫操作搀缠、Sp操作、Activity泄露近迁、未關(guān)閉的Closable對(duì)象泄露等艺普,以減少發(fā)生ANR等。嚴(yán)格模式主要有2個(gè)策略:一個(gè)是線程策略鉴竭,即TreadPolicy是針對(duì)一個(gè)具體的線程歧譬,另一個(gè)是VM策略,即VmPolicy搏存,是針對(duì)虛擬機(jī)的所有對(duì)象瑰步。
當(dāng)然可以添加Penalty(懲罰),一般我們都是用來打印日志璧眠、繪制紅框等缩焦。
二读虏、嚴(yán)格模式中是如何去檢測(cè),在哪個(gè)地方有注入StrictMode的代碼:
在源碼中可以直接植入StrictMode的檢查代碼袁滥、也可以植入代碼是BlockGuard和CloseGuard的代碼盖桥。
我們先看下UML:
1、BlockGuard:
public final class StrictMode {
public static void setThreadPolicy(final ThreadPolicy policy) {
setThreadPolicyMask(policy.mask);
}
private static void setThreadPolicyMask(final int policyMask) {
setBlockGuardPolicy(policyMask);
Binder.setThreadStrictModePolicy(policyMask);
}
// Sets the policy in Dalvik/libcore (BlockGuard)
private static void setBlockGuardPolicy(final int policyMask) {
if (policyMask == 0) {
BlockGuard.setThreadPolicy(BlockGuard.LAX_POLICY);
return;
}
final BlockGuard.Policy policy = BlockGuard.getThreadPolicy();
final AndroidBlockGuardPolicy androidPolicy;
if (policy instanceof AndroidBlockGuardPolicy) {
androidPolicy = (AndroidBlockGuardPolicy) policy;
} else {
androidPolicy = threadAndroidPolicy.get();
BlockGuard.setThreadPolicy(androidPolicy);
}
androidPolicy.setPolicyMask(policyMask);
}
}
其中在setBlockGuardPolicy(policyMask)把相應(yīng)的AndroidBlockGuardPolicy設(shè)置到BlockGuard類题翻,
并且Binder.setThreadStrictModePolicy()是Native層揩徊,當(dāng)進(jìn)程A發(fā)起跨進(jìn)程調(diào)用進(jìn)入到進(jìn)程B后,進(jìn)程B中的違規(guī)異常也是會(huì)通過Binder再傳回進(jìn)程A中嵌赠。
2塑荒、CloseGuard:
public final class StrictMode {
public static void setVmPolicy(final VmPolicy policy) {
synchronized (StrictMode.class) {
sVmPolicy = policy;
sVmPolicyMask = policy.mask;
setCloseGuardEnabled(vmClosableObjectLeaksEnabled());
...
}
}
private static void setCloseGuardEnabled(boolean enabled) {
if (!(CloseGuard.getReporter() instanceof AndroidCloseGuardReporter)) {
CloseGuard.setReporter(new AndroidCloseGuardReporter());
}
CloseGuard.setEnabled(enabled);
}
}
函數(shù)setCloseGuardEnabled注入AndroidCloseGuardReporter后,在Android很多代碼都會(huì)使用CloseGuard.get()獲取全局的CloseGuard進(jìn)行檢測(cè)輸出打印日志出來猾普。
三袜炕、植入嚴(yán)格模式檢測(cè)的代碼:
1)、FileInPutStream中:讀寫file時(shí)初家,有BlockGuard檢測(cè)UI線程操作文件偎窘,有CloseGuard檢測(cè)報(bào)告的未關(guān)閉文件等。
class FileInputStream extends InputStream{
private final CloseGuard guard = CloseGuard.get();
public FileInputStream(File file) throws FileNotFoundException {
...
BlockGuard.getThreadPolicy().onReadFromDisk();
open(name);
guard.open("close");
}
public void close() throws IOException {
...
guard.close();
...
}
protected void finalize() throws IOException {
if (guard != null) {
guard.warnIfOpen();
}
if ((fd != null) && (fd != FileDescriptor.in)) {
close();
}
}
}
2)溜在、SQLiteCursor :在SQLiteCursor對(duì)象銷毀時(shí)陌知,會(huì)對(duì)Cursor是否關(guān)閉進(jìn)行判斷相應(yīng)向StrictMode報(bào)告
public class SQLiteCursor extends AbstractWindowedCursor {
public SQLiteCursor(SQLiteCursorDriver driver, String editTable, SQLiteQuery query) {
...
if (StrictMode.vmSqliteObjectLeaksEnabled()) {
mStackTrace = new DatabaseObjectNotClosedException().fillInStackTrace();
} else {
mStackTrace = null;
}
...
}
protected void finalize() {
try {
if (mWindow != null) {
if (mStackTrace != null) {
String sql = mQuery.getSql();
int len = sql.length();
StrictMode.onSqliteObjectLeaked(
"Finalizing a Cursor that has not been deactivated or closed. " +
"database = " + mQuery.getDatabase().getLabel() +
", table = " + mEditTable +
", query = " + sql.substring(0, (len > 1000) ? 1000 : len),
mStackTrace);
}
close();
}
} finally {
super.finalize();
}
}
}
3)、Activity泄露掖肋,在ActivityThread中Activity的啟動(dòng)和銷毀都有植入StrictMode相應(yīng)的代碼去檢測(cè)Activity的引用計(jì)數(shù)仆葡,來判斷是否有泄露,在StrictMode.decrementExpectedActivityCount(activityClass);會(huì)手動(dòng)System.gc();
public final class ActivityThread {
private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
...
Activity activity = null;
try {
java.lang.ClassLoader cl = r.packageInfo.getClassLoader();
activity = mInstrumentation.newActivity(
cl, component.getClassName(), r.intent);
StrictMode.incrementExpectedActivityCount(activity.getClass());
...
} catch (Exception e) {
}
...
return activity;
}
private ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing,
int configChanges, boolean getNonConfigInstance) {
...
try {
r.activity.mCalled = false;
mInstrumentation.callActivityOnDestroy(r.activity);
...
} catch (Exception e) {
}
}
StrictMode.decrementExpectedActivityCount(activityClass);
return r;
}
}
4)志笼、SharedPreferencesImpl 中g(shù)etString沿盅、getInt等也植入了BlockGuard
final class SharedPreferencesImpl implements SharedPreferences {
@Nullable
public String getString(String key, @Nullable String defValue) {
synchronized (this) {
awaitLoadedLocked();
...
}
}
@Nullable
public Set<String> getStringSet(String key, @Nullable Set<String> defValues) {
synchronized (this) {
awaitLoadedLocked();
...
}
}
public int getInt(String key, int defValue) {
synchronized (this) {
awaitLoadedLocked();
...
}
}
private void awaitLoadedLocked() {
if (!mLoaded) {
// Raise an explicit StrictMode onReadFromDisk for this
// thread, since the real read will be in a different
// thread and otherwise ignored by StrictMode.
BlockGuard.getThreadPolicy().onReadFromDisk();
}
while (!mLoaded) {
try {
wait();
} catch (InterruptedException unused) {
}
}
}
}