android 5.1.1開機優化(framework層)

android原生系統中對於開機這一塊並未做深度的優化,由於領域的限制,這裏僅僅對framework中的一部分優化提出來說一下。

其實,這篇博客是對之前的一篇博客的整合,之前寫的比較亂。

 http://blog.csdn.net/xxm282828/article/details/43066923


一、涉及到的類文件

./base/core/java/com/android/internal/os/ZygoteInit.java


二、具體修改

主要的思路是加載class文件和resource文件比較多,耗時也多,因此主要從這裏開刀。

1)提升process的優先級

 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)

    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)

    /**
     * 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++;
                } 
				......
    }

	/*  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 */


其實,可以做的工作還可以有很多,不僅僅是這一點......


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章