Bugando Activity + Memory Error

Asked

Viewed 43 times

0

Good afternoon programmers, I am developing an application and in a certain part of it I populate textviews with some data coming from a webservice. When ordering data from the webservice in a specific class of everything the object comes complete, however when returning this object the class of the fragment I must popular the object comes incomplete giving some nulls in the textviews, sometimes in all... And then if I rotate the screen . The console returns me an error and gives a crash in the application.

follow the console errors:

    05-23 11:21:01.256 13392-13392/com.example.pizzamania.maniadepizza E/MotionRecognitionManager: mSContextService = null
05-23 11:21:01.256 13392-13392/com.example.pizzamania.maniadepizza E/MotionRecognitionManager: motionService = com.samsung.android.motion.IMotionRecognitionService$Stub$Proxy@38db738a
05-23 11:21:01.266 13392-13894/com.example.pizzamania.maniadepizza I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
05-23 11:21:01.281 13392-13392/com.example.pizzamania.maniadepizza I/art: WaitForGcToComplete blocked for 16.790ms for cause Alloc
05-23 11:21:01.286 13392-13894/com.example.pizzamania.maniadepizza I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
05-23 11:21:01.286 13392-13894/com.example.pizzamania.maniadepizza I/System.out: KnoxVpnUidStorageknoxVpnSupported API value returned is false
05-23 11:21:01.286 13392-13894/com.example.pizzamania.maniadepizza I/System.out: KnoxVpnUidStorageknoxVpnSupported API value returned is false
05-23 11:21:01.311 13392-13392/com.example.pizzamania.maniadepizza I/art: Alloc partial concurrent mark sweep GC freed 2174(212KB) AllocSpace objects, 6(912KB) LOS objects, 20% free, 30MB/38MB, paused 953us total 29.953ms
05-23 11:21:01.321 13392-13392/com.example.pizzamania.maniadepizza I/art: Alloc sticky concurrent mark sweep GC freed 0(0B) AllocSpace objects, 0(0B) LOS objects, 20% free, 30MB/38MB, paused 954us total 5.065ms
05-23 11:21:01.356 13392-13392/com.example.pizzamania.maniadepizza I/art: Alloc concurrent mark sweep GC freed 2538(222KB) AllocSpace objects, 0(0B) LOS objects, 20% free, 30MB/38MB, paused 909us total 35.674ms
05-23 11:21:01.361 13392-13392/com.example.pizzamania.maniadepizza I/art: Forcing collection of SoftReferences for 2GB allocation
05-23 11:21:01.401 13392-13392/com.example.pizzamania.maniadepizza I/art: Alloc concurrent mark sweep GC freed 11(344B) AllocSpace objects, 0(0B) LOS objects, 20% free, 30MB/38MB, paused 883us total 40.670ms
05-23 11:21:01.401 13392-13392/com.example.pizzamania.maniadepizza E/art: Throwing OutOfMemoryError "Failed to allocate a 2284037732 byte allocation with 8388512 free bytes and 225MB until OOM"
05-23 11:21:01.426 13392-13399/com.example.pizzamania.maniadepizza W/art: Suspending all threads took: 6.736ms
05-23 11:21:01.431 13392-13392/com.example.pizzamania.maniadepizza I/art: Alloc partial concurrent mark sweep GC freed 6(192B) AllocSpace objects, 0(0B) LOS objects, 20% free, 30MB/38MB, paused 1.796ms total 22.305ms
05-23 11:21:01.466 13392-13392/com.example.pizzamania.maniadepizza I/art: Alloc concurrent mark sweep GC freed 7(224B) AllocSpace objects, 0(0B) LOS objects, 20% free, 30MB/38MB, paused 804us total 34.129ms
05-23 11:21:01.471 13392-13392/com.example.pizzamania.maniadepizza I/art: Forcing collection of SoftReferences for 2GB allocation
05-23 11:21:01.501 13392-13392/com.example.pizzamania.maniadepizza I/art: Alloc concurrent mark sweep GC freed 3(96B) AllocSpace objects, 0(0B) LOS objects, 20% free, 30MB/38MB, paused 774us total 33.736ms
05-23 11:21:01.551 13392-13392/com.example.pizzamania.maniadepizza E/art: Throwing OutOfMemoryError "Failed to allocate a 2284037732 byte allocation with 8388608 free bytes and 225MB until OOM"
05-23 11:21:01.551 13392-13392/com.example.pizzamania.maniadepizza D/AndroidRuntime: Shutting down VM
05-23 11:21:01.551 13392-13392/com.example.pizzamania.maniadepizza E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                     Process: com.example.pizzamania.maniadepizza, PID: 13392
                                                                                     java.lang.OutOfMemoryError: Failed to allocate a 2284037732 byte allocation with 8388608 free bytes and 225MB until OOM
                                                                                         at java.lang.AbstractStringBuilder.<init>(AbstractStringBuilder.java:83)
                                                                                         at java.lang.StringBuilder.<init>(StringBuilder.java:67)
                                                                                         at com.example.pizzamania.maniadepizza.CodMapaPizzarias.carregaSobre(CodMapaPizzarias.java:115)
                                                                                         at com.example.pizzamania.maniadepizza.CodMapaPizzarias.onCreateView(CodMapaPizzarias.java:88)
                                                                                         at android.support.v4.app.Fragment.performCreateView(Fragment.java:2192)
                                                                                         at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1299)
                                                                                         at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528)
                                                                                         at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1595)
                                                                                         at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:2900)
                                                                                         at android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:201)
                                                                                         at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:603)
                                                                                         at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:178)
                                                                                         at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1264)
                                                                                         at android.app.Activity.performStart(Activity.java:6613)
                                                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3153)
                                                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3300)
                                                                                         at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:5206)
                                                                                         at android.app.ActivityThread.access$1100(ActivityThread.java:211)
                                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1711)
                                                                                         at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                         at android.os.Looper.loop(Looper.java:145)
                                                                                         at android.app.ActivityThread.main(ActivityThread.java:6946)
                                                                                         at java.lang.reflect.Method.invoke(Native Method)
                                                                                         at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
                                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)

