通过Flutter的平台通道发送数据太慢

时间:2020-03-27 12:29:53

标签: java android flutter dart

我正在利用Flutter的平台通道从Dart端向Java发送“大量”数据。 这是一个 Float32List ,由393216个元素组成,大约为1.6 MB。

在Dart端,我正在使用以下功能来发送数据:

  Future<Null> sendData(Float32List data) {
_channel.invokeMethod('sendData', {'data': data});
    return null;
  }

,Java端按以下方式处理传入消息:

  @Override
  public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {

    Map<String, ?> arguments = (Map<String, ?>) call.arguments;
    if (call.method.equals("sendData")) {
}
}

因此,目前无非就是将MethodCall的参数映射到HashMap。

无论如何,该数据需要每帧传输一次,例如每16毫秒一次。 不幸的是,帧速率从60 fps大幅下降到20 fps。 检查日志似乎似乎是问题的一部分似乎是垃圾收集器,该垃圾收集器不断启动以释放内存。

I/send_data( 6306): Waiting for a blocking GC Alloc
I/send_data( 6306): Background concurrent copying GC freed 1(15KB) AllocSpace objects, 0(0B) LOS objects, 3% free, 165MB/171MB, paused 477us total 675.471ms
I/send_data( 6306): WaitForGcToComplete blocked Alloc on ProfileSaver for 256.348ms
I/flutter ( 6306): FPS 21.1287106362165615
I/send_data( 6306): Starting a blocking GC Alloc
I/send_data( 6306): Alloc concurrent copying GC freed 4665422(89MB) AllocSpace objects, 2(72MB) LOS objects, 50% free, 4MB/8MB, paused 439us total 28.436ms
I/send_data( 6306): Background concurrent copying GC freed 53274(859KB) AllocSpace objects, 0(0B) LOS objects, 9% free, 59MB/65MB, paused 995us total 116.669ms
I/send_data( 6306): Background concurrent copying GC freed 0(30KB) AllocSpace objects, 0(0B) LOS objects, 6% free, 84MB/90MB, paused 417us total 1.021s
I/send_data( 6306): Background concurrent copying GC freed 83319(1317KB) AllocSpace objects, 0(0B) LOS objects, 4% free, 132MB/138MB, paused 2.752ms total 1.096s
I/flutter ( 6306): FPS 21.1287106362165615
I/send_data( 6306): Waiting for a blocking GC Alloc
I/send_data( 6306): Background concurrent copying GC freed 2253876(43MB) AllocSpace objects, 1(36MB) LOS objects, 6% free, 86MB/92MB, paused 916us total 312.417ms
I/send_data( 6306): WaitForGcToComplete blocked Alloc on HeapTrim for 53.920ms
I/send_data( 6306): Starting a blocking GC Alloc
I/send_data( 6306): Background concurrent copying GC freed 2429155(46MB) AllocSpace objects, 1(36MB) LOS objects, 9% free, 55MB/61MB, paused 1.316ms total 227.108ms
I/flutter ( 6306): FPS 21.1287106362165615
I/flutter ( 6306): FPS 21.0497952899184655
I/send_data( 6306): Background concurrent copying GC freed 35649(581KB) AllocSpace objects, 0(0B) LOS objects, 7% free, 75MB/81MB, paused 2.148ms total 778.779ms
I/Choreographer( 6306): Skipped 418 frames!  The application may be doing too much work on its main thread.
I/flutter ( 6306): FPS 20.9614768284084351
I/send_data( 6306): Background concurrent copying GC freed 1(18KB) AllocSpace objects, 0(0B) LOS objects, 6% free, 84MB/90MB, paused 3.093ms total 820.288ms
I/flutter ( 6306): FPS 20.9112724400838368

可能是因为在Java端,每次调用都会为传入的数据分配新的内存。

所以我想问自己是上述的正确方法吗?还是有另一种方法可以更有效地做到这一点?有没有一种方法可以共享特定的内存区域,使得Dart和Java端可以在不使用平台通道的情况下进行读写访问?

更新

如果使用 Float64List 而不是Float32List,则运行会好得多。不过,我不确定是否有优势,因为所有double值都需要在Java端转换为float。

1 个答案:

答案 0 :(得分:0)

是的,有办法,但不是最好的。我的解决方案是使用dart将数据保存在存储中,并通过java读取文件do操作并保存。最后从手机存储中读取新数据。