I have a simple activity which does stuff in a background thread, and
posts the results back to the  UI using a handler.  This activity
leaks memory whenever I flip orientation of the display.  Specifically
it leaks several (15 or more) 32-byte "4-byte array" instances,
according to the DDMS heap view.  In my real activity, if I do all my
work on the actual UI thread (which is of course a horrid thing to do,
given that it can take several seconds), it doesn't leak any memory,
so this seems to be associated with the presence of a handler or of
threads, somehow.  Maybe there's some cleanup I should be doing on the
Handler that I don't know about.

 I constructed a simplified example based on my activity, which I've
pasted in below.  If someone could point out the (probably silly)
mistake I'm making causing it to leak, I would be very grateful.
Thanks in advance,


--Terry.




package com.example.test;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.widget.TextView;

public class NotImplementedScreen extends Activity  {
        @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.notimplemented);

                _tv = (TextView)findViewById(R.id.notimplemented_text);

                startThread();
        }

        public void startThread() {
                _countingThread = new Thread(new Runnable() {
                        public void run() {
                                while(!_shouldExit) {
                                        _number += 1;
                                        _handler.post(updateUi);
                                        try { Thread.sleep(250); } 
catch(InterruptedException ex) {}
                                }
                                _countingThread = null;
                        }
                });
                _countingThread.setDaemon(true);
                _countingThread.start();
        }

        public void onDestroy() {
                _shouldExit = true;
                if(_countingThread != null)
                        _countingThread.stop();
                _countingThread = null;
                super.onDestroy();
        }


        final Handler _handler = new Handler();
        final Runnable updateUi = new Runnable() {
                public void run() {
                        _tv.setText(String.format("Value is %d\n", _number));
                }
        };

        TextView _tv;
        long _number = 0;
        Thread _countingThread;
        boolean _shouldExit = false;
}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/
android"
        android:layout_width="fill_parent"
android:layout_height="fill_parent"
        android:orientation="vertical">

        <TextView android:layout_width="fill_parent"
                android:layout_height="wrap_content" android:text="not 
implemented"
android:id="@+id/notimplemented_text" />
        <Button android:layout_width="wrap_content"
                android:layout_height="wrap_content" android:text="okay"
                android:id="@+id/button_ok" />

</LinearLayout>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Beginners" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to