7

Out of the following two option, which one do you prefer to init Gson object? Can someone help me understand the impact of both ways on Android app performance (start up time?), if there's any.

// inline creation
private final Gson gson = new Gson();

vs

// need base creation, overhead of sync everytime while accessing gson object
private synchronized Gson getOrCreateGson() {
    gson == null ? gson = new Gson() : gson;
    return gson.fromJson(jsonString, clazz);
}
Muzaffer
  • 1,457
  • 1
  • 13
  • 22
ua741
  • 1,446
  • 15
  • 28

6 Answers6

8

It's up to you but there's nothing wrong with a private static final to take care of this. You can read more about it at this related question.

private static final Gson gson = new Gson();
Community
  • 1
  • 1
MrEngineer13
  • 38,642
  • 13
  • 74
  • 93
0

Okay , as from your question you are asking which method is batter for handling json data !

then for that i am suggesting you 2 options !!

  1. https://github.com/loopj/android-async-http

  2. https://github.com/mcxiaoke/android-volley

Let me know which is best for you !!

Happy coding !!

Madhav Anadkat
  • 56
  • 1
  • 15
  • No, I am asking what's "Best way to init Gson object". It might happen during my app life-cycle i.e. When user opens app, does something, I might not need to parse any Json data. – ua741 May 06 '15 at 13:18
  • when you have a Data then initialize the json object ! from any where , but make sure you have a Data !! – Madhav Anadkat May 06 '15 at 13:20
0

If your gson object is not static, then you don't have to check gson is null. If you sure that gson is going to use often, inline option is better(no null checking). If gson usage is too sparse, second option maybe better. A lot of "if" :)

On the other hand, How many milliseconds takes initializing gson? I think you don't need to consider performance in this case.

Muzaffer
  • 1,457
  • 1
  • 13
  • 22
0

Check this one.

public static Gson gson;

public Constructor() {

}

public static Gson getInstance() {
    if (gson == null) {
        gson = new Gson();
    }

    return gson;
}

You just have to call getInstance() function to get initialized gson object for ready to use. Every time it will not initialize new Gson object and reused old initialized object.

Ziem
  • 6,579
  • 8
  • 53
  • 86
Kirankumar Zinzuvadia
  • 1,249
  • 10
  • 17
0

Personally I have it as a Dagger2 Singleton Module:

@Module
public class GsonModule {
    @Provides
    @Singleton
    public Gson providesGson() {
        Gson gson = new GsonBuilder()
            .setExclusionStrategies(new ExclusionStrategy() { //Realm workaround
            @Override
            public boolean shouldSkipField(FieldAttributes f) {
                return f.getDeclaringClass().equals(RealmObject.class);
            }

            @Override
            public boolean shouldSkipClass(Class<?> clazz) {
                return false;
            }
        }).create();
        return gson;
    }
}

@Module
public class ApplicationModule {
    private final CustomApplication application;

    public ApplicationModule(CustomApplication application) {
        this.application = application;
    }

    @Provides
    @Singleton
    public CustomApplication application() {
        return this.application;
    }
}

@Singleton
@Component(modules = {GsonModule.class, ApplicationModule.class})
public interface ApplicationComponent {
    void inject(MainActivity mainActivity);
    //exposed to sub-modules.
    CustomApplication application();
}

But technically that's like having a single singleton object somewhere.

EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
0

One instantiates a new object the other is ... wrong because it has two returns.

Without the said problem in your code, the first is better simply because in the second you have

return gson == null ? gson = new Gson() : gson;

which has an overhead null check and has to be done everytime you request a gson object. However this has nothing to do with Gson. This would work the same for any other instantiation. So you can use the first just fine and in regard of Gson the consideration would be if it's better to use new Gson() or GsonBuilder.create() which has more customizations.

inmyth
  • 8,880
  • 4
  • 47
  • 52