Removing activity from memory after completion

General topics about the Android-Platform itself.
Coding issues please to the subforum right below.

Removing activity from memory after completion

Postby vol » Wed Apr 16, 2008 2:30 pm

Hello everyone,

When my activity is finished, it remains in memory. Rereading the docs, it seems that this is normal behavior. However, I see some mentions that an empty activity can specify that it wishes to be removed from memory. How can this be done?

The reasoning behind this is that I have several static variables which I wish to start off in the same way every time the app is started. Am I pretty much doomed to running initializer methods in onCreate?
vol
Junior Developer
Junior Developer
 
Posts: 15
Joined: Wed Apr 16, 2008 1:35 pm

Top

Postby larryzou » Sun Apr 20, 2008 12:13 pm

Did you start another thread in your sub-activity? If you did that, all the memory that thread occupied will not be freed. It is a defect, and already submitted to google.
User avatar
larryzou
Freshman
Freshman
 
Posts: 5
Joined: Sun Apr 20, 2008 12:05 pm

Postby vol » Mon Apr 21, 2008 1:48 pm

I've specified a handler for the activity which continuously calls itself (to cause a continuous repaint different from just putting invalidate at the end of onDraw). That's the only thing I can think of. I did not explicitly start a new thread, as far as I know.
vol
Junior Developer
Junior Developer
 
Posts: 15
Joined: Wed Apr 16, 2008 1:35 pm

Postby larryzou » Mon Apr 21, 2008 4:47 pm

vol wrote:I've specified a handler for the activity which continuously calls itself (to cause a continuous repaint different from just putting invalidate at the end of onDraw). That's the only thing I can think of. I did not explicitly start a new thread, as far as I know.

any sample code?
User avatar
larryzou
Freshman
Freshman
 
Posts: 5
Joined: Sun Apr 20, 2008 12:05 pm

Postby vol » Mon Apr 21, 2008 4:55 pm

Use what package name you will.

Code: Select all
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class Counter extends Activity {
   
   private static int counter = 0;
   @Override
   protected void onCreate(Bundle icicle) {
      // TODO Auto-generated method stub
      super.onCreate(icicle);
      int ourCounter = counter++;
      TextView view = new TextView(this);
      view.setText("Counter: " + ourCounter);
      setContentView(view);
   }
   
   
}


Running this the first time results in 0.
Pressing back, then running again results in 1.
Pressing back, then running again results in 2.
etc.

For the sake of brevity, I haven't added any sort of buttons to call to finish(), but the effect would be the same.
vol
Junior Developer
Junior Developer
 
Posts: 15
Joined: Wed Apr 16, 2008 1:35 pm

Postby larryzou » Tue Apr 22, 2008 3:43 pm

nothing sepcial there, any out of memory exception thrown when you repeat the same action for several times?
in fact, the usage of heap size increases is a normal behavior. System will not do GC just when an activity is finished, it will keep your objects in memory if no GC happended.

For my case, it will keep increase (heap size), and system.gc() can't clean those objects, finally it throws out of memory runtime exception.

Did that happen to your appliaction?

vol wrote:Use what package name you will.

Code: Select all
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class Counter extends Activity {
   
   private static int counter = 0;
   @Override
   protected void onCreate(Bundle icicle) {
      // TODO Auto-generated method stub
      super.onCreate(icicle);
      int ourCounter = counter++;
      TextView view = new TextView(this);
      view.setText("Counter: " + ourCounter);
      setContentView(view);
   }
   
   
}


Running this the first time results in 0.
Pressing back, then running again results in 1.
Pressing back, then running again results in 2.
etc.

For the sake of brevity, I haven't added any sort of buttons to call to finish(), but the effect would be the same.
User avatar
larryzou
Freshman
Freshman
 
Posts: 5
Joined: Sun Apr 20, 2008 12:05 pm

Top

Postby vol » Tue Apr 22, 2008 3:48 pm

larryzou wrote:nothing sepcial there, any out of memory exception thrown when you repeat the same action for several times?
in fact, the usage of heap size increases is a normal behavior. System will not do GC just when an activity is finished, it will keep your objects in memory if no GC happended.

For my case, it will keep increase (heap size), and system.gc() can't clean those objects, finally it throws out of memory runtime exception.

Did that happen to your appliaction?


Huh? No. Nothing like that happened. I think we're getting off track here.

I understand that it's normal behavior to keep the classes in memory and such after the app has terminated. I just wonder if it's possible to request that the class be booted from memory (outside of launching a seperate Activity explicitly to run the garbage collector), or more importantly if there's some way to initialize static values back to what their defaults should be when the application is started for the first time. Am I pretty much stuck doing initialization in onCreate and being unable to rely on default static non-final values?
vol
Junior Developer
Junior Developer
 
Posts: 15
Joined: Wed Apr 16, 2008 1:35 pm

Top

Return to General

Who is online

Users browsing this forum: Exabot [Bot] and 5 guests