-1

In my app, there are some activities doing graphical things with high resolution PNG's. I had OutOfMemoryError errors and fixed it by adding android:largeHeap="true" to my manifest file.

Lenovo Vibe P1 (3GB RAM Total) enter image description here

In memory monitor, second activiy increase 100+ MB of RAM and doesn't decrease altought called finish() function for it. Here is the code for starting third activity:

public void start_game(View v){
    header.setText("Loading...");
    Intent i = new Intent(this,game.class);
    i.putExtra("episode",Integer.parseInt(v.getTag().toString()));
    i.putExtra("online",false);
    startActivity(i);
    finish();
}

How can I completely remove activity from both UI and RAM?

easeccy
  • 4,248
  • 1
  • 21
  • 37
  • First thing `android:largeHeap="true"` is just a bandage and not a real answer to OOM since it does not really address the real OOM issue which is the bad implementation of the code. Second `finish()` does not instantly release everything in that activity, Threads might also still hold that activity reference and does consuming memory. :) – Enzokie Apr 05 '17 at 09:49

2 Answers2

0

Press the 'garbage truck' icon in the memory monitor.
This forces the garbage collector to run and cleanup memory that can be freed.
The system will do this itself when needed, and at some point in the future after the activity is finished but not necessarily right after.

If running the gc does not free up the memory, then there are still references to the data. It could be in for example static variables or singleton objects. You would have to do more analysis of the code to figure out where that could be.

RobCo
  • 6,240
  • 2
  • 19
  • 26
0

For API level 11 and above you can try this to clear the history stack and see if this makes the previous activity release the RAM too.

Intent i = new Intent(this,game.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

And for API => 10 you may use IntentCompact (see this) class, something like

IntentCompat.FLAG_ACTIVITY_CLEAR_TASK

So you can support pre API level 11 as well.

Niraj Niroula
  • 2,376
  • 1
  • 17
  • 36