> In the following                                      scenario, the fields of 
> Activity is not released
> when the Activity is closed with calling finish().
> Class MyClass
> {
> }
>
> public MyActivity extends Activity
> {
>     private MyClass obj;
>
>     public void onCreate(xxx)
>     {
>         ........
>         obj = new MyClass();
>     }
> }
>
> when Activity is closed, the field 'obj' is not released.

I am going to assume that by "not released", you mean "not garbage
collected".

> Certainly, we
> can add statement "obj = null" in onDestroy() to release it explicitly,
> but I'm curious that other fields of Activity is not released, is there
> memory leak?

If something in your code is keeping your instance of MyActivity in memory
-- such as holding onto an instance of it or something referring to it in
a static context -- then you will have a memory leak.

> I tried following test:
> 1, Start Activity A
> 2, Start Activity B from A with calling startActivity
> 3, Close Activity B with calling finish();
> 4, Start Activity B in method onResume() of Activity A
> When B is started about 2000 times, Android system report out of memory,
> it seems that memory leaks during the processes, how to resolve this
> issue?

You are creating an infinite loop. Activity A be called with onResume() as
part of Activity B finishing, so you are restarting Activity B right away.
Do not create an infinite loop, and you will not run out of memory.

There are tools in Android to diagnose true memory leaks, such as the
Allocation Tracker and VM Heap in DDMS, or the dump HPROF file button in
DDMS in Android 2.x. Those will help you track down your memory leak.

-- 
Mark Murphy (a Commons Guy)
http://commonsware.com
Android App Developer Books: http://commonsware.com/books.html


-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" 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-developers?hl=en

Reply via email to