mikeperri / flutter_sequencer

A Flutter plugin for music sequencing.
Other
91 stars 39 forks source link

Memory leaks when loading large font files #3

Open razvangeangu opened 4 years ago

razvangeangu commented 4 years ago

It looks like on hard refresh android has a fatal error.

Restarted application in 2,259ms.
I/FLUTTER_SEQUENCER(20261): Successfully set asset manager in native module
I/OboeAudio(20261): openStream() OUTPUT -------- OboeVersion1.4.3 --------
I/AAudio  (20261): AAudioStreamBuilder_openStream() called ----------------------------------------
I/AudioStreamBuilder(20261): rate   =  44100, channels  = 2, format   = 5, sharing = SH, dir = OUTPUT
I/AudioStreamBuilder(20261): device =      0, sessionId = -1, perfMode = 12, callback: ON with frames = 0
I/AudioStreamBuilder(20261): usage  =      1, contentType = 2, inputPreset = 6, allowedCapturePolicy = 0
D/        (20261): PlayerBase::PlayerBase()
D/AudioStreamTrack(20261): open(), request notificationFrames = -8, frameCount = 0
I/AudioTrack(20261): createTrack_l(0): AUDIO_OUTPUT_FLAG_FAST successful; frameCount 0 -> 5288
W/AudioStreamTrack(20261): open() flags changed from 0x00000104 to 0x00000004
I/AAudio  (20261): AAudioStreamBuilder_openStream() returns 0 = AAUDIO_OK for s#2 ----------------
D/OboeAudio(20261): AudioStreamAAudio.open() format=2, sampleRate=44100, capacity = 5288
D/OboeAudio(20261): AudioStreamAAudio.open: AAudioStream_Open() returned AAUDIO_OK
D/AAudio  (20261): AAudioStream_close(s#1) called ---------------
D/AudioTrack(20261): stop(49): called with 141431526 frames delivered
D/AAudio  (20261): AAudioStream_close(s#1) returned 0 ---------
D/AAudio  (20261): AAudioStream_requestStart(s#2) called --------------
D/        (20261): PlayerBase::start() from IPlayer
D/AudioStreamLegacy(20261): onAudioDeviceUpdate() devId 2 => 2
D/AAudio  (20261): AAudioStream_requestStart(s#2) returned 0 ---------
I/FLUTTER_SEQUENCER(20261): Loading sample assets/wav/D3.wav
I/FLUTTER_SEQUENCER(20261): Loading sample assets/wav/F3.wav
I/FLUTTER_SEQUENCER(20261): Loading sample assets/wav/G#3.wav
I/FLUTTER_SEQUENCER(20261): Loading sample assets/sfz/samples/Mp-F3.wv
I/FLUTTER_SEQUENCER(20261): Loading sample assets/sfz/samples/MF-F3.wv
I/FLUTTER_SEQUENCER(20261): Loading sample assets/sfz/samples/Mp-C4.wv
I/FLUTTER_SEQUENCER(20261): Successfully loaded sample assets/wav/F3.wav
I/FLUTTER_SEQUENCER(20261): Loading sample assets/sfz/samples/MF-C4.wv
I/FLUTTER_SEQUENCER(20261): Loading sample assets/sfz/samples/FF-F3.wv
I/FLUTTER_SEQUENCER(20261): Loading sample assets/sfz/samples/Mp-C3.wv
I/FLUTTER_SEQUENCER(20261): Successfully loaded sample assets/wav/D3.wav
I/FLUTTER_SEQUENCER(20261): Loading sample assets/sfz/samples/MF-C3.wv
I/FLUTTER_SEQUENCER(20261): Loading sample assets/sfz/samples/FF-C3.wv
I/FLUTTER_SEQUENCER(20261): Loading sample assets/sfz/samples/FF-C4.wv
I/FLUTTER_SEQUENCER(20261): Successfully loaded sample assets/sfz/samples/FF-C3.wv
I/FLUTTER_SEQUENCER(20261): Successfully loaded sample assets/sfz/samples/MF-C4.wv
I/FLUTTER_SEQUENCER(20261): Successfully loaded sample assets/sfz/samples/Mp-C4.wv
I/FLUTTER_SEQUENCER(20261): Successfully loaded sample assets/sfz/samples/FF-C4.wv
I/FLUTTER_SEQUENCER(20261): Successfully loaded sample assets/sfz/samples/FF-F3.wv
I/FLUTTER_SEQUENCER(20261): Successfully loaded sample assets/sfz/samples/Mp-F3.wv
I/FLUTTER_SEQUENCER(20261): Successfully loaded sample assets/sfz/samples/MF-C3.wv
I/FLUTTER_SEQUENCER(20261): Successfully loaded sample assets/sfz/samples/MF-F3.wv
I/FLUTTER_SEQUENCER(20261): Successfully loaded sample assets/sfz/samples/Mp-C3.wv
W/libc    (20261): malloc(296392224) failed: returning null pointer
F/libc    (20261): Fatal signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x0 in tid 20691 (1.ui), pid 20261 (quencer_example)
*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
Build fingerprint: 'google/sdk_gphone_x86/generic_x86:10/QSR1.190920.001/5891938:user/release-keys'
Revision: '0'
ABI: 'x86'
Timestamp: 2020-11-11 23:36:05+0000
pid: 20261, tid: 20691, name: 1.ui  >>> com.michaeljperri.flutter_sequencer_example <<<
uid: 10137
signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x0
Cause: null pointer dereference
    eax be9cf568  ebx be9cf568  ecx 00000000  edx 00000004
    edi b73faf24  esi 00000800
    ebp b73fae88  esp b73fa630  eip be727ab6
backtrace:
      #00 pc 00099ab6  /data/app/com.michaeljperri.flutter_sequencer_example-ymh3CGzNCxMpMyM5_uuxBQ==/lib/x86/libflutter_sequencer.so (BuildId: dbe44abcb0c48571f6c4b7bb9b41ef5d6bb4d012)
      #01 pc 00098a3d  /data/app/com.michaeljperri.flutter_sequencer_example-ymh3CGzNCxMpMyM5_uuxBQ==/lib/x86/libflutter_sequencer.so (tsf_load+3261) (BuildId: dbe44abcb0c48571f6c4b7bb9b41ef5d6bb4d012)
      #02 pc 00098dc3  /data/app/com.michaeljperri.flutter_sequencer_example-ymh3CGzNCxMpMyM5_uuxBQ==/lib/x86/libflutter_sequencer.so (tsf_load_memory+163) (BuildId: dbe44abcb0c48571f6c4b7bb9b41ef5d6bb4d012)
      #03 pc 000abddd  /data/app/com.michaeljperri.flutter_sequencer_example-ymh3CGzNCxMpMyM5_uuxBQ==/lib/x86/libflutter_sequencer.so (SoundFontInstrument::SoundFontInstrument(int, bool, char const*, bool, int)+253) (BuildId: dbe44abcb0c48571f6c4b7bb9b41ef5d6bb4d012)
      #04 pc 000abc42  /data/app/com.michaeljperri.flutter_sequencer_example-ymh3CGzNCxMpMyM5_uuxBQ==/lib/x86/libflutter_sequencer.so (BuildId: dbe44abcb0c48571f6c4b7bb9b41ef5d6bb4d012)
      #05 pc 000abae9  /data/app/com.michaeljperri.flutter_sequencer_example-ymh3CGzNCxMpMyM5_uuxBQ==/lib/x86/libflutter_sequencer.so (BuildId: dbe44abcb0c48571f6c4b7bb9b41ef5d6bb4d012)
      #06 pc 000aba55  /data/app/com.michaeljperri.flutter_sequencer_example-ymh3CGzNCxMpMyM5_uuxBQ==/lib/x86/libflutter_sequencer.so (BuildId: dbe44abcb0c48571f6c4b7bb9b41ef5d6bb4d012)
      #07 pc 000ab670  /data/app/com.michaeljperri.flutter_sequencer_example-ymh3CGzNCxMpMyM5_uuxBQ==/lib/x86/libflutter_sequencer.so (BuildId: dbe44abcb0c48571f6c4b7bb9b41ef5d6bb4d012)
      #08 pc 0011a8e5  /apex/com.android.runtime/lib/bionic/libc.so (__pthread_start(void*)+53) (BuildId: 76290498408016ad14f4b98c3ab6c65c)
      #09 pc 000af6a7  /apex/com.android.runtime/lib/bionic/libc.so (__start_thread+71) (BuildId: 76290498408016ad14f4b98c3ab6c65c)
Lost connection to device.
Exited (sigterm)

After some discussion we determined that the important part of this error is:

malloc(296392224) failed: returning null pointer

and it's just happening when the Android emulator runs out of memory. It looks like there are two problems. One, hot reloading leaks memory, and two, the app crashes if you load too many samplers and run out of memory. It would also be great if there was a synthesizer that supports streaming samples from disk instead of loading them all into RAM. We can come back to these later.

_Originally posted by @mikeperri in https://github.com/mikeperri/flutter_sequencer/issues/2#issuecomment-730852806_

mikeperri commented 3 years ago

Hi Răzvan, I just published a new version of the library that uses a library called Sfizz for playing SFZ format, and it streams samples from disk.