Denis Brandi
1 min readMar 17, 2021

--

Hi Farid, thanks for reading the article!

Wow 130MB sounds quite a lot!

At that point even serializers/deserializers are harmful.

Few key points for dealing efficiently with the problem:

1. Remove the remote DTO so you are not getting delayed by the reflection of Gson, Moshi, Jackson...

2. Map JsonObjects/JsonArrays to domain models in the data mapper (instead of DTOs).

Libraries like Gson stream the Json content instead of allocating the whole object in memory.

PS: heap will not be tripled in size, the moment the DTO is mapped into Domain model it gets deallocated because no longer of use, and after you map the domain model to the database DTO, this will have a short lifespan (until the insert/update query is being executed).

Anyway if you have heap issues you should get rid of DTOs instead of the domain model and use dynamic structures for the remote communication (JsonObject and raw queries), because DTOs affect only the datasource logic while domain models affect the whole codebase

--

--