Skip to content

Modified FlautoRecorderEngine.java to re-use buffers and avoid GC churn#17

Open
781flyingdutchman wants to merge 6 commits intoCanardoux:masterfrom
781flyingdutchman:master
Open

Modified FlautoRecorderEngine.java to re-use buffers and avoid GC churn#17
781flyingdutchman wants to merge 6 commits intoCanardoux:masterfrom
781flyingdutchman:master

Conversation

@781flyingdutchman
Copy link

Hi, really appreciate your flutter_sound package, but per this issue when using the toStream option it generates massive Garbage Collection churn on Android, see this log:

2026-01-25 01:19:47.746 27156-27156                 I  Waiting for a blocking GC Alloc
2026-01-25 01:19:47.762 27156-27161                 I  Background young concurrent mark compact GC freed 104MB AllocSpace bytes, 0(0B) LOS objects, 0% free, 255MB/255MB, paused 413us,1.215ms total 302.740ms
2026-01-25 01:19:47.762 27156-27185                 I  WaitForGcToComplete blocked ProfileSaver on Background for 236.711ms
2026-01-25 01:19:47.762 27156-27156                 I  WaitForGcToComplete blocked Alloc on Background for 16.343ms
2026-01-25 01:19:47.762 27156-27156                 I  Starting a blocking GC Alloc
2026-01-25 01:19:47.815 27156-27156                 I  Alloc concurrent mark compact GC freed 250MB AllocSpace bytes, 0(0B) LOS objects, 81% free, 5448KB/29MB, paused 132us,1.471ms total 52.776ms
2026-01-25 01:19:47.815 27156-27161                 I  WaitForGcToComplete blocked Background on Alloc for 49.116ms
2026-01-25 01:19:48.455 27156-27156                 I  Waiting for a blocking GC Alloc
2026-01-25 01:19:48.498 27156-27161                 I  Background concurrent mark compact GC freed 250MB AllocSpace bytes, 0(0B) LOS objects, 81% free, 5460KB/29MB, paused 153us,1.360ms total 199.971ms
2026-01-25 01:19:48.498 27156-27156                 I  WaitForGcToComplete blocked Alloc on Background for 42.261ms

That is 604MB collected, causing 107ms of blocked Alloc in less than 1 second (and it goes on forever).

The reason is that in FlautoRecorderEngine.java you allocate buffers within a tight loop. I did not have time to look into this in detail, but this PR modifies FlautoRecorderEngine.java to use a re-usable buffer that does not have to be allocated frequently, and as a result the blocking GC Alloc calls are gone completely.

Because flutter_sound depends on this repo it is hard to make the change there, but perhaps you can adopt the changes in this PR (or something similar) so that the flutter_sound package becomes much more performant on Android.

Thanks!

@Haotian9850
Copy link

+1 to this issue. Frequent GC causes app jitter and (now that flutter has merged the platform and UI threads) it's even more of a performance issue

@781flyingdutchman
Copy link
Author

Following the initial commit to this PR I have made further changes to address runtime errors in both Android and iOS code bases, that caused app crashes for various reasons. Still not 100% convinced all nullpointer issues in iOS are fixed, but I do think it is getting better

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants