简体   繁体   English

使用 akka 流将数据分块写入文件

[英]Write data to file in chunks using akka streams

I use sttp lib with akka backend to load a file from server.我使用带有 akka 后端的 sttp lib 从服务器加载文件。 Either of the following approaches results in significant memory footprint to load 1Gb file:以下任一方法都会导致大量内存占用来加载 1Gb 文件:

import com.softwaremill.sttp._

val file: File = new File(...)

sttp.response(asStream[Source[ByteString, Any]])
.mapResponse { src =>
    src.runWith(FileIO.toPath(file.toPath, options, 0))
}

sttp.response(asFile(file, false))

VisualVM plots of sequential loads for 1Gb file. 1Gb 文件的顺序加载的 VisualVM 图。 在此处输入图片说明

Is there any chance to write data in chunks and evict chunks from memory right after the write?是否有机会在写入后立即以块的形式写入数据并从内存中逐出块

According to your plot, your application does not require a significant amount of memory.根据您的情节,您的应用程序不需要大量内存。 There are "spikes" up to 1700 MB, but just after that the Garbage Collector runs the usage of the heap drops to 250 MB.有高达 1700 MB 的“峰值”,但在垃圾收集器运行之后,堆的使用量下降到 250 MB。 sttp and Akka creates a lot of short-living objects; sttp 和 Akka 创建了很多短暂的对象; however, Garbage Collector cleans your memory quite well.但是,垃圾收集器可以很好地清理您的内存。

I've run your client app with 124 MB memory only to verify that it does not need 2GB of heap to download 1GB file:我已经使用 124 MB 内存运行您的客户端应用程序,只是为了验证它不需要 2GB 的堆来下载 1GB 的文件:

sbt -mem 124 run

The app didn't crash, it just used as much memory as it was available.该应用程序没有崩溃,它只是使用了可用的内存。 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM