hex007 / freej2me

A free J2ME emulator with libretro, awt and sdl2 frontends.
Other
500 stars 78 forks source link

Close MIDI Player to prevent memory leak #23

Closed quadpixels closed 6 years ago

quadpixels commented 6 years ago

https://github.com/hex007/freej2me/blob/f3b6f0533b97153e6665aad2b1cad5cb47f019ff/src/org/recompile/mobile/PlatformPlayer.java#L142

Hello Hex007! I think calling MidiSequencer.close() when deallocating a player which is an instance of a MIDI player can help prevent memory leak. (There is a particular game that re-creates a MIDI player every time when the character enters a new scene, so after entering 100 scenes or so the program starts to consume ~600 MB of memory and have ~200 running threads.)

On Linux, having more than a few MIDI players will cause the music to stop, but this doesn't happen on Windows.

The fix may look like the following:

public void deallocate() {
    stop();
    if (player instanceof midiPlayer) {
        ((midiPlayer)player).midi.close();
    }
    player = null;
    state = Player.CLOSED;
}

Thanks!

p.s. A similar issue and the usage of close() is mentioned in this Stackoverflow post: Java Sequencer using all memory? (7kb midi file)