It also follows my class searching the object on the webserver:

public class AsyncWebServer {

private static String SOAP_ACTION_INFO = "tempuri.org/MOBILEretornaPizzariasINFO";
private static String NAMESPACE = "tempuri.org/";
private static String METHOD_NAME_INFO = "MOBILEretornaPizzariasINFO";
private static String URL = "http://192.168.0.23/WebService.asmx?wsdl";

private static Context mCont;
static ObjPizzaria obj = new ObjPizzaria();
static ArrayList<ObjAvaliacoes> arrayobj;

public AsyncWebServer(Context m){
    this.mCont = m;
}

public static ObjPizzaria retornaINFO(int id){

    asyncINFO async = new asyncINFO();
    async.execute(id);
    return obj;
}
private static class asyncINFO extends AsyncTask<Integer,ObjPizzaria, ObjPizzaria>{
    @Override
    protected ObjPizzaria doInBackground(Integer... id) {
        ObjPizzaria pizzariaInfo = new ObjPizzaria();

        SoapObject resposta = new SoapObject(NAMESPACE, METHOD_NAME_INFO);
        resposta.addProperty("id", id[0]);

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(resposta);

        HttpTransportSE transportSE = new HttpTransportSE(URL, 9000);
        String resultado = null;
        JSONArray json = null;
        Base64toBMP imagem = new Base64toBMP();
        try {
            transportSE.call(SOAP_ACTION_INFO, envelope);
            resultado = envelope.getResponse().toString();
            json = new JSONArray(resultado);
            JSONObject jsonObj = json.getJSONObject(0);

            pizzariaInfo.setCep(jsonObj.getInt("CEP"));
            pizzariaInfo.setAval(Float.parseFloat(jsonObj.getString("NOTA")));
            pizzariaInfo.setBairro(jsonObj.getString("BAIRRO"));
            pizzariaInfo.setCidade(jsonObj.getString("CIDADE"));
            pizzariaInfo.setNome(jsonObj.getString("NOME"));
            pizzariaInfo.setCompl(jsonObj.getString("COMPLEMENTO"));
            LatLng ll = new LatLng(jsonObj.getDouble("LAT"), jsonObj.getDouble("LNG"));
            pizzariaInfo.setCoord(ll);
            pizzariaInfo.setEmail(jsonObj.getString("EMAIL"));
            pizzariaInfo.setId(id[0]);
            pizzariaInfo.setLogo(imagem.retornaIMG(jsonObj.getInt("ID_IMG")));
            pizzariaInfo.setLogr(jsonObj.getString("LOGRADOURO"));
            pizzariaInfo.setNumero(jsonObj.getInt("NUMERO"));
            pizzariaInfo.setRefe(jsonObj.getString("REFERENCIA"));
            pizzariaInfo.setSobre(jsonObj.getString("SOBRE"));
            pizzariaInfo.setTelefone(jsonObj.getInt("TELEFONE"));
            pizzariaInfo.setTelefone2(jsonObj.getInt("TELEFONE_2"));
            pizzariaInfo.setUf(jsonObj.getString("UF"));
            obj = new ObjPizzaria();
            obj = pizzariaInfo;
        } catch (IOException e) {
            Log.e("Erro1", e.toString());
        } catch (XmlPullParserException e) {
            Log.e("Erro1", e.toString());
        } catch (JSONException e) {
            Log.e("Erro1", e.toString());
        }

        return obj;
    }

}
}

And the class of the fragment that receives the object:

