Hi,

So the problem is quite simple. I start a Service from an Activity,
the user presses back, the Activity goes away and the Service stays
running in the background. However the activity (and it's views) still
stay in memory. Any ideas why is that?? I tested this with cupcake and
G1. To reproduce I've made a simple program.

The activity:

package com.android.service_test;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class StartService extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        startService(new Intent(getApplication(),
RunningService.class));
    }
}

The service:

package com.android.service_test;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

public class RunningService extends Service {

        @Override
        public void onCreate() {
                super.onCreate();
        }

        @Override
        public void onStart(Intent intent, int startId) {
                super.onStart(intent, startId);

                new Thread(new Runnable() {

                        public void run() {
                                while(true) {
                                        System.err.println("running");
                                        try {
                                                Thread.sleep(1000);
                                        } catch (InterruptedException e) {
                                                // TODO Auto-generated catch 
block
                                                e.printStackTrace();
                                        }
                                }
                        }
                }).start();
    }

        @Override
        public IBinder onBind(Intent intent) {
                // TODO Auto-generated method stub
                return null;
        }

}


Manifest:


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android";
      package="com.android.service_test"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/
app_name">
        <activity android:name=".StartService"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category
android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>


        <service android:name=".RunningService" />

    </application>
    <uses-sdk android:minSdkVersion="3" />

</manifest>


With this sample the activity on foreground, meminfo gives

** MEMINFO in pid 633 [com.android.service_test] **
                    native   dalvik    other    total
            size:     2368     2819      N/A     5187
       allocated:     2364     2159      N/A     4523
            free:        3      660      N/A      663
           (Pss):      562      786      928     2276
  (shared dirty):     1132     3940      480     5552
    (priv dirty):      488      556      864     1908

 Objects
           Views:        7        ViewRoots:        1
     AppContexts:        3       Activities:        1
          Assets:        2    AssetManagers:        2
   Local Binders:        6    Proxy Binders:       11
Death Recipients:        0
 OpenSSL Sockets:        0

 SQL
            heap:        0          dbFiles:        0
       numPagers:        0   inactivePageKB:        0
    activePageKB:        0


And after the activity has been destroyed meminfo gives:

** MEMINFO in pid 633 [com.android.service_test] **
                    native   dalvik    other    total
            size:     2380     2819      N/A     5199
       allocated:     2353     2085      N/A     4438
            free:       26      734      N/A      760
           (Pss):      608      929     1065     2602
  (shared dirty):     1088     3852      584     5524
    (priv dirty):      544      712      768     2024

 Objects
           Views:        7        ViewRoots:        0
     AppContexts:        3       Activities:        1
          Assets:        2    AssetManagers:        2
   Local Binders:        5    Proxy Binders:       10
Death Recipients:        0
 OpenSSL Sockets:        0

 SQL
            heap:        0          dbFiles:        0
       numPagers:        0   inactivePageKB:        0
    activePageKB:        0

And I remembered to force the garbage collector to system_process and
to the app process. Any ideas what is happening here??

-Mika

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to