Retrofit - I/art: Background sticky concurrent mark sweep GC freed

11,236

I realize this problem (infinate GC loop) happens when the model ("Usuario" in your case) have a member variable which causes serialization issue.

public class Usuario {
    @SerializedName("id")
    public String id;

    // the following will cause infinite loop of GC
    public TextView problemView;

    // use transient keyword will solve the problem in GC infinite loop
    private transient TextView okView;
}
Share:
11,236
Pablo Souza
Author by

Pablo Souza

Updated on June 30, 2022

Comments

  • Pablo Souza
    Pablo Souza almost 2 years

    I'm trying to make a simple REST request with Retrofit and i'm getting a lot of GC errors. I really don't know how to fix it. I tried to put more memory on emulator but the problem still happens. when make sync call Response<List<User>> response = usersCall.execute(); following exception

        java.lang.RuntimeException: An error occurred while executing doInBackground()
         android.os.AsyncTask$3.done(AsyncTask.java:309)
        at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
        at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
        at java.util.concurrent.FutureTask.run(FutureTask.java:242)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
    at java.lang.Thread.run(Thread.java:818)
    Cause by: java.lang.IllegalStateException: Expected a string but was BEGIN_ARRAY at line 1 column 2 path $
    at com.google.gson.stream.JsonReader.nextString(JsonReader.java:831)
    at com.google.gson.internal.bind.TypeAdapters$16.read(TypeAdapters.java:422)
    at com.google.gson.internal.bind.TypeAdapters$16.read(TypeAdapters.java:410)
    at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:37)
    at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:25)
    at retrofit2.ServiceMethod.toResponse(ServiceMethod.java:116)
    at retrofit2.OkHttpCall.parseResponse(OkHttpCall.java:211)
    at retrofit2.OkHttpCall.execute(OkHttpCall.java:174)
    at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall.execute(ExecutorCallAdapterFactory.java:89)
    

    and sync call using pojo model Response<List<User>> response = usersCall.execute(); then

    04-09 07:37:23.897 13396-13411/? I/art: Clamp target GC heap from 111MB to 96MB
    04-09 07:37:23.897 13396-13411/? I/art: Background partial concurrent mark sweep GC freed 3104(48KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 95MB/96MB, paused 7.520ms total 194.127ms
    04-09 07:37:23.897 13396-13396/? I/art: WaitForGcToComplete blocked for 182.909ms for cause Alloc
    04-09 07:37:23.929 13396-13411/? I/art: Background sticky concurrent mark sweep GC freed 128(5KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 95MB/96MB, paused 8.237ms total 31.761ms
    04-09 07:37:23.929 13396-13396/? I/art: WaitForGcToComplete blocked for 18.171ms for cause Alloc
    04-09 07:37:24.130 13396-13411/? I/art: Clamp target GC heap from 111MB to 96MB
    04-09 07:37:24.130 13396-13411/? I/art: Background partial concurrent mark sweep GC freed 5784(90KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 95MB/96MB, paused 9.388ms total 200.412ms
    04-09 07:37:24.130 13396-13396/? I/art: WaitForGcToComplete blocked for 200.758ms for cause Alloc
    04-09 07:37:24.158 13396-13411/? I/art: Background sticky concurrent mark sweep GC freed 1710(34KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 95MB/96MB, paused 6.897ms total 27.286ms
    04-09 07:37:24.158 13396-13396/? I/art: WaitForGcToComplete blocked for 18.297ms for cause Alloc
    04-09 07:37:24.346 13396-13396/? I/art: Clamp target GC heap from 111MB to 96MB
    04-09 07:37:24.346 13396-13396/? I/art: Alloc partial concurrent mark sweep GC freed 2092(33KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 95MB/96MB, paused 5.943ms total 188.191ms
    04-09 07:37:24.370 13396-13396/? I/art: Alloc sticky concurrent mark sweep GC freed 0(0B) AllocSpace objects, 0(0B) LOS objects, 0% free, 95MB/96MB, paused 8.501ms total 23.440ms
    04-09 07:37:24.566 13396-13396/? I/art: Clamp target GC heap from 111MB to 96MB
    04-09 07:37:24.566 13396-13396/? I/art: Alloc concurrent mark sweep GC freed 9(12KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 95MB/96MB, paused 6.105ms total 195.296ms
    04-09 07:37:24.566 13396-13411/? I/art: WaitForGcToComplete blocked for 395.241ms for cause Background
    04-09 07:37:24.598 13396-13411/? I/art: Background sticky concurrent mark sweep GC freed 0(0B) AllocSpace objects, 0(0B) LOS objects, 0% free, 95MB/96MB, paused 6.138ms total 29.195ms
    04-09 07:37:24.598 13396-13396/? I/art: WaitForGcToComplete blocked for 29.063ms for cause Alloc
    04-09 07:37:24.775 13396-13396/? I/art: Clamp target GC heap from 111MB to 96MB
    04-09 07:37:24.775 13396-13396/? I/art: Alloc partial concurrent mark sweep GC freed 16(56KB) AllocSpace objects, 1(54KB) LOS objects, 0% free, 95MB/96MB, paused 6.803ms total 177.429ms
    

    My code:

    Gson gson =
                    new GsonBuilder()
                            .registerTypeAdapter(Usuario.class, new UsuarioDeserializer())
                            .registerTypeAdapter(Date.class, new CustomDateDeserializer())
                            .create();
    
            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl(ConstantesUtil.URL_BASE)
                    .addConverterFactory(GsonConverterFactory.create(gson))
                    .build();
    
            UsuariosAPI usuarioApi = retrofit.create(UsuariosAPI.class);
    
            Usuario usuario = new Usuario(login, senha);
    
            Call<List<Usuario>> usuarios = usuarioApi.getUsuarios(usuario);
    
            usuarios.enqueue(new Callback<List<Usuario>>() {
                @Override
                public void onResponse(Call<List<Usuario>> call, Response<List<Usuario>> response) {
                    List<Usuario> usuarios = response.body();
    
                    for (Usuario u: usuarios) {
                        Log.i(TAG, u.getNome());
                    }
                }
    
                @Override
                public void onFailure(Call<List<Usuario>> call, Throwable t) {
    
                }
            });
    
  • Bhoomika Patel
    Bhoomika Patel about 6 years
    what to do if we have JsonObject with retrofit call?@Desmond Lua
  • Desmond Lua
    Desmond Lua about 6 years
    @BhoomikaPatel I have no experience with JsonObject using Retrofit. Why not use Gson with Retrofit call?
  • Bhoomika Patel
    Bhoomika Patel about 6 years
    i am already using Gson with Retrofit call, but still have problem of infinite GC loop. how can i fix it?
  • Desmond Lua
    Desmond Lua about 6 years
    @BhoomikaPatel do post a new question with your code.