一个进程的内存限制是定死的,如果有什么特别耗内存的操作便会OOM,使用多进程可以上我们的apk使用的内存限制变大几倍。
public class MultiProcessTestActivity extends Activity {
/** Called when the activity is first created. */
public static boolean isChecked = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
isChecked = true;
Log.i("MultiProcessTestActivity", isChecked + "");
int pid = android.os.Process.myPid();
String processNameString = "";
ActivityManager mActivityManager = (ActivityManager)this.getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningAppProcessInfo appProcess : mActivityManager.getRunningAppProcesses()) {
if (appProcess.pid == pid) {
processNameString = appProcess.processName;
}
Log.i("MultiProcessTestActivity", processNameString);
}
Intent mIntent = new Intent(this,MultiProcessTest_1_Activity.class);
startActivity(mIntent);
}
}
public class MultiProcessTest_1_Activity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Log.i("MultiProcessTest_1_Activity", MultiProcessTestActivity.isChecked + "");
int pid = android.os.Process.myPid();
String processNameString = "";
ActivityManager mActivityManager = (ActivityManager)this.getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningAppProcessInfo appProcess : mActivityManager.getRunningAppProcesses()) {
if (appProcess.pid == pid) {
processNameString = appProcess.processName;
}
Log.i("MultiProcessTest_1_Activity", processNameString);
}
}
}
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.breeze.multiprocess"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="7" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name">
<activity
android:name=".MultiProcessTestActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MultiProcessTest_1_Activity"
android:label="@string/app_name" >
</application>
</manifest>
logcat打印出来的信息:
11:32:14.973: I/MultiProcessTestActivity(13264): true
11:32:14.989: I/MultiProcessTestActivity(13264): com.breeze.multiprocess
11:32:14.989: I/MultiProcessTestActivity(13264): com.breeze.multiprocess
11:32:15.153: I/ActivityManager(1275): Starting activity: Intent { cmp=com.breeze.multiprocess/.MultiProcessTest_1_Activity }
11:32:15.169: I/WindowManager(1275): adjustConfigurationLw fontScale=1.0
11:32:15.215: I/MultiProcessTest_1_Activity(13264): true
11:32:15.340: I/MultiProcessTest_1_Activity(13264): com.breeze.multiprocess
在这里先是通过Process.myPid()获取进程的进程ID号,再通过ActivityManager.RunningAppProcessInfo来遍历所有运行中的Activity,对比ID来获取所需要的进程名(通过ID获取当前运行的进程名)。我们可以看到,两个Activity是在同一个Process中:com.breeze.multiprocess 即包名,同时两者所打出的MultiProcessTestActivity中的静态布尔变量也都是true,因为在第一个Activity中设置的是true。
接着我们对manifest进行修改,在后面一个acticity中添加一句
android:process = "com.breeze.MultiProcess.test"
再来看所打出来的日志
15:25:25.223: I/MultiProcessTestActivity(13893): true
15:25:25.239: I/MultiProcessTestActivity(13893): com.breeze.multiprocess
15:25:25.239: I/MultiProcessTestActivity(13893): com.breeze.multiprocess
15:25:25.239: I/MultiProcessTestActivity(13893): com.breeze.multiprocess
15:25:25.255: I/ActivityManager(1275): Starting activity: Intent { cmp=com.breeze.multiprocess/.MultiProcessTest_1_Activity }
15:25:25.262: I/ActivityManager(1275): Start proc com.breeze.MultiProcess.test for activity com.breeze.multiprocess/.MultiProcessTest_1_Activity: pid=13907 uid=10098 gids={}
15:25:25.450: D/ddm-heap(13907): Got feature list request
15:25:25.630: I/WindowManager(1275): adjustConfigurationLw fontScale=1.0
15:25:25.872: I/MultiProcessTest_1_Activity(13907): false
15:25:25.895: I/MultiProcessTest_1_Activity(13907): com.breeze.MultiProcess.test
两个Activity没有在同一进程中,并且后者打出来的布尔量是false。很明显,两个进程之间的内存相互是透明的,不同的进程是不同的虚拟机实例,相互之间互相透明,所以这个时候就需要在application中去对不同的进程来做不同的初始化工作和配合调度工作了。
每个进程都是运行在不同的虚拟机上,进程间内存的具有不可见性。对于不同的进程,他们载入的Class文件虽然名字一样(比如都是com.breeze.multiprocess.MultiProcessTestActivity),但是他们其实是加载到了不同的内存地址空间。com.breeze.multiprocess.MultiProcessTestActivity中把isChecked变量设置为true,它其实只是把当前进程(com.breeze.multiprocess)的MultiProcessTestActivity类的isChecked变量的设置为了true,com.breeze.MultiProcess.test进程中的 com.breeze.MultiProcess.test.MultiProcessTest_1_Activity类和它位于不同的内存地址空间,当然其变量isChecked也位于不同的内存地址空间,自然也不受影响。