0

Today, I encounter an werid problem about shared data inter-processes. I declare the MainActivity to run in another process, and write the shared data in TestApplication to be 1, and then start the SubActivity to show the shared data. Unfortunately that, the value shown in SubActivity is still 0, as a result, we have the conclusion that there are two TestApplication instance populate in two process, and the read and write of shared data is independent to each other. actually, the shared data does not been shared inter-processes any more. My question is that what is the other difference between an activity start in new process and the orignal one, such as about memory? Here are my codes:

<application
        android:name=".TestApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
<activity android:name=".MainActivity"
            android:process="com.rlk.miaoxinli.hellokitty"
            >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".SubActivity">

        </activity>
    </application>


public class TestApplication extends Application {

    public int mValue = 0;
}


public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView mTextView;
    private TestApplication mApplication;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mApplication = (TestApplication) getApplication();

        setContentView(R.layout.activity_main);
        mTextView = (TextView)findViewById(R.id.first);
        mApplication.mValue = 1;

        mTextView.setClickable(true);
        mTextView.setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mTextView.setText("" + mApplication.mValue);
    }

    @Override
    public void onClick(View view) {
        Intent intent = new Intent(this, SubActivity.class);
        startActivity(intent);
    }
}



public class SubActivity extends AppCompatActivity {

    private TextView mTextView;
    private TestApplication mApplication;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sub);
        mTextView = (TextView) findViewById(R.id.second);;
    }
}
David Wasser
  • 93,459
  • 16
  • 209
  • 274
Saint
  • 1,492
  • 1
  • 11
  • 20
  • Why are you running `MainActivity` in a different process? – David Wasser Sep 28 '17 at 09:43
  • There is a service running in the background in my project, which consume 7M memory, the memory will increase to 50M after start an activity in my project and decrease to 20M after finish rather than the origanl 7M. As a result of which, my solution is start the activity in new process and kill the new process once the activity exit to reclaim the memory. – Saint Sep 28 '17 at 09:56
  • Your solution is bad. You have a memory leak. You need to find out why you leak 13MB of memory. Your solution is a band-aid. It would be better to find and fix the problem. There are plenty of tools to help find memory leaks. – David Wasser Sep 28 '17 at 10:04
  • I have checked the memory usage by MAT, there is no memory leak. 13MB, most of which are occupied by native heap, EGL mtrack and GL mtrack. – Saint Sep 28 '17 at 10:48

1 Answers1

5

If you have 2 activities running in different processes, they share nothing. Android starts 2 different native OS processes. Each process has its own memory, and its own Dalvik VM.

In your manifest, if you provide a custom Application class (which you have done in your example with TestApplication, Android will create an new instance of this class whenever it creates a new OS process for your app. In your case, if you have 2 activities running in 2 separate processes, then you will have 2 instances of TestApplication, one in each OS process.

David Wasser
  • 93,459
  • 16
  • 209
  • 274