This question concerns memory in Android.
My method:
I have two activites, A and B. From A, I launch B like this:
Intent i =
Seems that you have a memory leak within that activity.
Probably you are leaking the context (the activity in this case). So ensure that all the references to the context are being cleaned when you call the onDestroy method in the activity. More details here.
Also take a look to possible content observers not being unregistered when you finish the activity.
Is it possible that this 0.08 MB of heap will always be allocated(and not collectable by the >GC) no matter what I do? If not, any idea of what might be causing this?
The 0.08MB of heap, if unused, will be reclaimed when the system considers that it needs it. Garbage Collection does not happen as soon as you call System.gc(), it more like a request for GC to happen as soon as it is possible. There is often a LONG time between an object being allocated and it being removed from memory. The Java Virtual Machine Specification and Java Language Specification specify the lifetime of the object undergoing the following stages:
Note that when an object is Unreachable, it merely becomes a candidate for garbage collection, as and when it may happen. Even with a small footprint JVM like Dalvik, I don't think an object will undergo the full lifetime in a span of time that short. GC is an expensive operation, so is done only as when its needed.
If you have objects that need to be removed from memory quickly (for some reason), then try WeakReference . Or maybe you could provide some more context as to what you're trying to do, and someone could be able to help you out better that way.
For more info on how GC happens in Java, this is a pretty good article
Consider using
android:launchMode="singleTask"
in your AndroidManifest.xml. See this.
What i think is, its a typical java question. And killing an Activity does't mean that its associated objects should be removed from the heap
, even if they are in lost land (their reference are null). because its completely dependent on virtual machine when it calls Garbage collector
(Irrespective of you saying System.GC()
). so when there the condition is like nearly out of memory it calls it and cleans it(cannot be sure again, may be immediately after their reference becomes null
), So i don't think you should be worrying about it.
edited:
call setContentView(getApplicationContext);
and where ever you are this
keyword to pass a context, change it to this.getApplicationContext()
If you have 5 instances of activity B, then you are not managing the activity stack correctly. I find the best way to check it is with the CLI command:
adb shell dumpsys meminfo 'your apps package name'
I had a similar problem in a two activity project when I switched between them. Every time I switched I got a new instance on the stack as revealed by the above command. I then set the flags for the launched activities to FLAG_ACTIVITY_REORDER_TO_FRONT with code like:
Intent i = new Intent("com.you.yourActivityB");
i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(i);
Once I'd done this, then the adb shell command did not show more instances of my two activities when I switched between them