Reputation: 60061
I have a code with Android Strict Mode turned on, VmPolicy detectAll and Penalty Death. This is turned on in Application.
So it helps me to detect memory leak. My code is very simple, 2 activities (almost blank). The MainActivity have a button click to open SubActivity. The SubActivity just have one EditText.
The code could be obtained here https://github.com/elye/issue_edittextleak
If you run the code, from MainActivity go to SubActivity, and return to MainActivity (using Back Key), and go to SubActivity, and return... It'll crash with
E/StrictMode: class com.elyeproj.edittextleak.SubActivity; instances=2; limit=1
android.os.StrictMode$InstanceCountViolation: class com.elyeproj.edittextleak.SubActivity; instances=2; limit=1
at android.os.StrictMode.setClassInstanceLimit(StrictMode.java:1)
This is because it detected SubActivity has leaked.
Why SubActivity leaked, because it has EditText. Removing it will cause it to not leak anymore.
This happens on Samsung S5 Lollipop 5.0 (v21). It also leaks on KitKat (v19). It doesn't happens on Samsung S7 Marshmallow and Nexus 6 (Nougat).
It doesn't happens on Emulated Nexus 5 Lollipop 5.0.2 (v21). But happens on v19 of Emulated Nexus 5.
I have check on many stackoverflow, and can't find the solution. You'll read of more detail of this issue and my exploration in https://medium.com/@elye.project/hell-level-4-unleashed-by-android-strict-mode-dare-you-challenge-it-1dc9048bb4fb#.aiffbdikn
So what I want? I think the memory leak has been resolved on Lollipop 5.0.2 and beyond. But for version before that, how could I prevent the leak while I could have the editText?
Upvotes: 2
Views: 504
Reputation: 3237
I Think this issue has nothing to do with the edit text, as from the strict mode log SubActivity; instances=2; limit=1
it's clear that you are having two instances of subActivity. This is happening because on each launch of subActivity you are creating a new instance of it, this issue can be solved by using launch mode flags singleInstance
or singleTask
while launching subActivity.Which guarantees that only one instance of activity can exist.
EDIT 1:
I played around with your code, and found that the issue is reproducible on v19 of Emulated Nexus 5. I'm clear that this issue has nothing to do with the edit text as the issue is reproducible even when the subActivity has no views associated with it. As answered in this Stack Oveflow post, this may be a bug in Strict mode as that link points out
If an Activity is started, and exited and restarted very quickly, you can get a StrictMode.InstanceCountViolation.
However this is simply because the garbage collector has not yet finalized the first instance of the Activity, meaning there are temporarily 2 (or more) instances in memory.
Calling System.gc() before startActivity() or startActivityForResult() will stop the StrictMode.InstanceCountViolation
And from the android DOcs
Don't feel compelled to fix everything that StrictMode finds. In particular, many cases of disk access are often necessary during the normal activity lifecycle. Use StrictMode to find things you did by accident. Network requests on the UI thread are almost always a problem, though.
Upvotes: 3
Reputation: 54
Use this code to finish SubActivity .
Add this code in SubActivity Class file.
@Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
Upvotes: 0