android: gson performance

后端 未结 5 1715
滥情空心
滥情空心 2020-12-14 10:42

I am trying to use gson to do my object mapping on the android emulator.

It has been ridiculously slow when processing json data around 208 kb. I do not have any hie

相关标签:
5条回答
  • 2020-12-14 10:52

    I've found that I can speed up gson.fromJSON quite considerably by not modelling all the elements in the JSON that I won't need. GSON will happily fill in only what is specified in your response classes.

    0 讨论(0)
  • 2020-12-14 10:59

    I've seen questions like this come up before, and the general consensus is that Jackson is much faster than Gson. See the following links for more information:

    • Jackson Vs. Gson
    • Replace standard Android JSON parser for better performance?
    • http://www.cowtowncoder.com/blog/archives/2009/12/entry_345.html
    • https://stackoverflow.com/questions/338586/a-better-java-json-library

    Here is one which specifically discusses Android: http://ubikapps.net/?p=525

    0 讨论(0)
  • 2020-12-14 11:04

    You'd probably get better performance if you wrapped that InputStream in a BufferedInputStream with a nice big buffer...

    3 minutes is insane. I seldom run the emulator but I have an app with a ~1.1MB JSON asset and that takes around 5 seconds to load and process on hardware.

    (Which is still far too long, but still).

    0 讨论(0)
  • 2020-12-14 11:06

    I have found that CREATING a Gson instance is a very expensive operation, both in terms of CPU used and memory allocated.

    Since Gson instances are thread-safe, constructing and reusing a single static instance pays off, especially if you are serializing / deserializing often.

    0 讨论(0)
  • 2020-12-14 11:10

    Have you tried the mixing the GSON streaming parser with the Gson object? http://sites.google.com/site/gson/streaming (look for the Mixed read example).

    This approach may help since Gson reads in an entire parse tree and then acts on it. With a large array list, reading in all elements and attempting to parse may cause lot of memory swaps (or thrashing). This approach will read in one element at a time.

    Hope this helps.

    0 讨论(0)
提交回复
热议问题