square / retrofit

A type-safe HTTP client for Android and the JVM
https://square.github.io/retrofit/
Apache License 2.0
43.1k stars 7.3k forks source link

Gson parsing freezes UI #1067

Closed ffelini closed 9 years ago

ffelini commented 9 years ago

We just started to use some services in our project with big data results. First thing we've noticed is that the UI freezes when response come. The api response is really big so we think it is cause of it. We do async. retrofit calls so logically everything should be done in background. Does deserialization works on the main thread ?

artem-zinnatullin commented 9 years ago

Try to debug it and see on what thread Gson do its work.

Probably, big responses cause frequent or long GCs which lead to UI freezes.

jbaginski commented 9 years ago

@ffelini check your callback executor.

ffelini commented 9 years ago

Thank you, I'l check it. Callback executor is fine, only adapter.notifyDataSetChanged() called there. @artem-zinnatullin I think you may be right. But how generally this kind of issues is avoided (when GC becomes crazy cause of big responses) ? I understand that this may be not a Retrofit related issue.

ffelini commented 9 years ago

Anyone tried this library with retrofit? https://github.com/bluelinelabs/LoganSquare Unfortunatelly not so much feedbacks about it, but looks promising.

artem-zinnatullin commented 9 years ago

But how generally this kind of issues is avoided (when GC becomes crazy cause of big responses) ?

Usually, the solution is to use stream-based parsing which will parse response stream instead of loading all response to the memory and only then parsing it.

But, anyway, if you have REALLY big responses — JSON is not best format for such cases because it has a lot of duplications (imagine huge array of objects with same structure). Consider switching to more efficient formats, such as: Apache Trift, Protocol Buffers, FlatBuffers, etc.

ffelini commented 9 years ago

@artem-zinnatullin Thank you for quick feedbacks. Unfortunatelly we have only Json, no choice :) I'l see what I can do

artem-zinnatullin commented 9 years ago

@ffelini if you want to use JSON try all available parsers and choose the best one which will be fast and good enough for you, also make sure that responses are Gzipped: in combination with Stream parsing it should give good results!

Some JSON parsers/serializers: Jackson, Moshi, Ig-Json-Parser, LoganSquare.

ffelini commented 9 years ago

Thank you for help :)