Flutter C++ Memory allocation causes jank on raster thread – Android NDK Dart FFI


I have a flutter app which uses Dart ffi to connect to my custom C++ audio backend. There I allocate around 10MB of total memory for my audio buffers. Each buffer has 10MB / 84 of memory. I use 84 audio players. Here is the ffi flow:

C++ bridge:

extern "C" __attribute__((visibility("default"))) __attribute__((used))
void *
loadMedia(char *filePath, int8_t *mediaLoadPointer, int64_t *currentPositionPtr, int8_t *mediaID) {
    LOGD("loadMedia %s", filePath);

    if (soundEngine == nullptr) {
        soundEngine = new SoundEngine();

    return soundEngine->loadMedia(filePath, mediaLoadPointer, currentPositionPtr, mediaID);

In my sound engine I launch a C++ thread:

void loadMedia(){


void startDecoderWorker(float*buffer){
     buffer = new float[30000]; // 30000 might be wrong here, I entered a huge value to just showcase the problem, the calculation of 10MB / 84 code is redundant to the code

So here is the problem, I dont know why but when I allocate memory with new keyword even inside a C++ thread, flutters raster thread janks and I can see that my flutter UI janks lots of frames. This is also present in performance overlay as it goes all red for 3 to 5 frames with each of it taking around 30 40ms. Tested on profile mode.

Here is how I came to this conclusion:
If I instantly return from my startDecoderWorker without running new memory allocation code, when I do this there is 0 jank. Everything is smooth 60fps, performance overlay doesnt show me red bars.

Here are some screenshots from Profile mode:

enter image description here
enter image description here
enter image description here


The actual cause, after discussions (in the comments of the question), is not because the memory allocation is too slow, but lie somewhere else – the calculations which will be heavy if the allocation is big.

For details, please refer to the comments and discussions of the question 😉

Answered By – ch271828n

Answer Checked By – David Goodson (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *