Android 如何避免后台音乐被low memory结束生命 M

转载 2013年12月02日 16:10:21

前言
         欢迎大家我分享和推荐好用的代码段~~
声明
         欢迎转载,但请保留文章原始出处:
         CSDN
http://www.csdn.net
         雨季o莫忧离:http://blog.csdn.net/luckkof

正文

 

为防止某些进程被low memory意外杀掉,可以将其加入白名单,降低误伤的概率;
一般来说,low memory killer会首先选择adj value徘徊在9~15的process去结束生命;
 
1.在ActivityManagerService.java 增加如下代码:
static final String[] mThirdPartyAppWhiteList = { "android.process.media"};
static final int [] mThirdPartyAppAdj = {7};
 
2.在ActivityManagerService.java修改如下代码:
  private final boolean updateOomAdjLocked(ProcessRecord app, int hiddenAdj,
            int clientHiddenAdj, int emptyAdj, ProcessRecord TOP_APP, boolean doingAll) {
        app.hiddenAdj = hiddenAdj;
        app.clientHiddenAdj = clientHiddenAdj;
        app.emptyAdj = emptyAdj;
 
        if (app.thread == null) {
            return false;
        }
 
        final boolean wasKeeping = app.keeping;
 
        boolean success = true;
        boolean isWLProc = false; /// M: ALPS00497111 [Volunteer free] LCA Memory Optimized
                   boolean isThirdPartyAppWhiteProcess =  false;     //add
                   int mThirdPartyAdj = ProcessList.HIDDEN_APP_MIN_ADJ;
 
        computeOomAdjLocked(app, hiddenAdj, clientHiddenAdj, emptyAdj, TOP_APP, false, doingAll);
 
        if (app.curRawAdj != app.setRawAdj) {
            if (wasKeeping && !app.keeping) {
                // This app is no longer something we want to keep.  Note
                // its current wake lock time to later know to kill it if
                // it is not behaving well.
                BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics();
                synchronized (stats) {
                    app.lastWakeTime = stats.getProcessWakeTime(app.info.uid,
                            app.pid, SystemClock.elapsedRealtime());
                }
                app.lastCpuTime = app.curCpuTime;
            }
 
            app.setRawAdj = app.curRawAdj;
        }
 
        if (app.curAdj != app.setAdj) {
            /// M: LTK @{
            if (FeatureOption.MTK_MEMORY_COMPRESSION_SUPPORT) {
                if(app.curAdj > app.setAdj && (app.curAdj >= ProcessList.PREVIOUS_APP_ADJ) && app.hasShownUi) {
                    try {
                        IAmsPlus amsplus = MediatekClassFactory.createInstance(IAmsPlus.class);
                        amsplus.afterAdjAdjustment(mMainStack.convertProcessRecord(app),
                                                   app.setAdj,
                                                   app.curAdj,
                                                   mMainStack.convertLaunchRecord(mMainStack.topRunningActivityLocked(null)));
                    } catch (Exception e) {
                        Log.w(TAG, "[LTK] Exception thrown during afterAdjAdjustment failed:", e);
                    }
                }
            }
            /// @}
 
            /// M: ALPS00497111 [Volunteer free] LCA Memory Optimized @{
            if(FeatureOption.MTK_LCA_RAM_OPTIMIZE)
            {
                for(int num = 0; num <= WL_PROCNAME.length -1 ;num++)
                {
                    if(WL_PROCNAME[num].equals(app.processName) &&
                        app.curAdj > ProcessList.HIDDEN_APP_MIN_ADJ)
                    {
                        isWLProc = true;
                        break;
                    }
                }
            }
 
//add 如下的code           
 
                            if(FeatureOption.MTK_LCA_RAM_OPTIMIZE)
            { 
                                if (mThirdPartyAppWhiteList.length != mThirdPartyAppAdj.length)
                                     {
                                               throw new Exception("mThirdPartyAppWhiteList is not match mThirdPartyAppAdj");
                                     }
                                for(int num = 0; num <= mThirdPartyAppWhiteList.length -1 ;num++)
                {
                    if(mThirdPartyAppWhiteList[num].equals(app.processName) &&
                        app.curAdj > mThirdPartyAppAdj[num])
                    {
                        isThirdPartyAppWhiteProcess = true;
                                                        mThirdPartyAdj = mThirdPartyAppAdj[num];                      
                    }
                }
            }
//add code  end
                           
            if(isWLProc)
            {
                if (Process.setOomAdj(app.pid, ProcessList.HIDDEN_APP_MIN_ADJ)) {
                    if (DEBUG_SWITCH || DEBUG_OOM_ADJ) Slog.v(
                        TAG, "Set " + app.pid + " " + app.processName +
                        " adj " + ProcessList.HIDDEN_APP_MIN_ADJ + ": " + app.adjType);
                    app.setAdj = ProcessList.HIDDEN_APP_MIN_ADJ;
                } else {
                    success = false;
                    Slog.w(TAG, "Failed setting oom adj of " + app + " to " + ProcessList.HIDDEN_APP_MIN_ADJ);
                }
            }
//添加如下的判断:
                            else if(isThirdPartyAppWhiteProcess)
                            {
                           
                             if (Process.setOomAdj(app.pid, mThirdPartyAdj)) {
                    if (DEBUG_SWITCH || DEBUG_OOM_ADJ) Slog.v(
                        TAG, "Set " + app.pid + " " + app.processName +
                        " adj " + mThirdPartyAdj + ": " + app.adjType);
                    app.setAdj = mThirdPartyAdj;
                } else {
                    success = false;
                    Slog.w(TAG, "Failed setting oom adj of " + app + " to " + mThirdPartyAdj);
                }                          
                            }                          
            else
            {
                if (Process.setOomAdj(app.pid, app.curAdj)) {
                    if (DEBUG_SWITCH || DEBUG_OOM_ADJ) Slog.v(
                        TAG, "Set " + app.pid + " " + app.processName +
                        " adj " + app.curAdj + ": " + app.adjType);
                    app.setAdj = app.curAdj;
                } else {
                    success = false;
                    Slog.w(TAG, "Failed setting oom adj of " + app + " to " + app.curAdj);
                }
            }
            /// @}
        }
        if (app.setSchedGroup != app.curSchedGroup) {
            app.setSchedGroup = app.curSchedGroup;
            if (DEBUG_SWITCH || DEBUG_OOM_ADJ) Slog.v(TAG,
                    "Setting process group of " + app.processName
                    + " to " + app.curSchedGroup);
            if (app.waitingToKill != null &&
                    app.setSchedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE) {
                Slog.i(TAG, "Killing " + app.toShortString() + ": " + app.waitingToKill);
                EventLog.writeEvent(EventLogTags.AM_KILL, app.userId, app.pid,
                        app.processName, app.setAdj, app.waitingToKill);
                app.killedBackground = true;
                Process.killProcessQuiet(app.pid);
                success = false;
            } else {
                if (true) {
                    long oldId = Binder.clearCallingIdentity();
                    try {
                        Process.setProcessGroup(app.pid, app.curSchedGroup);
                    } catch (Exception e) {
                        Slog.w(TAG, "Failed setting process group of " + app.pid
                                + " to " + app.curSchedGroup);
                        e.printStackTrace();
                    } finally {
                        Binder.restoreCallingIdentity(oldId);
                    }
                } else {
                    if (app.thread != null) {
                        try {
                            app.thread.setSchedulingGroup(app.curSchedGroup);
                        } catch (RemoteException e) {
                        }
                    }
                }
            }
        }
        return success;
    }


