android 5.1.1开机优化(framework层)
发布时间:2025-12-09 20:56:00
浏览次数:4
转自:http://blog.csdn.net/xxm282828/article/details/49095839
Android原生系统中对于开机这一块并未做深度的优化,由于领域的限制,这里仅仅对framework中的一部分优化提出来说一下。
一、涉及到的类文件
./base/core/Java/com/android/internal/os/ZygoteInit.java
二、具体修改
主要的思路是加载class文件和resource文件比较多,耗时也多,因此主要从这里开刀。
1)提升process的优先级
[java] view plain copy
public static void main(String argv[]) { try { ...... /* 20151013 optimize android boot begin */ //get the default priority. int defaultPriority = Process.getThreadPriority(Process.myPid()) ; //increase the priority . Process.setThreadPriority(Process.THREAD_PRIORITY_AUDIO) ; registerZygoteSocket(socketName); EventLog.writeEvent(LOG_BOOT_PROGRESS_PRELOAD_START, SystemClock.uptimeMillis()); preload(); EventLog.writeEvent(LOG_BOOT_PROGRESS_PRELOAD_END, SystemClock.uptimeMillis()); // Finish profiling the zygote initialization. SamplingProfilerIntegration.writeZygoteSnapshot(); // Do an initial gc to clean up after startup gc(); Process.setThreadPriority(defaultPriority) ; /* 20151013 optimize android boot end */ ...... } catch (MethodAndArgsCaller caller) { caller.run(); } catch (RuntimeException ex) { Log.e(TAG, "Zygote died with exception", ex); closeServerSocket(); throw ex; } } 2)
[java] view plain copy
static void preload() { Log.d(TAG, "begin preload"); preloadClasses(); /* 20151013 optimize android boot begin */ Thread resThread = new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub // preload resource . long startTime = SystemClock.uptimeMillis() ; preloadResources(); Log.i(":ZygoteInit","preloadResources' time :" + (SystemClock.uptimeMillis()-startTime) + "ms.") ; } }) ; resThread.start(); try { resThread.join(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } /* 20151013 optimize android boot end */ preloadOpenGL(); ...... }
3)
[java] view plain copy
/** * Performs Zygote process initialization. Loads and initializes * commonly used classes. * * Most classes only cause a few hundred bytes to be allocated, but * a few will allocate a dozen Kbytes (in one case, 500+K). */ private static void preloadClasses() { ...... Class.forName(line); /* 20151013 optimize android boot begin */ if (count%128==0 && Debug.getGlobalAllocSize() > PRELOAD_GC_THRESHOLD) { if (false) { Log.v(TAG, " GC at " + Debug.getGlobalAllocSize()); } System.gc(); runtime.runFinalizationSync(); Debug.resetGlobalAllocSize(); } /* 20151013 optimize android boot end */ count++; } ...... }
[java] view plain copy
/* 20151013 optimize android boot begin */ /** when preloading, GC after allocating this many bytes */ //private static final int PRELOAD_GC_THRESHOLD = 50000; private static final int PRELOAD_GC_THRESHOLD = 64 * 1024 * 1024; /* 20151013 optimize android boot end */
其实,可以做的工作还可以有很多,不仅仅是这一点......