public class CodMapaPizzarias extends Fragment implements View.OnClickListener{

int id;
ImageView imgLogo;
TextView txtNome;
RatingBar notaMedia;
Button cardapio;
TextView sobre;
TextView rua;
TextView refe;
TextView cidadeUF;
TextView tel1;
TextView tel2;
RatingBar minhaaval;
FloatingActionButton btnLigar;
ToggleButton fav;
ListView listaAval;
Context cont = Contexto.context;


private ObjPizzaria obj;
private ArrayList<ObjAvaliacoes> avaliacoes;

public CodMapaPizzarias() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    Bundle bundle = getArguments();

    // Inflate the layout for this fragment
    View v = inflater.inflate(R.layout.frag_tela_mapa_pizzarias, container, false);

    imgLogo = (ImageView) v.findViewById(R.id.sobre_imgLogo);
    txtNome = (TextView) v.findViewById(R.id.sobre_txtNome);
    notaMedia = (RatingBar) v.findViewById(R.id.sobre_Notapizzaria);
    cardapio = (Button) v.findViewById(R.id.sobre_btnCardapio);
    cardapio.setOnClickListener(this);
    sobre = (TextView) v.findViewById(R.id.sobre_txtVsobre);
    rua = (TextView) v.findViewById(R.id.Sobre_txtvrua);
    refe = (TextView) v.findViewById(R.id.sobre_txtvRefe);
    cidadeUF = (TextView) v.findViewById(R.id.sobre_txtvCidadeUf);
    tel1 = (TextView) v.findViewById(R.id.txtv_SobreTel1);
    tel2 = (TextView) v.findViewById(R.id.txtv_SobreTel2);
    minhaaval = (RatingBar) v.findViewById(R.id.sobre_avaliar);
    minhaaval.setOnClickListener(this);
    btnLigar = (FloatingActionButton) v.findViewById(R.id.flLigar);
    btnLigar.setOnClickListener(this);
    fav = (ToggleButton) v.findViewById(R.id.tbtnFav);
    fav.setOnClickListener(this);
    listaAval = (ListView) v.findViewById(R.id.sobre_lstAval);


    if (bundle != null)
    {
        id = bundle.getInt("id");
        carregaSobre(id);
    }
    return v;
}

private void carregaSobre(int id_load) {
    AsyncWebServer asyncWebServer = new AsyncWebServer(cont);
    obj = new ObjPizzaria();
    try {
        obj = asyncWebServer.retornaINFO(id_load);
    }
    catch(Exception e){
        Log.e("Erro1", e.toString());
    }
  //  avaliacoes = asyncWebServer.retornaPizzariaAVAL(id_load);

    imgLogo.setImageBitmap(obj.getLogo());
    txtNome.setText(obj.getNome());
    notaMedia.setRating(obj.getAval());
    sobre.setText(obj.getSobre());
    rua.setText(obj.getLogr()+ ", " + obj.getNumero() + " " + obj.getCompl() + " " + obj.getBairro());
    StringBuilder cep = new StringBuilder(obj.getCep());
    if(cep.length() == 8) {
        cep.insert(5, "-");
    }
    refe.setText(obj.getRefe() + "\n" + cep);
    cidadeUF.setText(obj.getCidade() + "-" + obj.getUf());
    StringBuilder tele1 = new StringBuilder(obj.getTelefone());
    if (tele1.length() == 11) {
        tele1.insert(0, "(");
        tele1.insert(3, ") ");
        tele1.insert(10, "-");
    }
    else if (tele1.length() == 10){
        tele1.insert(0, "(");
        tele1.insert(3, ") ");
        tele1.insert(9, "-");
    }
    tel1.setText(tele1);
    StringBuilder tele2 = new StringBuilder(obj.getTelefone2());
    if (tele2.length() == 11) {
        tele2.insert(0, "(");
        tele2.insert(3, ") ");
        tele2.insert(10, "-");
    }
    else if (tele2.length() == 10){
        tele2.insert(0, "(");
        tele2.insert(3, ") ");
        tele2.insert(9, "-");
    }
    tel2.setText(tele1);
}

@Override
public void onClick(View v) {

}
}

I’m running everything on a Galaxy J2 (I don’t know if it influences these errors). I’m sorry if it’s obvious, because I’m a layman still on the subject...

1 answer

0

The error is simple, exactly what is written java.lang.OutOfMemoryError: Failed to allocate a 2284037732 byte Common error for beginners, you are consuming much more memory than your phone supports, you are loading lots of data from one tip at a time by your web-service.

2284037732 byte is a lot for an app

seen that you are loading an image, the most common mistake is to put many large and not optimized images for phones.

  1. Do not load all the data from your database into a single call in the webservice, if it is too much data divide into other endpoints or load data into a few "ex, if it is a list instead of loading all the items in the list press 100 on 100"
  2. Do not send images via webservice, just send the absolute path to the URL and use libraries as Picasso to display the images

  3. Optimize images to the maximum size possible.

  • I even found it strange this whole size ... but it has a line with at most 300 characters in json and an image of 1mb in the other call, I don’t know what to do ...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.