举报

相关文章推荐

Android 如何避免后台音乐被low memory结束生命 M

前言          欢迎大家我分享和推荐好用的代码段~~ 声明          欢迎转载,但请保留文章原始出处: ...

android APP从后台重启后如何避免异常

android中,当我们按home键(一般指手机的物理按钮的中间那个键)回到手机桌面,此时app并没有退出,而是一直运行与后台中,此时如果我们点击app图标,还是会进入刚刚的界面之中。但是这存在一个问...

精选:深入理解 Docker 内部原理及网络配置

网络绝对是任何系统的核心,对于容器而言也是如此。Docker 作为目前最火的轻量级容器技术,有很多令人称道的功能,如 Docker 的镜像管理。然而,Docker的网络一直以来都比较薄弱,所以我们有必要深入了解Docker的网络知识,以满足更高的网络需求。

如何避免android ANR

ANR是Application Not Responding的简称,当android某个应用处于长期假死状态时,系统会弹出一个窗口: XXX is not responding, force clos...

Android 如何避免OOM

Android 如何避免OOM

Android如何避免OOM总结

前面介绍了一些基础的内存管理机制以及OOM的基础知识,那么在实践操作当中,有哪些指导性的规则可以参考呢?归纳下来,可以从四个方面着手,首先是减小对象的内存占用,其次是内存对象的重复利用,然后是避免对象...

Android如何避免OOM总结

前面介绍了一些基础的内存管理机制以及OOM的基础知识,那么在实践操作当中,有哪些指导性的规则可以参考呢?归纳下来,可以从四个方面着手,首先是减小对象的内存占用,其次是内存对象的重复利用,然后是避免对象...

Android Low Memory Killer

转自:http://www.cnblogs.com/angeldevil/archive/2013/05/21/3090872.html Low Memory Killer的原理  在Android...

Android Low memory killer

Android在内存管理上与linux有些小的区别。其中一个就是引入了Low memory killer .1,引入原因Android是一个多任务系统,也就是说可以同时运行多个程序,这个大家应该很熟悉...

Android Low memory killer

Android Low memory killer                                   ...

Android开发中如何避免 Out Of Memory(OOM) ?

在android开发中,Process使用的内存是有限的,一旦超出了限定,程序就会跳异常。那这个问题如何避免呢?小编搜罗了一些比较好的方法,希望可以给大家一个借鉴吧。   1、不要Memory L...
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)