performance recommendations regarding the Gson instance

1,094 views
Skip to first unread message

Daniel

unread,
Oct 20, 2008, 1:50:31 PM10/20/08
to google-gson
Should I re-use the same Gson instance? Are there any performance
benefits like cached reflection lookups if do so?

Daniel

inde...@gmail.com

unread,
Oct 20, 2008, 2:26:44 PM10/20/08
to google-gson
Yes, Gson is designed to be stateless from the point of view of the
object that it is called upon. So, it is best to just create Gson
instance once, and then reuse it everywhere.

Creating Gson is a somewhat expensive operation since it registers all
the default serializers, deserializers and instance creators. We have
tried to optimize that path by storing the default list as a static
final constant but still it is best to reuse the same instance of Gson
wherever you can. That will also give you the benefits of any JVM
hotspot optimizations that may happen because of the reused instance.

We typically use Guice to inject Gson once, and use that everywhere.

Inder

Reply all
Reply to author
Forward
0 new messages