Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Activity memory leak caused by anonymous threads #35

Open
cuixiaoyiyi opened this issue Aug 29, 2022 · 1 comment
Open

Activity memory leak caused by anonymous threads #35

cuixiaoyiyi opened this issue Aug 29, 2022 · 1 comment

Comments

@cuixiaoyiyi
Copy link

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.

@Spikatrix
Copy link
Owner

Hey @cuixiaoyiyi. Those threads prevent the UI blocking while doing work.

The thread in onCreate reads and processes lyrics from an external LRC file. The thread in readyUpMediaPlayer loads and prepares the music file. These are done asynchronously as they could take time and won't block the main UI thread so I would prefer keeping the threads.

Can you please make a PR fixing the issue? Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants