2

我有一些绑定到某个进程的服务的 Android 应用程序。

我发现adb shell top -n 1返回:

PID PR CPU% S  #THR     VSS     RSS PCY UID      Name
31647  0   1% S    70 1733640K  98960K  bg u0_a132  com.my.app.dev
31727  0   1% S    29 1523892K  62924K  fg u0_a132  com.my.app.dev:myService

即使我的应用程序保持在后台,为什么还要top PCY告诉'fg'ae 前台?

有人可以传播这个问题吗?

这是我的 Manifest.xml:

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
          package="com.my.app.dev" 
          android:versionName="4.0.1.6700000"
          android:versionCode="5033" >
    
    <uses-sdk android:minSdkVersion="10"
              android:targetSdkVersion="16" />
    
    
    
    
        
    <application android:icon="@drawable/icon" 
                 android:label="@string/config_app_name" 
                 android:theme="@style/Theme.NoActionBarAppCompat" 
                 android:name="com.my.app.Mine" > 
                 
        <!-- we would prefer the launchMode to be standard, but it causes a lot of problems. -->
        <activity android:name="com.my.ui.main.MineApp" 
                  android:label="@string/config_app_name"
                  android:launchMode="singleTask" 
                  android:clearTaskOnLaunch="true" 
                  android:theme="@style/Theme.Translucent.NoActionBarAppCompat" >

            <intent-filter>      
                <category android:name="android.intent.category.DEFAULT" />
                <action android:name="mine.action.HomeActivity" />
                
                <category android:name="android.intent.category.LAUNCHER" />
                <action android:name="android.intent.action.MAIN" />
            </intent-filter>
        </activity>
        
        
        
        <meta-data android:name="com.google.android.maps.v2.API_KEY"
                   android:value="xxxxxx" />
        
        <meta-data android:name="com.google.android.gms.version" 
                   android:value="@integer/google_play_services_version" />
        
        
        
        <uses-library android:name="com.google.android.maps" /> 
        
        
    
        
        <service android:name="com.my.engine.logic.EngineService" 
                 android:process=":myService">
            <intent-filter>
                
                <action android:name="com_my_RemoteService" />
                <action android:name="com_my_EngineService" />
            </intent-filter>
        </service>
        
        

        
                
        <receiver 
          android:name="com.my.engine.analytics.ReferrerReceiver" 
          android:exported="true" android:process=":myService">
            <intent-filter>
                <action 
              android:name="com.android.vending.INSTALL_REFERRER"/>
            </intent-filter>
        </receiver>
        
        <receiver 
              android:name="com.my.engine.BootBroadcastReceiver" 
              android:process=":myService">
            <intent-filter>
                <action 
android:name="android.intent.action.BOOT_COMPLETED"/>
            </intent-filter>
        </receiver>

        <receiver 
             android:name="com.my.engine.util.MineWatcherReceiver" 
             android:process=":myServiceWatcher">
            <intent-filter>
                <action
       android:name="android.intent.action.ACTION_POWER_CONNECTED" />
                <action   
       android:name="android.intent.action.BATTERY_OKAY" />
                <action 
       android:name="android.net.conn.CONNECTIVITY_CHANGE" />
                <action 
       android:name="android.intent.action.USER_PRESENT" />
            </intent-filter>
        </receiver>
    
        <receiver 
           android:name="com.my.engine.logic.InitServiceReceiver" 
           android:exported="true" android:process=":myService">
            <intent-filter>
                <action android:name="initServiceCheck"/>
            </intent-filter>
        </receiver>

        <receiver 
            android:name="com.my.engine.logic.UpdateCounterReceiver" 
            android:exported="true" android:process=":myService">
            <intent-filter>
                <action android:name="updateCustomCounter"/>
            </intent-filter>
        </receiver>
        
        <receiver 
         android:name="com.my.engine.logic.PackageChangeReceiver" 
         android:process=":myService">
            <intent-filter>
                <action  
              android:name="android.intent.action.PACKAGE_ADDED"/>
                <action 
              android:name="android.intent.action.PACKAGE_REPLACED"/>
                <action 
              android:name="android.intent.action.PACKAGE_REMOVED"/>
                <action 
              android:name="android.intent.action.PACKAGE_INSTALL"/>
                <data android:scheme="package"/>
            </intent-filter>
        </receiver>
        
        <!-- for notification try next action -->
        <service android:name="com.my.notifications.actions.TryNextNotificationActionService" />
        
        <receiver android:name="com.my.ui.base.EulaReminderReceiver">
            <intent-filter>
                <action android:name="eulaReminderAction" />         
            </intent-filter>
        </receiver>
        
        <receiver android:name="com.my.ui.base.MineGuiBroadcastReceiver">
            <intent-filter>
                <action android:name="finishStoppedActivities" /> 
            </intent-filter>
        </receiver>
        
        <service android:name="com.my.infra.motion.ActivityRecognitionService"
                 android:label="ActivityRecognitionService"
                 android:exported="true"
                 android:enabled="true"
                 android:process=":myService">
        </service>
        
    </application>
    
</manifest> 

编辑 1

我什至停止了“设置”中的通知但仍在process=":myService前台

编辑 2

来源:_

if (p == SP_BACKGROUND)
        strcpy(proc->policy, "bg");
    else if (p == SP_FOREGROUND)
        strcpy(proc->policy, "fg");
    else
        strcpy(proc->policy, "er");

来自 其他问题的答案

对于 PCY 来说,大部分是未受过教育的,有点随机的,在黑暗中刺伤——

PCY -- Policy -- 确定应用程序应如何被 Android 的内存管理器处理

FG -- Foreground -- 进程被认为是前台进程,不应被杀死以释放内存

BG -- Background -- 进程被认为是后台进程(不在前台主动运行,可能会被杀死以释放内存)

4

2 回答 2

2

PCY代表scheduling policyfg在该列中表示该进程的优先级高于bg. 这并不意味着该进程正在前台运行。

于 2015-09-21T18:06:10.010 回答
2

详细说明 Alex P. 的回答:

我相信 PCY 列是指cgroup进程分配到的。Android 定义了两个cgroup组,SP_FOREGROUND并且SP_BACKGROUND. 的实际cgroup名称SP_BACKGROUNDbg_non_interactive。这些组分别top用缩写fg和表示bg

您可以在整个框架中找到对这些的引用,尤其是在 中Process.java,以及与 Linux文件系统接口以管理运行进程的各个方面的IPCThreadState.cpp本机代码。根据这些文件中的源代码注释,似乎前台的所有线程都以“正常”的 CPU 份额进行调度,而后台的线程则以“减少”的份额进行调度。android_util_Process.cpp/proccgroupcgroup

至于正常和减少的定义,这篇博客指出SP_BACKGROUND线程被限制在 5% 的 CPU 使用率。/dev/cpuctl/bg_non_interactive/cpu.shares您可以通过查看正在运行的设备来确认这一点。在运行 AOSP 5.1 的 Nexus 5 上,我得到:

root@hammerhead:/ # cat /dev/cpuctl/bg_non_interactive/cpu.shares              
52
root@hammerhead:/ #

此处,52 是指 中线程允许的“CPU 份额”数量,cgroup最多 1024 个份额。因此,在这种情况下bg_non_interactive,确实允许组中所有线程的最大 CPU 使用率总计约为 5%。

无论如何,很明显,这个上下文中的前台和后台与 Android 的Activity 生命周期以及前台和后台应用程序的想法几乎没有关系。这只是 Android 利用 Linuxcgroups功能的方式。

于 2015-09-21T19:25:24.060 回答