尝试通过发送BASE64来发送大型BASE64时,对RX的改造会导致OutOfMemory不足 [英] Retrofit with RX runs OutOfMemory while trying to send a large BASE64

查看:102
本文介绍了尝试通过发送BASE64来发送大型BASE64时,对RX的改造会导致OutOfMemory不足的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想发送一个模型抛出Retrofit,其中包含一些标准字段和文件,它们以BASE64格式编码.我知道,这是一种疯狂的发送文件的方式,但是我有一台没有后端开发人员的服务器.

I want to send a model throw Retrofit, which contains some standard fields and files, encoded in BASE64 format. I know, that this is a crazy way to send files, but I have a server with no backend developer.

例如,当我发送3个pdf文件(每个20mb)时,我的内存用完了,像这样的异常

So, when I'm sending, for example, 3 pdf files 20mb each, I runs out of memory which exception like this

未能分配30544558字节分配,其中2085152个空闲字节和26MB直到OOM

Failed to allocate a 30544558 byte allocation with 2085152 free bytes and 26MB until OOM

我发现,解析base64字符串是一个问题,我已经在内存中供Json抛出GSON.根据

I figured out, that it's a problem in parse of base64 string, which I'm already have in memory to Json throw GSON. I tried to create to create a custom adapter, according to Retrofit OutOfMemory exception while loading a files in BASE64 to server but runs out of ideas in case of a large model. What will fix this problem with passing of a large string to a JSON? Maybe I can use other adapters, not a GSON?

Json中文档字段的模型和整个模型

Model of documents field in Json and whole model

data class Document(
    var base64: MutableList<String?>,
    var mime: String?,
    var name: String?
)

data class OrderMainModel(
    var INN: String?,
    var KPP: String?,
    var addressOfEstateObject: String?,
    var addressOfGardenObject: String?,
    var agentFathersName: String?,
    var agentFirstName: String?,
    var agentLastName: String?,
    var approximateSizeOfEstateObject: Double?,
    var approximateSizeOfOKS: Double?,
    var area: String?,
    var bankAccountNumber: Int?,
    var companyAddress: String?,
    var companyName: String?,
    var documents: List<Document?>
}

改造方法

@Streaming
@POST("/api/order/")
fun makeOrder(@Header("Authorization") token: String, @Body order: OrderMainModel): Single<Response<PhoneNumberResponse>>

推荐答案

也许,您应该将Flowable与适当的背压策略一起使用,而不要使用Single. 但是,如果您认为它适合您的适配器,则可以使用Jackson,因为它比Gson更快(根据

Maybe, you should use Flowable with proper backpressure strategy instead of Single. But if you think it's for your adapter you can use Jackson because it's faster than Gson(According to this article). And if you want more performance maybe you should use Protobuf instead of JSON.

这篇关于尝试通过发送BASE64来发送大型BASE64时,对RX的改造会导致OutOfMemory不足的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