详细分析Android中实现Zygote的源码

2020-01-06 13:36:52丽君

preloadClasses看起来很简单,但是实际上它有很多的类需要加载。可以查看一下/frameworks/base/preloaded-classes文件,这里面都是需要预加载的类。

接下来,分析一下preloadResources函数的源码:

 

 
  1. private static final boolean PRELOAD_RESOURCES = true;  private static void preloadResources() 
  2. {  final VMRuntime runtime = VMRuntime.getRuntime(); 
  3. Debug.startAllocCounting();   
  4. try {  System.gc(); 
  5. runtime.runFinalizationSync();  mResources = Resources.getSystem(); 
  6. mResources.startPreloading();  if (PRELOAD_RESOURCES) { 
  7. Log.i(TAG, "Preloading resources...");   
  8. long startTime = SystemClock.uptimeMillis();  TypedArray ar = mResources.obtainTypedArray(com.android.internal.R.array.preloaded_drawables); 
  9. int N = preloadDrawables(runtime, ar);  ar.recycle(); 
  10. Log.i(TAG, "...preloaded " + N + " resources in " + (SystemClock.uptimeMillis()-startTime) + "ms.");   
  11. startTime = SystemClock.uptimeMillis();  ar = mResources.obtainTypedArray(com.android.internal.R.array.preloaded_color_state_lists); 
  12. N = preloadColorstateLists(runtime, ar);  ar.recycle(); 
  13. Log.i(TAG, "...preloaded " + N + " resources in " + (SystemClock.uptimeMillis() - startTime) + "ms.");  } 
  14. mResources.finishPreloading();  } catch (RuntimeException e) { 
  15. Log.w(TAG, "Failure preloading resources", e);  } finally { 
  16. Debug.stopAllocCounting();  }