本文主要是介绍android 5.1.1开机优化(framework层),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
转自:http://blog.csdn.net/xxm282828/article/details/49095839
Android原生系统中对于开机这一块并未做深度的优化,由于领域的限制,这里仅仅对framework中的一部分优化提出来说一下。
一、涉及到的类文件
./base/core/Java/com/android/internal/os/ZygoteInit.java
二、具体修改
主要的思路是加载class文件和resource文件比较多,耗时也多,因此主要从这里开刀。
1)提升process的优先级
- public static void main(String argv[]) {
- try {
- ......
-
-
-
- int defaultPriority = Process.getThreadPriority(Process.myPid()) ;
-
- 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());
-
-
- SamplingProfilerIntegration.writeZygoteSnapshot();
-
-
- gc();
-
- Process.setThreadPriority(defaultPriority) ;
-
- ......
- } catch (MethodAndArgsCaller caller) {
- caller.run();
- } catch (RuntimeException ex) {
- Log.e(TAG, "Zygote died with exception", ex);
- closeServerSocket();
- throw ex;
- }
- }
2)
- static void preload() {
- Log.d(TAG, "begin preload");
- preloadClasses();
-
- Thread resThread = new Thread(new Runnable() {
- @Override
- public void run() {
-
-
- long startTime = SystemClock.uptimeMillis() ;
- preloadResources();
-
- Log.i(":ZygoteInit","preloadResources' time :" + (SystemClock.uptimeMillis()-startTime) + "ms.") ;
- }
- }) ;
- resThread.start();
-
- try {
- resThread.join();
- } catch (InterruptedException e) {
-
- e.printStackTrace();
- }
-
- preloadOpenGL();
- ......
- }
3)
-
-
-
-
-
-
-
- private static void preloadClasses() {
-
- ......
-
- Class.forName(line);
-
- if (count%128==0 && Debug.getGlobalAllocSize() > PRELOAD_GC_THRESHOLD) {
- if (false) {
- Log.v(TAG,
- " GC at " + Debug.getGlobalAllocSize());
- }
- System.gc();
- runtime.runFinalizationSync();
- Debug.resetGlobalAllocSize();
- }
-
- count++;
- }
- ......
- }
-
-
-
- private static final int PRELOAD_GC_THRESHOLD = 64 * 1024 * 1024;
-
其实,可以做的工作还可以有很多,不仅仅是这一点......
这篇关于android 5.1.1开机优化(framework层)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!