LRC-Editor icon indicating copy to clipboard operation
LRC-Editor copied to clipboard

Activity memory leak caused by anonymous threads

Open cuixiaoyiyi opened this issue 3 years ago • 1 comments

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.

cuixiaoyiyi avatar Aug 29 '22 13:08 cuixiaoyiyi

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.

Spikatrix avatar Aug 30 '22 04:08 Spikatrix