Skip to content

Instantly share code, notes, and snippets.

@anitaa1990
Created May 16, 2018 17:11
Show Gist options
  • Save anitaa1990/b85e2118479011f0775a5df27ef7b0e8 to your computer and use it in GitHub Desktop.
Save anitaa1990/b85e2118479011f0775a5df27ef7b0e8 to your computer and use it in GitHub Desktop.
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