Thanks.

Even if I called System.gc(), the 'obj' field of MyActivity is still not 
released.

If I don't use infinite loop, and tried the following test, exception 'out of 
memory' still occurs
1, Start Activity A
2, Press a button on Activity A to start Activity B
3, In onCreate method of B, allocate 5MB, then assign it to a field, this field 
is not released explicitly in onDestroy of B
4, Press "Back" key to return to A,
5, Execute System.gc in A, then start B again
6, Repeat steps 4 and 5 about 4 times, exception "out of memory" occurs

--- 10年5月20日,周四, Mark Murphy <mmur...@commonsware.com> 写道:

发件人: Mark Murphy <mmur...@commonsware.com>
主题: Re: [android-developers] When Activity is closed, its fields is not   
release, how to resolve this issue.
收件人: android-developers@googlegroups.com
日期: 2010年5月20日,周四,下午8:17


> 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 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



      

-- 
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