f2prateek / rx-preferences

Reactive SharedPreferences for Android
http://f2prateek.com/2015/10/05/rx-preferences/
Apache License 2.0
1.54k stars 132 forks source link

OutOfMemoryError while serialize gson.toJson(obj) #121

Closed PraveenSheroes closed 6 years ago

PraveenSheroes commented 6 years ago

public class GsonConverter implements Preference.Converter { final Gson gson; private String json; private Class clazz; public GsonConverter(Gson gson, Class clazz) { this.gson = gson; this.clazz = clazz; } @NonNull @Override public T deserialize(@NonNull String serialized) { return gson.fromJson(serialized, clazz); }

@NonNull
@Override
public String serialize(@NonNull T t) {
    json = gson.toJson(t);
    return json;
}

}

f2prateek commented 6 years ago

Do you have a stacktrace?

PraveenSheroes commented 6 years ago

Caused by java.lang.OutOfMemoryError: Failed to allocate a 79288 byte allocation with 46080 free bytes and 44KB until OOM at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:95) at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:146) at java.lang.StringBuffer.append(StringBuffer.java:219) at java.io.StringWriter.write(StringWriter.java:147) at java.io.StringWriter.append(StringWriter.java:199) at java.io.StringWriter.append(StringWriter.java:30) at com.google.gson.stream.JsonWriter.beforeValue(JsonWriter.java:651) at com.google.gson.stream.JsonWriter.value(JsonWriter.java:481) at com.google.gson.internal.bind.TypeAdapters$3.write(TypeAdapters.java:165) at com.google.gson.internal.bind.TypeAdapters$3.write(TypeAdapters.java:151) at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:125) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:243) at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69) at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:97) at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:61) at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:125) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:243) at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69) at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:97) at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:61) at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:125) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:243) at com.google.gson.Gson.toJson(Gson.java:669) at com.google.gson.Gson.toJson(Gson.java:648) at com.google.gson.Gson.toJson(Gson.java:603) at com.google.gson.Gson.toJson(Gson.java:583) at appliedlife.pvtltd.SHEROES.preferences.GsonConverter.serialize(GsonConverter.java:24) at com.f2prateek.rx.preferences2.ConverterAdapter.set(ConverterAdapter.java:25) at com.f2prateek.rx.preferences2.RealPreference.set(RealPreference.java:72) at appliedlife.pvtltd.SHEROES.presenters.FeedPresenter$2.onNext(FeedPresenter.java:271) at appliedlife.pvtltd.SHEROES.presenters.FeedPresenter$2.onNext(FeedPresenter.java:253) at io.reactivex.observers.SerializedObserver.onNext(SerializedObserver.java:111) at io.reactivex.internal.operators.observable.ObservableTakeUntil$TakeUntilObserver.onNext(ObservableTakeUntil.java:87) at io.reactivex.internal.operators.observable.ObservableObserveOn$ObserveOnObserver.drainNormal(ObservableObserveOn.java:200) at io.reactivex.internal.operators.observable.ObservableObserveOn$ObserveOnObserver.run(ObservableObserveOn.java:252) at io.reactivex.android.schedulers.HandlerScheduler$ScheduledRunnable.run(HandlerScheduler.java:109) at android.os.Handler.handleCallback(Handler.java:815) at android.os.Handler.dispatchMessage(Handler.java:104) at android.os.Looper.loop(Looper.java:214) at android.app.ActivityThread.main(ActivityThread.java:6102) at java.lang.reflect.Method.invoke(Method.java) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)

f2prateek commented 6 years ago

Failed to allocate a 79288 byte allocation with 46080 free bytes and 44KB until OOM

I doubt this is an issue in rx-preferences or even GSON. You only had 44kb of memory left. Something else in your application is using up this memory.

PraveenSheroes commented 6 years ago

But I am getting this OutOfMemoryError only GsonConverter.serialize(), this method used for RxSharedPreferences @Singleton @Provides public Preference provideTokenPref(RxSharedPreferences rxSharedPreferences, Gson gson) { return rxSharedPreferences.getObject(AppConstants.AUTH_TOKEN, new LoginResponse(), new GsonConverter(gson, LoginResponse.class)); }