Skip to content

Activity memory leak caused by anonymous threads #35

@cuixiaoyiyi

Description

@cuixiaoyiyi

An anonymous inner class will hold a reference to the this pointer of the outer class and will not be released until the thread ends.

It will hold the Activity and prevent its timely release.

com.cg.lrceditor.EditorActivity: onCreate(...)
com.cg.lrceditor.EditorActivity: readyUpMediaPlayer(...)
Is the anonymous inner class thread necessary?

new Thread(() -> {
       runOnUiThread(() -> swipeRefreshLayout.setRefreshing(true));
      if (r.getErrorMsg() != null || !r.readLyrics()) {
		runOnUiThread(() -> { ... }
       }
     runOnUiThread(...);
}

new Thread(() -> {
     runOnUiThread(...);
}

com.cg.lrceditor.HomePage: void onResume()

If it is necessary, it can be changed to static class + weak reference to eliminate the reference to the activity, which may cause memory leaks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions