3

I am try to deal with Android memory. The problem is I want to completely free all memory and start a new activity. I found some useful answer:

Android: Clear the back stack

Android: Clear Activity Stack

Clear the entire history stack and start a new activity on Android

After that, I try to make two examples:

The first example:

    Intent intent = new Intent(MainActivity.this, Main2Activity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    startActivity(intent);

I am switching between two activities A and B by using the above code. Here is the picture of memory consumption: enter image description here

The second example:

In MainActivity:

    Intent intent = new Intent(MainActivity.this, Main2Activity.class);
    startActivity(intent);

In Main2Activity:

    onBackPressed();

I am also switching between two activities. The memory consumption of this example: enter image description here

It looks like that Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK is not the prefect way to completely release the memory.

I am really need to know that is there anyway to release all memory like in the second example?

Thank you in advance!

Community
  • 1
  • 1
Luong Truong
  • 1,953
  • 2
  • 27
  • 46
  • Why not just call finish(); after you start your new activity? – Razgriz Jan 11 '16 at 03:00
  • Thank you for your comment, I aim to release memory after starting many activities so that the onBackPressed() or finish() function is not satisfied what I need. I make that example because I want to be sure that Android somehow they can release all memeory, but Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK cannot do this – Luong Truong Jan 12 '16 at 01:01

3 Answers3

1

Add android:noHistory="true" attribute to your <activity> in the AndroidManifest.xml like this:

<activity android:name=".MyActivity"
    android:noHistory="true">
</activity>

set flags

Intent i = new Intent(this, Splash.class);
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    startActivity(i);
    finish();

and also check this link Changing activity in android clears the memory needed for the previous activities?
and also this link https://commonsware.com/blog/2011/10/03/activities-not-destroyed-to-free-heap-space.html

Community
  • 1
  • 1
Mahalakshmi
  • 320
  • 2
  • 13
0

If you're having this issue, you probably have a memory problem that can be solved more gracefully, for example by fixing up memory leaks and maybe reducing the size of your bitmaps. If you don't have any memory leaks, the first Activity should be cleared from memory if the program ever gets close to running out of space in the heap.

But if you really, really need to do exactly this, you can do:

Intent intent = new Intent(MainActivity.this, Main2Activity.class);
startActivity(intent);
System.exit(0);

This will create an undesirable delay when changing the activities, breaks the "standard" use of Android's back button, and could have other negative impacts (for example if you're playing background music), but it will make sure that the memory usage restarts from 0.

kmell96
  • 1,365
  • 1
  • 15
  • 39
  • Thank you, @kmell96! In my first example, there is no images at all. I just have 2 activities A and B. By using the function, I start activity B from A and again I start A from B. Whenever I start a new activity, I set flag Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK but somehow it cannot release all memory like the function onBackPressed() or finish(). – Luong Truong Jan 12 '16 at 01:11
0

Here is what worked for me to free memory after I finish Activity:

in onDestroy() method of your activity add

Runtime.getRuntime().gc();

Tested on Android 5.1.1, 4.4 - works perfect.

Inoy
  • 1,065
  • 1
  • 17
  • 24
  • @LuongTruong there was one more trick - I've reduced size of Bitmap I've used as a background. On exit, I've managed to clear most memory took by activity. – Inoy Jan 16 '17 at 17:17
  • Using gc() is a pretty good indicator of fundamentally broken code. Avoid to use it. You don't know what the garbage collector will do – Pablo Insua Apr 20 '18 at 12:06