Created
May 16, 2018 17:11
-
-
Save anitaa1990/b85e2118479011f0775a5df27ef7b0e8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ThreadReferenceLeakActivity extends AppCompatActivity { | |
/* | |
* Mistake Number 1: Do not use static variables | |
* */ | |
private static LeakyThread thread; | |
@Override | |
protected void onCreate(@Nullable Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_first); | |
createThread(); | |
redirectToNewScreen(); | |
} | |
private void createThread() { | |
thread = new LeakyThread(); | |
thread.start(); | |
} | |
private void redirectToNewScreen() { | |
startActivity(new Intent(this, SecondActivity.class)); | |
} | |
/* | |
* Mistake Number 2: Non-static anonymous classes hold an | |
* implicit reference to their enclosing class. | |
* */ | |
private class LeakyThread extends Thread { | |
@Override | |
public void run() { | |
while (true) { | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment